diff --git a/Helper/Data.php b/Helper/Data.php index 4814772..1fa3f77 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,30 @@ 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)) { + $labels = [current($value)]; + } 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 +147,76 @@ 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)) { + $websiteValueArray = [current($websiteValue)]; + $currentValueArray = [current($currentValue)]; + 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)) { + $storeValueArray = [current($value)]; + 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 +232,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 +247,10 @@ protected function getFormattedValueLabels(array $labels) { $formattedLabels = ''; - foreach($labels as $label) { + foreach ($labels as $label) { $formattedLabels .= '
' . __('This config field is overridden at the following scope(s):') . '
' . '