Skip to content

Commit c3ed73d

Browse files
committed
MAGETWO-58067: [GitHub] Data Loss for Country Multiselect in Backend on Website Level
1 parent e29ea9c commit c3ed73d

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

app/code/Magento/Backend/view/adminhtml/templates/system/shipping/applicable_country.phtml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,14 @@ CountryModel.prototype = {
6161
if (applyCountryElement && applyCountryElement.id) {
6262
var specifCountryElement = $(applyCountryElement.id.replace(/sallowspecific/, 'specificcountry'));
6363
var showMethodElement = $(applyCountryElement.id.replace(/sallowspecific/, 'showmethod'));
64+
// 'Use Default' checkbox of the related county list UI element
65+
var useDefaultElement = document.getElementById(specifCountryElement.id + '_inherit');
66+
6467
//var specifErrMsgElement = $(applyCountryElement.id.replace(/sallowspecific/, 'specificerrmsg'));
6568
if (specifCountryElement) {
6669
if (applyCountryElement.value == 1) {
67-
//if specific country element selected
68-
specifCountryElement.enable();
70+
// enable related country select only if its 'Use Default' checkbox is absent or is unchecked
71+
specifCountryElement.disabled = (useDefaultElement) ? useDefaultElement.checked : false;
6972
if (showMethodElement) {
7073
this.showElement(showMethodElement.up(1));
7174
}

app/code/Magento/Config/Block/System/Config/Form/Field/Select/Allowspecific.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,32 @@ class Allowspecific extends \Magento\Framework\Data\Form\Element\Select
2222
*/
2323
public function getAfterElementHtml()
2424
{
25-
$javaScript = "\n <script type=\"text/javascript\">require(['prototype'], function(){\n Event.observe('{$this->getHtmlId()}', 'change', function(){\n specific=\$('{$this
26-
->getHtmlId()}').value;\n \$('{$this
27-
->_getSpecificCountryElementId()}').disabled = (!specific || specific!=1);\n });\n });</script>";
28-
return $javaScript . parent::getAfterElementHtml();
25+
$elementId = $this->getHtmlId();
26+
$countryListId = $this->_getSpecificCountryElementId();
27+
$useDefaultElementId = $countryListId . '_inherit';
28+
29+
$elementJavaScript = <<<HTML
30+
<script type="text/javascript">
31+
//<![CDATA[
32+
document.getElementById('{$elementId}').addEventListener('change', function(event) {
33+
var isCountrySpecific = event.target.value == 1,
34+
specificCountriesElement = document.getElementById('{$countryListId}'),
35+
// 'Use Default' checkbox of the related county list UI element
36+
useDefaultElement = document.getElementById('{$useDefaultElementId}');
37+
38+
if (isCountrySpecific) {
39+
// enable related country select only if its 'Use Default' checkbox is absent or is unchecked
40+
specificCountriesElement.disabled = (useDefaultElement) ? useDefaultElement.checked : false;
41+
} else {
42+
// disable related country select if all countries are used
43+
specificCountriesElement.disabled = true;
44+
}
45+
});
46+
//]]>
47+
</script>
48+
HTML;
49+
50+
return $elementJavaScript . parent::getAfterElementHtml();
2951
}
3052

3153
/**

app/code/Magento/Config/Test/Unit/Block/System/Config/Form/Field/Select/AllowspecificTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ protected function setUp()
3737
public function testGetAfterElementHtml()
3838
{
3939
$this->_formMock->expects(
40-
$this->exactly(2)
40+
$this->once()
4141
)->method(
4242
'getHtmlIdPrefix'
4343
)->will(
4444
$this->returnValue('test_prefix_')
4545
);
4646
$this->_formMock->expects(
47-
$this->exactly(2)
47+
$this->once()
4848
)->method(
4949
'getHtmlIdSuffix'
5050
)->will(
@@ -57,7 +57,7 @@ public function testGetAfterElementHtml()
5757

5858
$actual = $this->_object->getAfterElementHtml();
5959

60-
$this->assertStringEndsWith($afterHtmlCode, $actual);
60+
$this->assertStringEndsWith('</script>' . $afterHtmlCode, $actual);
6161
$this->assertStringStartsWith('<script type="text/javascript">', trim($actual));
6262
$this->assertContains('test_prefix_spec_element_test_suffix', $actual);
6363
}

0 commit comments

Comments
 (0)