Skip to content

Commit 8829508

Browse files
committed
AC-12860: Weight unit issue fixed
1 parent 42f0d08 commit 8829508

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

app/code/Magento/Catalog/Block/Adminhtml/Product/Helper/Form/Weight.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function getElementHtml()
122122
$html .= '<label class="admin__addon-suffix" for="' .
123123
$this->getHtmlId() .
124124
'"><span>' .
125-
$this->directoryHelper->getWeightUnit() .
125+
$this->_escaper->escapeHtml($this->directoryHelper->getWeightUnit()) .
126126
'</span></label></div>';
127127

128128
if ($afterElementJs = $this->getAfterElementJs()) {
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Directory\Model\Config\Backend;
7+
8+
use LocalizedException;
9+
use Magento\Directory\Model\Config\Source\WeightUnit as Source;
10+
use Magento\Framework\App\Cache\TypeListInterface;
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
use Magento\Framework\App\Config\Value;
13+
use Magento\Framework\Data\Collection\AbstractDb;
14+
use Magento\Framework\Model\Context;
15+
use Magento\Framework\Model\ResourceModel\AbstractResource;
16+
use Magento\Framework\Registry;
17+
18+
/**
19+
* Backend source for weight unit configuration field
20+
*/
21+
class WeightUnit extends Value
22+
{
23+
/**
24+
* @var Source
25+
*/
26+
private $source;
27+
28+
/**
29+
* @param Source $source
30+
* @param Context $contesxt
31+
* @param Registry $registry
32+
* @param ScopeConfigInterface $config
33+
* @param TypeListInterface $cacheTypeList
34+
* @param CookieLifetimeValidator $configValidator
35+
* @param AbstractResource $resource
36+
* @param AbstractDb $resourceCollection
37+
* @param array $data
38+
*
39+
* @codeCoverageIgnore
40+
*/
41+
public function __construct(
42+
Source $source,
43+
Context $context,
44+
Registry $registry,
45+
ScopeConfigInterface $config,
46+
TypeListInterface $cacheTypeList,
47+
AbstractResource $resource = null,
48+
AbstractDb $resourceCollection = null,
49+
array $data = []
50+
) {
51+
$this->source = $source;
52+
parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
53+
}
54+
55+
/**
56+
* Check whether weight unit value is acceptable or not
57+
*
58+
* @return $this
59+
*/
60+
public function beforeSave()
61+
{
62+
if ($this->isValueChanged()) {
63+
$weightUnit = $this->getData('value');
64+
if (!in_array($weightUnit, $this->getOptions())) {
65+
throw new LocalizedException(__('There was an error save new configuration value.'));
66+
}
67+
}
68+
return parent::beforeSave();
69+
}
70+
71+
/**
72+
* Get available options for weight unit
73+
*
74+
* @return array
75+
*/
76+
private function getOptions()
77+
{
78+
$options = $this->source->toOptionArray();
79+
return array_column($options, 'value');
80+
}
81+
}

app/code/Magento/Directory/etc/adminhtml/system.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@
157157
<field id="weight_unit" translate="label" type="select" sortOrder="7" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
158158
<label>Weight Unit</label>
159159
<source_model>Magento\Directory\Model\Config\Source\WeightUnit</source_model>
160+
<backend_model>Magento\Directory\Model\Config\Backend\WeightUnit</backend_model>
160161
</field>
161162
</group>
162163
</section>

0 commit comments

Comments
 (0)