From 12e4bd66bb847c2c0a6cd8f5179aad057bf1f327 Mon Sep 17 00:00:00 2001 From: Lex Koomen Date: Tue, 19 Mar 2019 13:34:55 +0100 Subject: [PATCH 1/2] SCPURDEY-523 +: fix for exception with nl2br function on pages with array config --- Helper/Data.php | 150 ++++++++++++++++++++++++++++++++---------------- composer.json | 2 +- 2 files changed, 100 insertions(+), 52 deletions(-) diff --git a/Helper/Data.php b/Helper/Data.php index 4814772..1451971 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -1,8 +1,9 @@ array()); + public function getScopeTree() + { + $tree = [self::WEBSITE_SCOPE_CODE => []]; $websites = $this->storeManager->getWebsites(); /* @var $website Website */ - foreach($websites as $website) { - $tree[self::WEBSITE_SCOPE_CODE][$website->getId()] = array(self::STORE_VIEW_SCOPE_CODE => array()); + foreach ($websites as $website) { + $tree[self::WEBSITE_SCOPE_CODE][$website->getId()] = [self::STORE_VIEW_SCOPE_CODE => []]; /* @var $store Store */ - foreach($website->getStores() as $store) { + foreach ($website->getStores() as $store) { $tree[self::WEBSITE_SCOPE_CODE][$website->getId()][self::STORE_VIEW_SCOPE_CODE][] = $store->getId(); } } @@ -89,7 +91,8 @@ public function getScopeTree() { * @param string|int $contextScopeId * @return string */ - protected function _getConfigValue($path, $contextScope, $contextScopeId) { + protected function _getConfigValue($path, $contextScope, $contextScopeId) + { return $this->context->getScopeConfig()->getValue($path, $contextScope, $contextScopeId); } @@ -101,25 +104,31 @@ protected function _getConfigValue($path, $contextScope, $contextScopeId) { * @param string|int $contextScopeId * @return array */ - public function getConfigDisplayValue($path, $contextScope, $contextScopeId) { + public function getConfigDisplayValue($path, $contextScope, $contextScopeId) + { $value = $this->_getConfigValue($path, $contextScope, $contextScopeId); - $labels = [$value]; //default labels to raw value + if (is_array($value)) { + $firstKey = array_keys($value)[0]; + $labels = [$value[$firstKey]]; + } else { + $labels = [$value]; //default labels to raw value + } /** @var \Magento\Config\Model\Config\Structure\Element\Field $field */ $field = $this->configStructure->getElement($path); - if($field->getOptions()) { + if ($field->getOptions()) { $labels = []; //reset labels so we can add human-friendly labels $optionsByValue = []; - foreach($field->getOptions() as $option) { + foreach ($field->getOptions() as $option) { $optionsByValue[$option['value']] = $option; } $values = explode(',', $value); - foreach($values as $valueInstance) { + foreach ($values as $valueInstance) { $labels[] = isset($optionsByValue[$valueInstance]) ? $optionsByValue[$valueInstance]['label'] : $valueInstance; @@ -139,49 +148,82 @@ public function getConfigDisplayValue($path, $contextScope, $contextScopeId) { * @param $contextScopeId * @return array */ - public function getOverriddenLevels($path, $contextScope, $contextScopeId) { + public function getOverriddenLevels($path, $contextScope, $contextScopeId) + { $tree = $this->getScopeTree(); $currentValue = $this->_getConfigValue($path, $contextScope, $contextScopeId); - $overridden = array(); + $overridden = []; - switch($contextScope) { + switch ($contextScope) { case self::WEBSITE_SCOPE_CODE: $stores = array_values($tree[self::WEBSITE_SCOPE_CODE][$contextScopeId][self::STORE_VIEW_SCOPE_CODE]); - foreach($stores as $storeId) { + foreach ($stores as $storeId) { $value = $this->_getConfigValue($path, self::STORE_VIEW_SCOPE_CODE, $storeId); - if($value != $currentValue) { - $overridden[] = array( - 'scope' => 'store', - 'scope_id' => $storeId, + if ($value != $currentValue) { + $overridden[] = [ + 'scope' => 'store', + 'scope_id' => $storeId, 'value' => $value, - 'display_value' => $this->getConfigDisplayValue($path, self::STORE_VIEW_SCOPE_CODE, $storeId) - ); + 'display_value' => $this->getConfigDisplayValue($path, self::STORE_VIEW_SCOPE_CODE, + $storeId) + ]; } } break; case 'default': - foreach($tree[self::WEBSITE_SCOPE_CODE] as $websiteId => $website) { + foreach ($tree[self::WEBSITE_SCOPE_CODE] as $websiteId => $website) { $websiteValue = $this->_getConfigValue($path, self::WEBSITE_SCOPE_CODE, $websiteId); - if($websiteValue != $currentValue) { - $overridden[] = array( - 'scope' => 'website', - 'scope_id' => $websiteId, + + if (is_array($currentValue) && is_array($websiteValue)) { + $firstKeyCurrent = array_keys($currentValue)[0]; + $firstKeyWebsite = array_keys($websiteValue)[0]; + + $websiteValueArray = $websiteValue[$firstKeyWebsite]; + $currentValueArray = $currentValue[$firstKeyCurrent]; + if ($websiteValueArray != $currentValueArray) { + $overridden[] = [ + 'scope' => 'website', + 'scope_id' => $websiteId, + 'value' => $websiteValueArray, + 'display_value' => $this->getConfigDisplayValue($path, self::WEBSITE_SCOPE_CODE, + $websiteId) + ]; + } + } elseif ($websiteValue != $currentValue) { + $overridden[] = [ + 'scope' => 'website', + 'scope_id' => $websiteId, 'value' => $websiteValue, 'display_value' => $this->getConfigDisplayValue($path, self::WEBSITE_SCOPE_CODE, $websiteId) - ); + ]; } - foreach($website[self::STORE_VIEW_SCOPE_CODE] as $storeId) { + foreach ($website[self::STORE_VIEW_SCOPE_CODE] as $storeId) { $value = $this->_getConfigValue($path, self::STORE_VIEW_SCOPE_CODE, $storeId); - if($value != $currentValue && $value != $websiteValue) { - $overridden[] = array( - 'scope' => 'store', - 'scope_id' => $storeId, + if (is_array($currentValue) && is_array($value)) { + $firstKeyCurrent = array_keys($currentValue)[0]; + $firstKeyStore = array_keys($value)[0]; + + $storeValueArray = $value[$firstKeyStore]; + if ($storeValueArray != $websiteValueArray) { + $overridden[] = [ + 'scope' => 'store', + 'scope_id' => $storeId, + 'value' => $storeValueArray, + 'display_value' => $this->getConfigDisplayValue($path, self::STORE_VIEW_SCOPE_CODE, + $storeId) + ]; + } + } elseif ($value != $currentValue && $value != $websiteValue) { + $overridden[] = [ + 'scope' => 'store', + 'scope_id' => $storeId, 'value' => $value, - 'display_value' => $this->getConfigDisplayValue($path, self::STORE_VIEW_SCOPE_CODE, $storeId) - ); + 'display_value' => $this->getConfigDisplayValue($path, self::STORE_VIEW_SCOPE_CODE, + $storeId) + ]; } } } @@ -197,9 +239,14 @@ public function getOverriddenLevels($path, $contextScope, $contextScopeId) { * @param array $labels * @return string */ - protected function getFormattedValueLabels(array $labels) { - if(count($labels) == 1) { + protected function getFormattedValueLabels(array $labels) + { + if (count($labels) == 1) { //if only one value, simply return it + //if value is an array return + if (is_array(($labels[0]))) { + return 'Check the corresponding config level to view the changes'; + } return '' . nl2br($this->escaper->escapeHtml($labels[0])) . ''; @@ -207,10 +254,10 @@ protected function getFormattedValueLabels(array $labels) { $formattedLabels = ''; - foreach($labels as $label) { + foreach ($labels as $label) { $formattedLabels .= '
  • ' . nl2br($this->escaper->escapeHtml($label)) . - '
  • '; + ''; } return ''; @@ -223,12 +270,13 @@ protected function getFormattedValueLabels(array $labels) { * @param array $overridden * @return string */ - public function formatOverriddenScopes($section, array $overridden) { + public function formatOverriddenScopes($section, array $overridden) + { $formatted = '
    ' . '

    ' . __('This config field is overridden at the following scope(s):') . '

    ' . '
    '; - foreach($overridden as $overriddenScope) { + foreach ($overridden as $overriddenScope) { $scope = $overriddenScope['scope']; $scopeId = $overriddenScope['scope_id']; $value = $overriddenScope['value']; @@ -236,14 +284,14 @@ public function formatOverriddenScopes($section, array $overridden) { $scopeLabel = $scopeId; $url = '#'; - switch($scope) { + switch ($scope) { case 'website': $url = $this->urlBuilder->getUrl( '*/*/*', - array( + [ 'section' => $section, 'website' => $scopeId - ) + ] ); $scopeLabel = __( 'Website %2', @@ -258,10 +306,10 @@ public function formatOverriddenScopes($section, array $overridden) { $website = $store->getWebsite(); $url = $this->urlBuilder->getUrl( '*/*/*', - array( - 'section' => $section, - 'store' => $store->getId() - ) + [ + 'section' => $section, + 'store' => $store->getId() + ] ); $scopeLabel = __( 'Store view %2', @@ -272,8 +320,8 @@ public function formatOverriddenScopes($section, array $overridden) { } $formatted .= - '
    ' - . $scopeLabel . + '
    ' + . $scopeLabel . '
    ' . '
    ' . $this->getFormattedValueLabels($valueLabel) . '
    '; } diff --git a/composer.json b/composer.json index bceb82a..77efc15 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ "magento/module-config": "*" }, "type": "magento2-module", - "version": "3.1.1", + "version": "3.1.2", "autoload": { "files": [ "registration.php" ], "psr-4": { From 0c66f2126bb1d5407fdc564c29f7cd279931413e Mon Sep 17 00:00:00 2001 From: Lex Koomen Date: Wed, 20 Mar 2019 10:12:01 +0100 Subject: [PATCH 2/2] SCPURDEY-523 +: removed firstKey to current --- Helper/Data.php | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/Helper/Data.php b/Helper/Data.php index 1451971..1fa3f77 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -109,8 +109,7 @@ public function getConfigDisplayValue($path, $contextScope, $contextScopeId) $value = $this->_getConfigValue($path, $contextScope, $contextScopeId); if (is_array($value)) { - $firstKey = array_keys($value)[0]; - $labels = [$value[$firstKey]]; + $labels = [current($value)]; } else { $labels = [$value]; //default labels to raw value } @@ -177,11 +176,8 @@ public function getOverriddenLevels($path, $contextScope, $contextScopeId) $websiteValue = $this->_getConfigValue($path, self::WEBSITE_SCOPE_CODE, $websiteId); if (is_array($currentValue) && is_array($websiteValue)) { - $firstKeyCurrent = array_keys($currentValue)[0]; - $firstKeyWebsite = array_keys($websiteValue)[0]; - - $websiteValueArray = $websiteValue[$firstKeyWebsite]; - $currentValueArray = $currentValue[$firstKeyCurrent]; + $websiteValueArray = [current($websiteValue)]; + $currentValueArray = [current($currentValue)]; if ($websiteValueArray != $currentValueArray) { $overridden[] = [ 'scope' => 'website', @@ -203,10 +199,7 @@ public function getOverriddenLevels($path, $contextScope, $contextScopeId) foreach ($website[self::STORE_VIEW_SCOPE_CODE] as $storeId) { $value = $this->_getConfigValue($path, self::STORE_VIEW_SCOPE_CODE, $storeId); if (is_array($currentValue) && is_array($value)) { - $firstKeyCurrent = array_keys($currentValue)[0]; - $firstKeyStore = array_keys($value)[0]; - - $storeValueArray = $value[$firstKeyStore]; + $storeValueArray = [current($value)]; if ($storeValueArray != $websiteValueArray) { $overridden[] = [ 'scope' => 'store',