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,44 @@ public function __construct(
7584 */
7685 public function getElementHtml ()
7786 {
78- if (!$ this ->getForm ()->getDataObject ()->getTypeInstance ()->hasWeight ()) {
79- $ this ->weightSwitcher ->setValue (WeightResolver::HAS_NO_WEIGHT );
80- }
8187 if ($ this ->getDisabled ()) {
8288 $ this ->weightSwitcher ->setDisabled ($ this ->getDisabled ());
8389 }
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> ' ;
90+
91+ $ htmlId = $ this ->getHtmlId ();
92+ $ html = '' ;
93+
94+ if ($ beforeElementHtml = $ this ->getBeforeElementHtml ()) {
95+ $ html .= '<label class="addbefore" for=" ' . $ htmlId . '"> ' . $ beforeElementHtml . '</label> ' ;
96+ }
97+
98+ $ html .= '<div class="admin__control-addon"> ' ;
99+
100+ if (is_array ($ this ->getValue ())) {
101+ foreach ($ this ->getValue () as $ value ) {
102+ $ html .= $ this ->getHtmlForInputByValue ($ this ->_escape ($ value ));
103+ }
104+ } else {
105+ $ html .= $ this ->getHtmlForInputByValue ($ this ->getEscapedValue ());
106+ }
107+
108+ $ html .= '<label class="admin__addon-suffix" for=" ' .
109+ $ this ->getHtmlId () .
110+ '"><span> ' .
111+ $ this ->directoryHelper ->getWeightUnit () .
112+ '</span></label></div> ' ;
113+
114+ if ($ afterElementJs = $ this ->getAfterElementJs ()) {
115+ $ html .= $ afterElementJs ;
116+ }
117+
118+ if ($ afterElementHtml = $ this ->getAfterElementHtml ()) {
119+ $ html .= '<label class="addafter" for=" ' . $ htmlId . '"> ' . $ afterElementHtml . '</label> ' ;
120+ }
121+
122+ $ html .= $ this ->getHtmlForWeightSwitcher ();
123+
124+ return $ html ;
100125 }
101126
102127 /**
@@ -112,8 +137,7 @@ public function setForm($form)
112137 }
113138
114139 /**
115- * @param null|int|string $index
116- * @return null|string
140+ * @inheritDoc
117141 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
118142 */
119143 public function getEscapedValue ($ index = null )
@@ -134,4 +158,53 @@ public function getEscapedValue($index = null)
134158
135159 return $ value ;
136160 }
161+
162+ /**
163+ * Get input html by sting value.
164+ *
165+ * @param string|null $value
166+ *
167+ * @return string
168+ */
169+ private function getHtmlForInputByValue ($ value )
170+ {
171+ return '<input id=" ' . $ this ->getHtmlId () . '" name=" ' . $ this ->getName () . '" ' . $ this ->_getUiId ()
172+ . ' value=" ' . $ value . '" ' . $ this ->serialize ($ this ->getHtmlAttributes ()) . '/> ' ;
173+ }
174+
175+ /**
176+ * Get weight switcher html.
177+ *
178+ * @return string
179+ */
180+ private function getHtmlForWeightSwitcher ()
181+ {
182+ $ html = '<div class="admin__control-addon"> ' ;
183+ $ html .= '<div class="admin__field-control weight-switcher"> ' .
184+ '<div class="admin__control-switcher" data-role="weight-switcher"> ' .
185+ $ this ->weightSwitcher ->getLabelHtml () .
186+ '<div class="admin__field-control-group"> ' .
187+ $ this ->weightSwitcher ->getElementHtml () .
188+ '</div> ' .
189+ '</div> ' ;
190+
191+ $ html .= '<label class="addafter"> ' ;
192+ $ elementId = ProductAttributeInterface::CODE_HAS_WEIGHT ;
193+ $ nameAttributeHtml = 'name=" ' . $ elementId . '_checkbox" ' ;
194+ $ dataCheckboxName = "toggle_ {$ elementId }" ;
195+ $ checkboxLabel = __ ('Change ' );
196+ $ html .= <<<HTML
197+ <span class="attribute-change-checkbox">
198+ <input type="checkbox" id=" $ dataCheckboxName" name=" $ dataCheckboxName" class="checkbox" $ nameAttributeHtml
199+ onclick="toogleFieldEditMode(this, 'weight-switcher1'); toogleFieldEditMode(this, 'weight-switcher0');" />
200+ <label class="label" for=" $ dataCheckboxName">
201+ {$ checkboxLabel }
202+ </label>
203+ </span>
204+ HTML ;
205+
206+ $ html .= '</label></div></div> ' ;
207+
208+ return $ html ;
209+ }
137210}
0 commit comments