From 060af9bdb4f34293d0213ed6b93b49a471233642 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Wed, 1 Nov 2023 13:26:10 +0100 Subject: [PATCH] [BUGFIX][IN23-255] Silencing errors is discouraged; found: @$optionsByValue[$option... AND display of array value's is broken due to faulty logic for human readable labels. --- CHANGELOG.md | 28 ++++++++++++ Helper/Data.php | 116 +++++++++++++++++++++++++----------------------- 2 files changed, 88 insertions(+), 56 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 72ad5fe..d1ae312 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,31 @@ +## 4.1.9 (2023-11-01) + +[View Release](git@github.com:experius/magento2-configscopehints.git/commits/tag/4.1.9) + +* [BUGFIX][IN23-255] Silencing errors is discouraged; found: @$optionsByValue[$option... AND display of array value's is broken due to faulty logic for human readable labels. *(Boris van Katwijk)* + + +## 4.1.8 (2021-12-24) + +[View Release](git@github.com:experius/magento2-configscopehints.git/commits/tag/4.1.8) + +* Add fallback for FieldArray labels *(Experius)* + + +## 4.1.7 (2021-08-03) + +[View Release](git@github.com:experius/magento2-configscopehints.git/commits/tag/4.1.7) + +* [BUGFIX] Solved issue in combination with Magmodules_Channable *(Mr. Lewis)* + + +## 4.1.6 (2021-01-25) + +[View Release](git@github.com:experius/magento2-configscopehints.git/commits/tag/4.1.6) + +* fixed error when is empty or no array *(mhaagen)* + + ## 4.1.5 (2020-10-28) [View Release](git@github.com:experius/magento2-configscopehints.git/commits/tag/4.1.5) diff --git a/Helper/Data.php b/Helper/Data.php index 07ef769..9f98b8c 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -6,6 +6,7 @@ declare(strict_types=1); namespace EW\ConfigScopeHints\Helper; + use \Magento\Store\Model\Website; use \Magento\Store\Model\Store; @@ -76,17 +77,18 @@ public function __construct( * * @return array */ - public function getScopeTree() { - $tree = array(self::WEBSITE_SCOPE_CODE => 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(); } } @@ -102,7 +104,8 @@ public function getScopeTree() { * @param string|int $contextScopeId * @return string */ - public function getConfigUpdatedAtLabel($path, $contextScope, $contextScopeId) { + public function getConfigUpdatedAtLabel($path, $contextScope, $contextScopeId) + { $connection = $this->resourceConnection->getConnection(); if ($contextScope === 'store') { $contextScope = 'stores'; @@ -127,7 +130,8 @@ public function getConfigUpdatedAtLabel($path, $contextScope, $contextScopeId) { * @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); } @@ -139,7 +143,8 @@ 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 @@ -147,11 +152,11 @@ public function getConfigDisplayValue($path, $contextScope, $contextScopeId) { /** @var \Magento\Config\Model\Config\Structure\Element\Field $field */ $field = $this->configStructure->getElement($path); - if($field->getOptions()) { + if ($field->getOptions() && $value && !is_array($value)) { $labels = []; //reset labels so we can add human-friendly labels $optionsByValue = []; - foreach($field->getOptions() as $id => $option) { + foreach ($field->getOptions() as $id => $option) { // Magento Enterprise modules can have different configuration value/label structure if ($option instanceof \Magento\Framework\Phrase) { $option = [ @@ -160,18 +165,14 @@ public function getConfigDisplayValue($path, $contextScope, $contextScopeId) { ]; } if (isset($option['value'])) { - @$optionsByValue[$option['value']] = $option; + $optionsByValue[$option['value']] = $option; } } - if (is_array($value)) { - $values = explode(',', $value); - - foreach ($values as $valueInstance) { - $labels[] = isset($optionsByValue[$valueInstance]) - ? $optionsByValue[$valueInstance]['label'] : $valueInstance; - - } + foreach (explode(',', $value) as $valueInstance) { + $labels[] = isset($optionsByValue[$valueInstance]) + && isset($optionsByValue[$valueInstance]['label']) + ? $optionsByValue[$valueInstance]['label'] : $valueInstance; } } @@ -188,52 +189,53 @@ 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), 'updated_at' => $this->getConfigUpdatedAtLabel($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 ($websiteValue != $currentValue) { + $overridden[] = [ + 'scope' => 'website', + 'scope_id' => $websiteId, 'value' => $websiteValue, 'display_value' => $this->getConfigDisplayValue($path, self::WEBSITE_SCOPE_CODE, $websiteId), 'updated_at' => $this->getConfigUpdatedAtLabel($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 ($value != $currentValue && $value != $websiteValue) { + $overridden[] = [ + 'scope' => 'store', + 'scope_id' => $storeId, 'value' => $value, 'display_value' => $this->getConfigDisplayValue($path, self::STORE_VIEW_SCOPE_CODE, $storeId), 'updated_at' => $this->getConfigUpdatedAtLabel($path, self::STORE_VIEW_SCOPE_CODE, $storeId) - ); + ]; } } } @@ -249,11 +251,12 @@ 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 (is_array($labels[0])) { return '' . - __('Please change scope to see store specific values') . + __('Please change scope to see store specific values') . '';; } //if only one value, simply return it @@ -264,10 +267,10 @@ protected function getFormattedValueLabels(array $labels) { $formattedLabels = ''; - foreach($labels as $label) { + foreach ($labels as $label) { $formattedLabels .= '
  • ' . nl2br($this->escaper->escapeHtml($label)) . - '
  • '; + ''; } return ''; @@ -280,12 +283,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']; $valueLabel = $overriddenScope['display_value']; @@ -293,14 +297,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', @@ -315,10 +319,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', @@ -329,8 +333,8 @@ public function formatOverriddenScopes($section, array $overridden) { } $formatted .= - '
    ' - . $scopeLabel . + '
    ' + . $scopeLabel . '
    ' . '
    ' . $this->getFormattedValueLabels($valueLabel) . $updatedAt . '
    '; }