AbstractMain.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Product attribute add/edit form main tab
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Eav\Block\Adminhtml\Attribute\Edit\Main;
  12. use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
  13. abstract class AbstractMain extends \Magento\Backend\Block\Widget\Form\Generic
  14. {
  15. /**
  16. * Attribute instance
  17. *
  18. * @var Attribute
  19. */
  20. protected $_attribute = null;
  21. /**
  22. * Eav data
  23. *
  24. * @var \Magento\Eav\Helper\Data
  25. */
  26. protected $_eavData = null;
  27. /**
  28. * @var \Magento\Eav\Block\Adminhtml\Attribute\PropertyLocker
  29. */
  30. protected $propertyLocker;
  31. /**
  32. * @var \Magento\Config\Model\Config\Source\YesnoFactory
  33. */
  34. protected $_yesnoFactory;
  35. /**
  36. * @var \Magento\Eav\Model\Adminhtml\System\Config\Source\InputtypeFactory
  37. */
  38. protected $_inputTypeFactory;
  39. /**
  40. * @param \Magento\Backend\Block\Template\Context $context
  41. * @param \Magento\Framework\Registry $registry
  42. * @param \Magento\Framework\Data\FormFactory $formFactory
  43. * @param \Magento\Eav\Helper\Data $eavData
  44. * @param \Magento\Config\Model\Config\Source\YesnoFactory $yesnoFactory
  45. * @param \Magento\Eav\Model\Adminhtml\System\Config\Source\InputtypeFactory $inputTypeFactory
  46. * @param \Magento\Eav\Block\Adminhtml\Attribute\PropertyLocker $propertyLocker
  47. * @param array $data
  48. * @codeCoverageIgnore
  49. */
  50. public function __construct(
  51. \Magento\Backend\Block\Template\Context $context,
  52. \Magento\Framework\Registry $registry,
  53. \Magento\Framework\Data\FormFactory $formFactory,
  54. \Magento\Eav\Helper\Data $eavData,
  55. \Magento\Config\Model\Config\Source\YesnoFactory $yesnoFactory,
  56. \Magento\Eav\Model\Adminhtml\System\Config\Source\InputtypeFactory $inputTypeFactory,
  57. \Magento\Eav\Block\Adminhtml\Attribute\PropertyLocker $propertyLocker,
  58. array $data = []
  59. ) {
  60. $this->_eavData = $eavData;
  61. $this->_yesnoFactory = $yesnoFactory;
  62. $this->_inputTypeFactory = $inputTypeFactory;
  63. $this->propertyLocker = $propertyLocker;
  64. parent::__construct($context, $registry, $formFactory, $data);
  65. }
  66. /**
  67. * Set attribute object
  68. *
  69. * @param Attribute $attribute
  70. * @return $this
  71. * @codeCoverageIgnore
  72. */
  73. public function setAttributeObject($attribute)
  74. {
  75. $this->_attribute = $attribute;
  76. return $this;
  77. }
  78. /**
  79. * Return attribute object
  80. *
  81. * @return Attribute
  82. */
  83. public function getAttributeObject()
  84. {
  85. if (null === $this->_attribute) {
  86. return $this->_coreRegistry->registry('entity_attribute');
  87. }
  88. return $this->_attribute;
  89. }
  90. /**
  91. * Preparing default form elements for editing attribute
  92. *
  93. * @return $this
  94. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  95. */
  96. protected function _prepareForm()
  97. {
  98. $attributeObject = $this->getAttributeObject();
  99. /** @var \Magento\Framework\Data\Form $form */
  100. $form = $this->_formFactory->create(
  101. ['data' => ['id' => 'edit_form', 'action' => $this->getData('action'), 'method' => 'post']]
  102. );
  103. $fieldset = $form->addFieldset('base_fieldset', ['legend' => __('Attribute Properties')]);
  104. if ($attributeObject->getAttributeId()) {
  105. $fieldset->addField('attribute_id', 'hidden', ['name' => 'attribute_id']);
  106. }
  107. $this->_addElementTypes($fieldset);
  108. $yesno = $this->_yesnoFactory->create()->toOptionArray();
  109. $labels = $attributeObject->getFrontendLabel();
  110. $fieldset->addField(
  111. 'attribute_label',
  112. 'text',
  113. [
  114. 'name' => 'frontend_label[0]',
  115. 'label' => __('Default Label'),
  116. 'title' => __('Default label'),
  117. 'required' => true,
  118. 'value' => is_array($labels) ? $labels[0] : $labels
  119. ]
  120. );
  121. $validateClass = sprintf(
  122. 'validate-code validate-length maximum-length-%d',
  123. \Magento\Eav\Model\Entity\Attribute::ATTRIBUTE_CODE_MAX_LENGTH
  124. );
  125. $fieldset->addField(
  126. 'attribute_code',
  127. 'text',
  128. [
  129. 'name' => 'attribute_code',
  130. 'label' => __('Attribute Code'),
  131. 'title' => __('Attribute Code'),
  132. 'note' => __(
  133. 'This is used internally. Make sure you don\'t use spaces or more than %1 symbols.',
  134. \Magento\Eav\Model\Entity\Attribute::ATTRIBUTE_CODE_MAX_LENGTH
  135. ),
  136. 'class' => $validateClass,
  137. 'required' => true
  138. ]
  139. );
  140. $fieldset->addField(
  141. 'frontend_input',
  142. 'select',
  143. [
  144. 'name' => 'frontend_input',
  145. 'label' => __('Catalog Input Type for Store Owner'),
  146. 'title' => __('Catalog Input Type for Store Owner'),
  147. 'value' => 'text',
  148. 'values' => $this->_inputTypeFactory->create()->toOptionArray()
  149. ]
  150. );
  151. $fieldset->addField(
  152. 'is_required',
  153. 'select',
  154. [
  155. 'name' => 'is_required',
  156. 'label' => __('Values Required'),
  157. 'title' => __('Values Required'),
  158. 'values' => $yesno
  159. ]
  160. );
  161. $fieldset->addField(
  162. 'default_value_text',
  163. 'text',
  164. [
  165. 'name' => 'default_value_text',
  166. 'label' => __('Default Value'),
  167. 'title' => __('Default Value'),
  168. 'value' => $attributeObject->getDefaultValue()
  169. ]
  170. );
  171. $fieldset->addField(
  172. 'default_value_yesno',
  173. 'select',
  174. [
  175. 'name' => 'default_value_yesno',
  176. 'label' => __('Default Value'),
  177. 'title' => __('Default Value'),
  178. 'values' => $yesno,
  179. 'value' => $attributeObject->getDefaultValue()
  180. ]
  181. );
  182. $dateFormat = $this->_localeDate->getDateFormat(\IntlDateFormatter::SHORT);
  183. $fieldset->addField(
  184. 'default_value_date',
  185. 'date',
  186. [
  187. 'name' => 'default_value_date',
  188. 'label' => __('Default Value'),
  189. 'title' => __('Default Value'),
  190. 'value' => $attributeObject->getDefaultValue(),
  191. 'date_format' => $dateFormat
  192. ]
  193. );
  194. $fieldset->addField(
  195. 'default_value_textarea',
  196. 'textarea',
  197. [
  198. 'name' => 'default_value_textarea',
  199. 'label' => __('Default Value'),
  200. 'title' => __('Default Value'),
  201. 'value' => $attributeObject->getDefaultValue()
  202. ]
  203. );
  204. $fieldset->addField(
  205. 'is_unique',
  206. 'select',
  207. [
  208. 'name' => 'is_unique',
  209. 'label' => __('Unique Value'),
  210. 'title' => __('Unique Value (not shared with other products)'),
  211. 'note' => __('Not shared with other products.'),
  212. 'values' => $yesno
  213. ]
  214. );
  215. $fieldset->addField(
  216. 'frontend_class',
  217. 'select',
  218. [
  219. 'name' => 'frontend_class',
  220. 'label' => __('Input Validation for Store Owner'),
  221. 'title' => __('Input Validation for Store Owner'),
  222. 'values' => $this->_eavData->getFrontendClasses($attributeObject->getEntityType()->getEntityTypeCode())
  223. ]
  224. );
  225. if ($attributeObject->getId()) {
  226. $form->getElement('attribute_code')->setDisabled(1);
  227. $form->getElement('frontend_input')->setDisabled(1);
  228. if (!$attributeObject->getIsUserDefined()) {
  229. $form->getElement('is_unique')->setDisabled(1);
  230. }
  231. }
  232. $this->propertyLocker->lock($form);
  233. $this->setForm($form);
  234. return parent::_prepareForm();
  235. }
  236. /**
  237. * Initialize form fields values
  238. *
  239. * @return $this
  240. */
  241. protected function _initFormValues()
  242. {
  243. $this->_eventManager->dispatch(
  244. 'adminhtml_block_eav_attribute_edit_form_init',
  245. ['form' => $this->getForm()]
  246. );
  247. $this->getForm()->addValues($this->getAttributeObject()->getData());
  248. return parent::_initFormValues();
  249. }
  250. /**
  251. * Processing block html after rendering
  252. * Adding js block to the end of this block
  253. *
  254. * @param string $html
  255. * @return string
  256. */
  257. protected function _afterToHtml($html)
  258. {
  259. $jsScripts = $this->getLayout()->createBlock(\Magento\Eav\Block\Adminhtml\Attribute\Edit\Js::class)
  260. ->toHtml();
  261. return $html . $jsScripts;
  262. }
  263. }