33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+ declare (strict_types=1 );
67
7- /**
8- * Product form weight field helper
9- */
108namespace Magento \Catalog \Block \Adminhtml \Product \Helper \Form ;
119
10+ use Magento \Catalog \Api \Data \ProductAttributeInterface ;
11+ use Magento \Directory \Helper \Data ;
1212use Magento \Framework \Data \Form ;
1313use 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 ;
1420
15- class Weight extends \Magento \Framework \Data \Form \Element \Text
21+ /**
22+ * Product form weight field helper
23+ */
24+ class Weight extends Text
1625{
1726 /**
1827 * Weight switcher radio-button element
1928 *
20- * @var \Magento\Framework\Data\Form\Element\Checkbox
29+ * @var Radios
2130 */
2231 protected $ weightSwitcher ;
2332
2433 /**
25- * @var \Magento\Framework\Locale\ Format
34+ * @var Format
2635 */
2736 protected $ localeFormat ;
2837
2938 /**
30- * @var \Magento\Directory\Helper\ Data
39+ * @var Data
3140 */
3241 protected $ directoryHelper ;
3342
3443 /**
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
4049 * @param array $data
4150 */
4251 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 ,
4857 array $ data = []
4958 ) {
5059 $ this ->directoryHelper = $ directoryHelper ;
5160 $ this ->localeFormat = $ localeFormat ;
5261 $ this ->weightSwitcher = $ factoryElement ->create ('radios ' );
5362 $ this ->weightSwitcher ->setValue (
54- WeightResolver::HAS_WEIGHT
63+ WeightResolver::HAS_NO_WEIGHT
5564 )->setValues (
5665 [
5766 ['value ' => WeightResolver::HAS_WEIGHT , 'label ' => __ ('Yes ' )],
@@ -75,28 +84,48 @@ public function __construct(
7584 */
7685 public function getElementHtml ()
7786 {
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 );
8089 }
90+
8191 if ($ this ->getDisabled ()) {
8292 $ this ->weightSwitcher ->setDisabled ($ this ->getDisabled ());
8393 }
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 ;
100129 }
101130
102131 /**
@@ -112,8 +141,7 @@ public function setForm($form)
112141 }
113142
114143 /**
115- * @param null|int|string $index
116- * @return null|string
144+ * @inheritDoc
117145 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
118146 */
119147 public function getEscapedValue ($ index = null )
@@ -134,4 +162,53 @@ public function getEscapedValue($index = null)
134162
135163 return $ value ;
136164 }
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+ }
137214}
0 commit comments