Form.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Variable\Block\System\Variable\Edit;
  7. /**
  8. * Custom Variable Edit Form
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Form extends \Magento\Backend\Block\Widget\Form\Generic
  14. {
  15. /**
  16. * Getter
  17. *
  18. * @return \Magento\Variable\Model\Variable
  19. */
  20. public function getVariable()
  21. {
  22. return $this->_coreRegistry->registry('current_variable');
  23. }
  24. /**
  25. * Prepare form before rendering HTML
  26. *
  27. * @return \Magento\Variable\Block\System\Variable\Edit\Form
  28. */
  29. protected function _prepareForm()
  30. {
  31. /** @var \Magento\Framework\Data\Form $form */
  32. $form = $this->_formFactory->create(
  33. ['data' => ['id' => 'edit_form', 'action' => $this->getData('action'), 'method' => 'post']]
  34. );
  35. $fieldset = $form->addFieldset('base', ['legend' => __('Variable'), 'class' => 'fieldset-wide']);
  36. $fieldset->addField(
  37. 'code',
  38. 'text',
  39. [
  40. 'name' => 'code',
  41. 'label' => __('Variable Code'),
  42. 'title' => __('Variable Code'),
  43. 'required' => true,
  44. 'class' => 'validate-xml-identifier'
  45. ]
  46. );
  47. $fieldset->addField(
  48. 'name',
  49. 'text',
  50. ['name' => 'name', 'label' => __('Variable Name'), 'title' => __('Variable Name'), 'required' => true]
  51. );
  52. $useDefault = false;
  53. if ($this->getVariable()->getId() && $this->getVariable()->getStoreId()) {
  54. $useDefault = !(bool)$this->getVariable()->getStoreHtmlValue();
  55. $this->getVariable()->setUseDefaultValue((int)$useDefault);
  56. $fieldset->addField(
  57. 'use_default_value',
  58. 'select',
  59. [
  60. 'name' => 'use_default_value',
  61. 'label' => __('Use Default Variable Values'),
  62. 'title' => __('Use Default Variable Values'),
  63. 'onchange' => 'toggleValueElement(this);',
  64. 'values' => [0 => __('No'), 1 => __('Yes')]
  65. ]
  66. );
  67. }
  68. $fieldset->addField(
  69. 'html_value',
  70. 'textarea',
  71. [
  72. 'name' => 'html_value',
  73. 'label' => __('Variable HTML Value'),
  74. 'title' => __('Variable HTML Value'),
  75. 'disabled' => $useDefault
  76. ]
  77. );
  78. $fieldset->addField(
  79. 'plain_value',
  80. 'textarea',
  81. [
  82. 'name' => 'plain_value',
  83. 'label' => __('Variable Plain Value'),
  84. 'title' => __('Variable Plain Value'),
  85. 'disabled' => $useDefault
  86. ]
  87. );
  88. $form->setValues($this->getVariable()->getData())->addFieldNameSuffix('variable')->setUseContainer(true);
  89. $this->setForm($form);
  90. return parent::_prepareForm();
  91. }
  92. }