Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractMain.php
Go to the documentation of this file.
1 <?php
13 
15 
16 abstract class AbstractMain extends \Magento\Backend\Block\Widget\Form\Generic
17 {
23  protected $_attribute = null;
24 
30  protected $_eavData = null;
31 
35  protected $propertyLocker;
36 
40  protected $_yesnoFactory;
41 
45  protected $_inputTypeFactory;
46 
58  public function __construct(
59  \Magento\Backend\Block\Template\Context $context,
60  \Magento\Framework\Registry $registry,
61  \Magento\Framework\Data\FormFactory $formFactory,
62  \Magento\Eav\Helper\Data $eavData,
63  \Magento\Config\Model\Config\Source\YesnoFactory $yesnoFactory,
64  \Magento\Eav\Model\Adminhtml\System\Config\Source\InputtypeFactory $inputTypeFactory,
65  \Magento\Eav\Block\Adminhtml\Attribute\PropertyLocker $propertyLocker,
66  array $data = []
67  ) {
68  $this->_eavData = $eavData;
69  $this->_yesnoFactory = $yesnoFactory;
70  $this->_inputTypeFactory = $inputTypeFactory;
71  $this->propertyLocker = $propertyLocker;
72  parent::__construct($context, $registry, $formFactory, $data);
73  }
74 
82  public function setAttributeObject($attribute)
83  {
84  $this->_attribute = $attribute;
85  return $this;
86  }
87 
93  public function getAttributeObject()
94  {
95  if (null === $this->_attribute) {
96  return $this->_coreRegistry->registry('entity_attribute');
97  }
98  return $this->_attribute;
99  }
100 
107  protected function _prepareForm()
108  {
109  $attributeObject = $this->getAttributeObject();
110 
112  $form = $this->_formFactory->create(
113 
114  ['data' => ['id' => 'edit_form', 'action' => $this->getData('action'), 'method' => 'post']]
115  );
116 
117  $fieldset = $form->addFieldset('base_fieldset', ['legend' => __('Attribute Properties')]);
118 
119  if ($attributeObject->getAttributeId()) {
120  $fieldset->addField('attribute_id', 'hidden', ['name' => 'attribute_id']);
121  }
122 
123  $this->_addElementTypes($fieldset);
124 
125  $yesno = $this->_yesnoFactory->create()->toOptionArray();
126 
127  $labels = $attributeObject->getFrontendLabel();
128  $fieldset->addField(
129  'attribute_label',
130  'text',
131  [
132  'name' => 'frontend_label[0]',
133  'label' => __('Default Label'),
134  'title' => __('Default label'),
135  'required' => true,
136  'value' => is_array($labels) ? $labels[0] : $labels
137  ]
138  );
139 
140  $validateClass = sprintf(
141  'validate-code validate-length maximum-length-%d',
143  );
144  $fieldset->addField(
145  'attribute_code',
146  'text',
147  [
148  'name' => 'attribute_code',
149  'label' => __('Attribute Code'),
150  'title' => __('Attribute Code'),
151  'note' => __(
152  'This is used internally. Make sure you don\'t use spaces or more than %1 symbols.',
154  ),
155  'class' => $validateClass,
156  'required' => true
157  ]
158  );
159 
160  $fieldset->addField(
161  'frontend_input',
162  'select',
163  [
164  'name' => 'frontend_input',
165  'label' => __('Catalog Input Type for Store Owner'),
166  'title' => __('Catalog Input Type for Store Owner'),
167  'value' => 'text',
168  'values' => $this->_inputTypeFactory->create()->toOptionArray()
169  ]
170  );
171 
172  $fieldset->addField(
173  'is_required',
174  'select',
175  [
176  'name' => 'is_required',
177  'label' => __('Values Required'),
178  'title' => __('Values Required'),
179  'values' => $yesno
180  ]
181  );
182 
183  $fieldset->addField(
184  'default_value_text',
185  'text',
186  [
187  'name' => 'default_value_text',
188  'label' => __('Default Value'),
189  'title' => __('Default Value'),
190  'value' => $attributeObject->getDefaultValue()
191  ]
192  );
193 
194  $fieldset->addField(
195  'default_value_yesno',
196  'select',
197  [
198  'name' => 'default_value_yesno',
199  'label' => __('Default Value'),
200  'title' => __('Default Value'),
201  'values' => $yesno,
202  'value' => $attributeObject->getDefaultValue()
203  ]
204  );
205 
206  $dateFormat = $this->_localeDate->getDateFormat(\IntlDateFormatter::SHORT);
207  $fieldset->addField(
208  'default_value_date',
209  'date',
210  [
211  'name' => 'default_value_date',
212  'label' => __('Default Value'),
213  'title' => __('Default Value'),
214  'value' => $attributeObject->getDefaultValue(),
215  'date_format' => $dateFormat
216  ]
217  );
218 
219  $fieldset->addField(
220  'default_value_textarea',
221  'textarea',
222  [
223  'name' => 'default_value_textarea',
224  'label' => __('Default Value'),
225  'title' => __('Default Value'),
226  'value' => $attributeObject->getDefaultValue()
227  ]
228  );
229 
230  $fieldset->addField(
231  'is_unique',
232  'select',
233  [
234  'name' => 'is_unique',
235  'label' => __('Unique Value'),
236  'title' => __('Unique Value (not shared with other products)'),
237  'note' => __('Not shared with other products.'),
238  'values' => $yesno
239  ]
240  );
241 
242  $fieldset->addField(
243  'frontend_class',
244  'select',
245  [
246  'name' => 'frontend_class',
247  'label' => __('Input Validation for Store Owner'),
248  'title' => __('Input Validation for Store Owner'),
249  'values' => $this->_eavData->getFrontendClasses($attributeObject->getEntityType()->getEntityTypeCode())
250  ]
251  );
252 
253  if ($attributeObject->getId()) {
254  $form->getElement('attribute_code')->setDisabled(1);
255  $form->getElement('frontend_input')->setDisabled(1);
256  if (!$attributeObject->getIsUserDefined()) {
257  $form->getElement('is_unique')->setDisabled(1);
258  }
259  }
260 
261  $this->propertyLocker->lock($form);
262  $this->setForm($form);
263 
264  return parent::_prepareForm();
265  }
266 
272  protected function _initFormValues()
273  {
274  $this->_eventManager->dispatch(
275  'adminhtml_block_eav_attribute_edit_form_init',
276  ['form' => $this->getForm()]
277  );
278  $this->getForm()->addValues($this->getAttributeObject()->getData());
279  return parent::_initFormValues();
280  }
281 
289  protected function _afterToHtml($html)
290  {
291  $jsScripts = $this->getLayout()->createBlock(\Magento\Eav\Block\Adminhtml\Attribute\Edit\Js::class)
292  ->toHtml();
293  return $html . $jsScripts;
294  }
295 }
_addElementTypes(\Magento\Framework\Data\Form\AbstractForm $baseElement)
Definition: Form.php:255
getData($key='', $index=null)
Definition: DataObject.php:119
setForm(\Magento\Framework\Data\Form $form)
Definition: Form.php:112
__()
Definition: __.php:13
__construct(\Magento\Backend\Block\Template\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Data\FormFactory $formFactory, \Magento\Eav\Helper\Data $eavData, \Magento\Config\Model\Config\Source\YesnoFactory $yesnoFactory, \Magento\Eav\Model\Adminhtml\System\Config\Source\InputtypeFactory $inputTypeFactory, \Magento\Eav\Block\Adminhtml\Attribute\PropertyLocker $propertyLocker, array $data=[])