3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
6
7
7
- /**
8
- * Product form weight field helper
9
- */
10
8
namespace Magento \Catalog \Block \Adminhtml \Product \Helper \Form ;
11
9
10
+ use Magento \Catalog \Api \Data \ProductAttributeInterface ;
11
+ use Magento \Directory \Helper \Data ;
12
12
use Magento \Framework \Data \Form ;
13
13
use Magento \Catalog \Model \Product \Edit \WeightResolver ;
14
+ use Magento \Framework \Data \Form \Element \CollectionFactory ;
15
+ use Magento \Framework \Data \Form \Element \Factory ;
16
+ use Magento \Framework \Data \Form \Element \Radios ;
17
+ use Magento \Framework \Data \Form \Element \Text ;
18
+ use Magento \Framework \Escaper ;
19
+ use Magento \Framework \Locale \Format ;
14
20
15
- class Weight extends \Magento \Framework \Data \Form \Element \Text
21
+ /**
22
+ * Product form weight field helper
23
+ */
24
+ class Weight extends Text
16
25
{
17
26
/**
18
27
* Weight switcher radio-button element
19
28
*
20
- * @var \Magento\Framework\Data\Form\Element\Checkbox
29
+ * @var Radios
21
30
*/
22
31
protected $ weightSwitcher ;
23
32
24
33
/**
25
- * @var \Magento\Framework\Locale\ Format
34
+ * @var Format
26
35
*/
27
36
protected $ localeFormat ;
28
37
29
38
/**
30
- * @var \Magento\Directory\Helper\ Data
39
+ * @var Data
31
40
*/
32
41
protected $ directoryHelper ;
33
42
34
43
/**
35
- * @param \Magento\Framework\Data\Form\Element\ Factory $factoryElement
36
- * @param \Magento\Framework\Data\Form\Element\ CollectionFactory $factoryCollection
37
- * @param \Magento\Framework\ Escaper $escaper
38
- * @param \Magento\Framework\Locale\ Format $localeFormat
39
- * @param \Magento\Directory\Helper\ Data $directoryHelper
44
+ * @param Factory $factoryElement
45
+ * @param CollectionFactory $factoryCollection
46
+ * @param Escaper $escaper
47
+ * @param Format $localeFormat
48
+ * @param Data $directoryHelper
40
49
* @param array $data
41
50
*/
42
51
public function __construct (
43
- \ Magento \ Framework \ Data \ Form \ Element \ Factory $ factoryElement ,
44
- \ Magento \ Framework \ Data \ Form \ Element \ CollectionFactory $ factoryCollection ,
45
- \ Magento \ Framework \ Escaper $ escaper ,
46
- \ Magento \ Framework \ Locale \ Format $ localeFormat ,
47
- \ Magento \ Directory \ Helper \ Data $ directoryHelper ,
52
+ Factory $ factoryElement ,
53
+ CollectionFactory $ factoryCollection ,
54
+ Escaper $ escaper ,
55
+ Format $ localeFormat ,
56
+ Data $ directoryHelper ,
48
57
array $ data = []
49
58
) {
50
59
$ this ->directoryHelper = $ directoryHelper ;
51
60
$ this ->localeFormat = $ localeFormat ;
52
61
$ this ->weightSwitcher = $ factoryElement ->create ('radios ' );
53
62
$ this ->weightSwitcher ->setValue (
54
- WeightResolver::HAS_WEIGHT
63
+ WeightResolver::HAS_NO_WEIGHT
55
64
)->setValues (
56
65
[
57
66
['value ' => WeightResolver::HAS_WEIGHT , 'label ' => __ ('Yes ' )],
@@ -75,28 +84,48 @@ public function __construct(
75
84
*/
76
85
public function getElementHtml ()
77
86
{
78
- if (! $ this ->getForm ()->getDataObject ()->getTypeInstance ()->hasWeight ()) {
79
- $ this ->weightSwitcher ->setValue (WeightResolver::HAS_NO_WEIGHT );
87
+ if ($ this ->getForm ()->getDataObject ()->getTypeInstance ()->hasWeight ()) {
88
+ $ this ->weightSwitcher ->setValue (WeightResolver::HAS_WEIGHT );
80
89
}
90
+
81
91
if ($ this ->getDisabled ()) {
82
92
$ this ->weightSwitcher ->setDisabled ($ this ->getDisabled ());
83
93
}
84
- return '<div class="admin__field-control weight-switcher"> ' .
85
- '<div class="admin__control-switcher" data-role="weight-switcher"> ' .
86
- $ this ->weightSwitcher ->getLabelHtml () .
87
- '<div class="admin__field-control-group"> ' .
88
- $ this ->weightSwitcher ->getElementHtml () .
89
- '</div> ' .
90
- '</div> ' .
91
- '<div class="admin__control-addon"> ' .
92
- parent ::getElementHtml () .
93
- '<label class="admin__addon-suffix" for=" ' .
94
- $ this ->getHtmlId () .
95
- '"><span> ' .
96
- $ this ->directoryHelper ->getWeightUnit () .
97
- '</span></label> ' .
98
- '</div> ' .
99
- '</div> ' ;
94
+
95
+ $ htmlId = $ this ->getHtmlId ();
96
+ $ html = '' ;
97
+
98
+ if ($ beforeElementHtml = $ this ->getBeforeElementHtml ()) {
99
+ $ html .= '<label class="addbefore" for=" ' . $ htmlId . '"> ' . $ beforeElementHtml . '</label> ' ;
100
+ }
101
+
102
+ $ html .= '<div class="admin__control-addon"> ' ;
103
+
104
+ if (is_array ($ this ->getValue ())) {
105
+ foreach ($ this ->getValue () as $ value ) {
106
+ $ html .= $ this ->getHtmlForInputByValue ($ this ->_escape ($ value ));
107
+ }
108
+ } else {
109
+ $ html .= $ this ->getHtmlForInputByValue ($ this ->getEscapedValue ());
110
+ }
111
+
112
+ $ html .= '<label class="admin__addon-suffix" for=" ' .
113
+ $ this ->getHtmlId () .
114
+ '"><span> ' .
115
+ $ this ->directoryHelper ->getWeightUnit () .
116
+ '</span></label></div> ' ;
117
+
118
+ if ($ afterElementJs = $ this ->getAfterElementJs ()) {
119
+ $ html .= $ afterElementJs ;
120
+ }
121
+
122
+ if ($ afterElementHtml = $ this ->getAfterElementHtml ()) {
123
+ $ html .= '<label class="addafter" for=" ' . $ htmlId . '"> ' . $ afterElementHtml . '</label> ' ;
124
+ }
125
+
126
+ $ html .= $ this ->getHtmlForWeightSwitcher ();
127
+
128
+ return $ html ;
100
129
}
101
130
102
131
/**
@@ -112,8 +141,7 @@ public function setForm($form)
112
141
}
113
142
114
143
/**
115
- * @param null|int|string $index
116
- * @return null|string
144
+ * @inheritDoc
117
145
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
118
146
*/
119
147
public function getEscapedValue ($ index = null )
@@ -134,4 +162,53 @@ public function getEscapedValue($index = null)
134
162
135
163
return $ value ;
136
164
}
165
+
166
+ /**
167
+ * Get input html by sting value.
168
+ *
169
+ * @param string|null $value
170
+ *
171
+ * @return string
172
+ */
173
+ private function getHtmlForInputByValue ($ value )
174
+ {
175
+ return '<input id=" ' . $ this ->getHtmlId () . '" name=" ' . $ this ->getName () . '" ' . $ this ->_getUiId ()
176
+ . ' value=" ' . $ value . '" ' . $ this ->serialize ($ this ->getHtmlAttributes ()) . '/> ' ;
177
+ }
178
+
179
+ /**
180
+ * Get weight switcher html.
181
+ *
182
+ * @return string
183
+ */
184
+ private function getHtmlForWeightSwitcher ()
185
+ {
186
+ $ html = '<div class="admin__control-addon"> ' ;
187
+ $ html .= '<div class="admin__field-control weight-switcher"> ' .
188
+ '<div class="admin__control-switcher" data-role="weight-switcher"> ' .
189
+ $ this ->weightSwitcher ->getLabelHtml () .
190
+ '<div class="admin__field-control-group"> ' .
191
+ $ this ->weightSwitcher ->getElementHtml () .
192
+ '</div> ' .
193
+ '</div> ' ;
194
+
195
+ $ html .= '<label class="addafter"> ' ;
196
+ $ elementId = ProductAttributeInterface::CODE_HAS_WEIGHT ;
197
+ $ nameAttributeHtml = 'name=" ' . $ elementId . '_checkbox" ' ;
198
+ $ dataCheckboxName = "toggle_ {$ elementId }" ;
199
+ $ checkboxLabel = __ ('Change ' );
200
+ $ html .= <<<HTML
201
+ <span class="attribute-change-checkbox">
202
+ <input type="checkbox" id=" $ dataCheckboxName" name=" $ dataCheckboxName" class="checkbox" $ nameAttributeHtml
203
+ onclick="toogleFieldEditMode(this, 'weight-switcher1'); toogleFieldEditMode(this, 'weight-switcher0');" />
204
+ <label class="label" for=" $ dataCheckboxName">
205
+ {$ checkboxLabel }
206
+ </label>
207
+ </span>
208
+ HTML ;
209
+
210
+ $ html .= '</label></div></div> ' ;
211
+
212
+ return $ html ;
213
+ }
137
214
}
0 commit comments