Skip to content

Commit

Permalink
[BUGFIX][IN23-255] Silencing errors is discouraged; found: @$optionsB…
Browse files Browse the repository at this point in the history
…yValue[$option... AND display of array value's is broken due to faulty logic for human readable labels.
  • Loading branch information
borisvankatwijk committed Nov 1, 2023
1 parent f043179 commit 060af9b
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 56 deletions.
28 changes: 28 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
## 4.1.9 (2023-11-01)

[View Release]([email protected]: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]([email protected]:experius/magento2-configscopehints.git/commits/tag/4.1.8)

* Add fallback for FieldArray labels *(Experius)*


## 4.1.7 (2021-08-03)

[View Release]([email protected]: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]([email protected]: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]([email protected]:experius/magento2-configscopehints.git/commits/tag/4.1.5)
Expand Down
116 changes: 60 additions & 56 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
declare(strict_types=1);

namespace EW\ConfigScopeHints\Helper;

use \Magento\Store\Model\Website;
use \Magento\Store\Model\Store;

Expand Down Expand Up @@ -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();
}
}
Expand All @@ -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';
Expand All @@ -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);
}

Expand All @@ -139,19 +143,20 @@ 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

/** @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 = [
Expand All @@ -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;
}
}

Expand All @@ -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)
);
];
}
}
}
Expand All @@ -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 '<span class="override-value-hint-label">' .
__('Please change scope to see store specific values') .
__('Please change scope to see store specific values') .
'</span>';;
}
//if only one value, simply return it
Expand All @@ -264,10 +267,10 @@ protected function getFormattedValueLabels(array $labels) {

$formattedLabels = '';

foreach($labels as $label) {
foreach ($labels as $label) {
$formattedLabels .= '<li class="override-value-hint-label">' .
nl2br($this->escaper->escapeHtml($label)) .
'</li>';
'</li>';
}

return '<ul class="override-value-hint-labels">' . $formattedLabels . '</ul>';
Expand All @@ -280,27 +283,28 @@ protected function getFormattedValueLabels(array $labels) {
* @param array $overridden
* @return string
*/
public function formatOverriddenScopes($section, array $overridden) {
public function formatOverriddenScopes($section, array $overridden)
{
$formatted = '<div class="overridden-hint-wrapper">' .
'<p class="lead-text">' . __('This config field is overridden at the following scope(s):') . '</p>' .
'<dl class="overridden-hint-list">';

foreach($overridden as $overriddenScope) {
foreach ($overridden as $overriddenScope) {
$scope = $overriddenScope['scope'];
$scopeId = $overriddenScope['scope_id'];
$valueLabel = $overriddenScope['display_value'];
$updatedAt = $overriddenScope['updated_at'];
$scopeLabel = $scopeId;

$url = '#';
switch($scope) {
switch ($scope) {
case 'website':
$url = $this->urlBuilder->getUrl(
'*/*/*',
array(
[
'section' => $section,
'website' => $scopeId
)
]
);
$scopeLabel = __(
'Website <a href="%1">%2</a>',
Expand All @@ -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 <a href="%1">%2</a>',
Expand All @@ -329,8 +333,8 @@ public function formatOverriddenScopes($section, array $overridden) {
}

$formatted .=
'<dt class="override-scope ' . $scope . '" title="'. __('Click to see overridden value') .'">'
. $scopeLabel .
'<dt class="override-scope ' . $scope . '" title="' . __('Click to see overridden value') . '">'
. $scopeLabel .
'</dt>' .
'<dd class="override-value">' . $this->getFormattedValueLabels($valueLabel) . $updatedAt . '</dd>';
}
Expand Down

0 comments on commit 060af9b

Please sign in to comment.