Form.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Block\Widget;
  7. use Magento\Framework\App\ObjectManager;
  8. /**
  9. * Backend form widget
  10. *
  11. * @api
  12. * @deprecated 100.2.0 in favour of UI component implementation
  13. * @SuppressWarnings(PHPMD.NumberOfChildren)
  14. * @since 100.0.2
  15. */
  16. class Form extends \Magento\Backend\Block\Widget
  17. {
  18. /**
  19. * Form Object
  20. *
  21. * @var \Magento\Framework\Data\Form
  22. */
  23. protected $_form;
  24. /**
  25. * @var string
  26. */
  27. protected $_template = 'Magento_Backend::widget/form.phtml';
  28. /** @var Form\Element\ElementCreator */
  29. private $creator;
  30. /**
  31. * Constructs form
  32. *
  33. * @param \Magento\Backend\Block\Template\Context $context
  34. * @param array $data
  35. * @param Form\Element\ElementCreator|null $creator
  36. */
  37. public function __construct(
  38. \Magento\Backend\Block\Template\Context $context,
  39. array $data = [],
  40. Form\Element\ElementCreator $creator = null
  41. ) {
  42. parent::__construct($context, $data);
  43. $this->creator = $creator ?: ObjectManager::getInstance()->get(Form\Element\ElementCreator::class);
  44. }
  45. /**
  46. * Class constructor
  47. *
  48. * @return void
  49. */
  50. protected function _construct()
  51. {
  52. parent::_construct();
  53. $this->setDestElementId('edit_form');
  54. }
  55. /**
  56. * Preparing global layout
  57. *
  58. * You can redefine this method in child classes for changing layout
  59. *
  60. * @return $this
  61. */
  62. protected function _prepareLayout()
  63. {
  64. \Magento\Framework\Data\Form::setElementRenderer(
  65. $this->getLayout()->createBlock(
  66. \Magento\Backend\Block\Widget\Form\Renderer\Element::class,
  67. $this->getNameInLayout() . '_element'
  68. )
  69. );
  70. \Magento\Framework\Data\Form::setFieldsetRenderer(
  71. $this->getLayout()->createBlock(
  72. \Magento\Backend\Block\Widget\Form\Renderer\Fieldset::class,
  73. $this->getNameInLayout() . '_fieldset'
  74. )
  75. );
  76. \Magento\Framework\Data\Form::setFieldsetElementRenderer(
  77. $this->getLayout()->createBlock(
  78. \Magento\Backend\Block\Widget\Form\Renderer\Fieldset\Element::class,
  79. $this->getNameInLayout() . '_fieldset_element'
  80. )
  81. );
  82. return parent::_prepareLayout();
  83. }
  84. /**
  85. * Get form object
  86. *
  87. * @return \Magento\Framework\Data\Form
  88. */
  89. public function getForm()
  90. {
  91. return $this->_form;
  92. }
  93. /**
  94. * Get form HTML
  95. *
  96. * @return string
  97. */
  98. public function getFormHtml()
  99. {
  100. if (is_object($this->getForm())) {
  101. return $this->getForm()->getHtml();
  102. }
  103. return '';
  104. }
  105. /**
  106. * Set form object
  107. *
  108. * @param \Magento\Framework\Data\Form $form
  109. * @return $this
  110. */
  111. public function setForm(\Magento\Framework\Data\Form $form)
  112. {
  113. $this->_form = $form;
  114. $this->_form->setParent($this);
  115. $this->_form->setBaseUrl($this->_urlBuilder->getBaseUrl());
  116. $customAttributes = $this->getData('custom_attributes');
  117. if (is_array($customAttributes)) {
  118. foreach ($customAttributes as $key => $value) {
  119. $this->_form->addCustomAttribute($key, $value);
  120. }
  121. }
  122. return $this;
  123. }
  124. /**
  125. * Prepare form before rendering HTML
  126. *
  127. * @return $this
  128. */
  129. protected function _prepareForm()
  130. {
  131. return $this;
  132. }
  133. /**
  134. * This method is called before rendering HTML
  135. *
  136. * @return $this
  137. */
  138. protected function _beforeToHtml()
  139. {
  140. $this->_prepareForm();
  141. $this->_initFormValues();
  142. return parent::_beforeToHtml();
  143. }
  144. /**
  145. * Initialize form fields values
  146. *
  147. * Method will be called after prepareForm and can be used for field values initialization
  148. *
  149. * @return $this
  150. */
  151. protected function _initFormValues()
  152. {
  153. return $this;
  154. }
  155. /**
  156. * Set Fieldset to Form
  157. *
  158. * @param array $attributes attributes that are to be added
  159. * @param \Magento\Framework\Data\Form\Element\Fieldset $fieldset
  160. * @param array $exclude attributes that should be skipped
  161. * @return void
  162. */
  163. protected function _setFieldset($attributes, $fieldset, $exclude = [])
  164. {
  165. $this->_addElementTypes($fieldset);
  166. foreach ($attributes as $attribute) {
  167. /* @var $attribute \Magento\Eav\Model\Entity\Attribute */
  168. if (!$this->_isAttributeVisible($attribute)) {
  169. continue;
  170. }
  171. if (($inputType = $attribute->getFrontend()->getInputType())
  172. && !in_array($attribute->getAttributeCode(), $exclude)
  173. && ('media_image' !== $inputType || $attribute->getAttributeCode() == 'image')
  174. ) {
  175. $element = $this->creator->create($fieldset, $attribute);
  176. $element->setAfterElementHtml($this->_getAdditionalElementHtml($element));
  177. $this->_applyTypeSpecificConfig($inputType, $element, $attribute);
  178. }
  179. }
  180. }
  181. /**
  182. * Check whether attribute is visible
  183. *
  184. * @param \Magento\Eav\Model\Entity\Attribute $attribute
  185. * @return bool
  186. */
  187. protected function _isAttributeVisible(\Magento\Eav\Model\Entity\Attribute $attribute)
  188. {
  189. return !(!$attribute || $attribute->hasIsVisible() && !$attribute->getIsVisible());
  190. }
  191. /**
  192. * Apply configuration specific for different element type
  193. *
  194. * @param string $inputType
  195. * @param \Magento\Framework\Data\Form\Element\AbstractElement $element
  196. * @param \Magento\Eav\Model\Entity\Attribute $attribute
  197. * @return void
  198. */
  199. protected function _applyTypeSpecificConfig($inputType, $element, \Magento\Eav\Model\Entity\Attribute $attribute)
  200. {
  201. switch ($inputType) {
  202. case 'select':
  203. $element->setValues($attribute->getSource()->getAllOptions(true, true));
  204. break;
  205. case 'multiselect':
  206. $element->setValues($attribute->getSource()->getAllOptions(false, true));
  207. $element->setCanBeEmpty(true);
  208. break;
  209. case 'date':
  210. $element->setDateFormat($this->_localeDate->getDateFormatWithLongYear());
  211. break;
  212. case 'multiline':
  213. $element->setLineCount($attribute->getMultilineCount());
  214. break;
  215. default:
  216. break;
  217. }
  218. }
  219. /**
  220. * Add new element type
  221. *
  222. * @param \Magento\Framework\Data\Form\AbstractForm $baseElement
  223. * @return void
  224. */
  225. protected function _addElementTypes(\Magento\Framework\Data\Form\AbstractForm $baseElement)
  226. {
  227. $types = $this->_getAdditionalElementTypes();
  228. foreach ($types as $code => $className) {
  229. $baseElement->addType($code, $className);
  230. }
  231. }
  232. /**
  233. * Retrieve predefined additional element types
  234. *
  235. * @return array
  236. */
  237. protected function _getAdditionalElementTypes()
  238. {
  239. return [];
  240. }
  241. /**
  242. * Render additional element
  243. *
  244. * @param \Magento\Framework\Data\Form\Element\AbstractElement $element
  245. * @return string
  246. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  247. */
  248. protected function _getAdditionalElementHtml($element)
  249. {
  250. return '';
  251. }
  252. }