Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Advanced.php
Go to the documentation of this file.
1 <?php
13 
19 
24 class Advanced extends Generic
25 {
31  protected $_eavData = null;
32 
36  protected $_yesNo;
37 
42 
46  private $propertyLocker;
47 
57  public function __construct(
58  \Magento\Backend\Block\Template\Context $context,
59  \Magento\Framework\Registry $registry,
60  \Magento\Framework\Data\FormFactory $formFactory,
61  Yesno $yesNo,
62  Data $eavData,
63  array $disableScopeChangeList = ['sku'],
64  array $data = []
65  ) {
66  $this->_yesNo = $yesNo;
67  $this->_eavData = $eavData;
68  $this->disableScopeChangeList = $disableScopeChangeList;
69  parent::__construct($context, $registry, $formFactory, $data);
70  }
71 
78  protected function _prepareForm()
79  {
80  $attributeObject = $this->getAttributeObject();
81 
82  $form = $this->_formFactory->create(
83  ['data' => ['id' => 'edit_form', 'action' => $this->getData('action'), 'method' => 'post']]
84  );
85 
86  $fieldset = $form->addFieldset(
87  'advanced_fieldset',
88  ['legend' => __('Advanced Attribute Properties'), 'collapsable' => true]
89  );
90 
91  $yesno = $this->_yesNo->toOptionArray();
92 
93  $validateClass = sprintf(
94  'validate-code validate-length maximum-length-%d',
95  \Magento\Eav\Model\Entity\Attribute::ATTRIBUTE_CODE_MAX_LENGTH
96  );
97  $fieldset->addField(
98  'attribute_code',
99  'text',
100  [
101  'name' => 'attribute_code',
102  'label' => __('Attribute Code'),
103  'title' => __('Attribute Code'),
104  'note' => __(
105  'This is used internally. Make sure you don\'t use spaces or more than %1 symbols.',
106  \Magento\Eav\Model\Entity\Attribute::ATTRIBUTE_CODE_MAX_LENGTH
107  ),
108  'class' => $validateClass
109  ]
110  );
111 
112  $fieldset->addField(
113  'default_value_text',
114  'text',
115  [
116  'name' => 'default_value_text',
117  'label' => __('Default Value'),
118  'title' => __('Default Value'),
119  'value' => $attributeObject->getDefaultValue()
120  ]
121  );
122 
123  $fieldset->addField(
124  'default_value_yesno',
125  'select',
126  [
127  'name' => 'default_value_yesno',
128  'label' => __('Default Value'),
129  'title' => __('Default Value'),
130  'values' => $yesno,
131  'value' => $attributeObject->getDefaultValue()
132  ]
133  );
134 
135  $dateFormat = $this->_localeDate->getDateFormat(\IntlDateFormatter::SHORT);
136  $fieldset->addField(
137  'default_value_date',
138  'date',
139  [
140  'name' => 'default_value_date',
141  'label' => __('Default Value'),
142  'title' => __('Default Value'),
143  'value' => $attributeObject->getDefaultValue(),
144  'date_format' => $dateFormat
145  ]
146  );
147 
148  $fieldset->addField(
149  'default_value_textarea',
150  'textarea',
151  [
152  'name' => 'default_value_textarea',
153  'label' => __('Default Value'),
154  'title' => __('Default Value'),
155  'value' => $attributeObject->getDefaultValue()
156  ]
157  );
158 
159  $fieldset->addField(
160  'is_unique',
161  'select',
162  [
163  'name' => 'is_unique',
164  'label' => __('Unique Value'),
165  'title' => __('Unique Value (not shared with other products)'),
166  'note' => __('Not shared with other products.'),
167  'values' => $yesno
168  ]
169  );
170 
171  $fieldset->addField(
172  'frontend_class',
173  'select',
174  [
175  'name' => 'frontend_class',
176  'label' => __('Input Validation for Store Owner'),
177  'title' => __('Input Validation for Store Owner'),
178  'values' => $this->_eavData->getFrontendClasses($attributeObject->getEntityType()->getEntityTypeCode())
179  ]
180  );
181 
182  $fieldset->addField(
183  'is_used_in_grid',
184  'select',
185  [
186  'name' => 'is_used_in_grid',
187  'label' => __('Add to Column Options'),
188  'title' => __('Add to Column Options'),
189  'values' => $yesno,
190  'value' => $attributeObject->getData('is_used_in_grid') ?: 1,
191  'note' => __('Select "Yes" to add this attribute to the list of column options in the product grid.'),
192  ]
193  );
194 
195  $fieldset->addField(
196  'is_visible_in_grid',
197  'hidden',
198  [
199  'name' => 'is_visible_in_grid',
200  'value' => $attributeObject->getData('is_visible_in_grid') ?: 1,
201  ]
202  );
203 
204  $fieldset->addField(
205  'is_filterable_in_grid',
206  'select',
207  [
208  'name' => 'is_filterable_in_grid',
209  'label' => __('Use in Filter Options'),
210  'title' => __('Use in Filter Options'),
211  'values' => $yesno,
212  'value' => $attributeObject->getData('is_filterable_in_grid') ?: 1,
213  'note' => __('Select "Yes" to add this attribute to the list of filter options in the product grid.'),
214  ]
215  );
216 
217  if ($attributeObject->getId()) {
218  $form->getElement('attribute_code')->setDisabled(1);
219  if (!$attributeObject->getIsUserDefined()) {
220  $form->getElement('is_unique')->setDisabled(1);
221  }
222  }
223 
224  $scopes = [
225  \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE => __('Store View'),
226  \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_WEBSITE => __('Website'),
227  \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL => __('Global'),
228  ];
229 
230  if ($attributeObject->getAttributeCode() == 'status' || $attributeObject->getAttributeCode() == 'tax_class_id'
231  ) {
232  unset($scopes[\Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE]);
233  }
234 
235  $fieldset->addField(
236  'is_global',
237  'select',
238  [
239  'name' => 'is_global',
240  'label' => __('Scope'),
241  'title' => __('Scope'),
242  'note' => __('Declare attribute value saving scope.'),
243  'values' => $scopes
244  ],
245  'attribute_code'
246  );
247 
248  $this->_eventManager->dispatch('product_attribute_form_build', ['form' => $form]);
249  if (in_array($attributeObject->getAttributeCode(), $this->disableScopeChangeList)) {
250  $form->getElement('is_global')->setDisabled(1);
251  }
252  $this->setForm($form);
253  $this->getPropertyLocker()->lock($form);
254  return $this;
255  }
256 
262  protected function _initFormValues()
263  {
264  $this->getForm()->addValues($this->getAttributeObject()->getData());
265  return parent::_initFormValues();
266  }
267 
273  private function getAttributeObject()
274  {
275  return $this->_coreRegistry->registry('entity_attribute');
276  }
277 
283  private function getPropertyLocker()
284  {
285  if (null === $this->propertyLocker) {
286  $this->propertyLocker = ObjectManager::getInstance()->get(PropertyLocker::class);
287  }
288  return $this->propertyLocker;
289  }
290 }
getData($key='', $index=null)
Definition: DataObject.php:119
__construct(\Magento\Backend\Block\Template\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Data\FormFactory $formFactory, Yesno $yesNo, Data $eavData, array $disableScopeChangeList=['sku'], array $data=[])
Definition: Advanced.php:57
setForm(\Magento\Framework\Data\Form $form)
Definition: Form.php:112
__()
Definition: __.php:13