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