From 5c56b89241e0f4f1b6ce0aac63e51d1fa47c76a5 Mon Sep 17 00:00:00 2001 From: Andreas von Studnitz Date: Fri, 30 Sep 2022 16:15:48 +0200 Subject: [PATCH 1/4] Support config options and multiselect --- Plugin/ConfigFieldPlugin.php | 107 ++++++++++++++++++++++++----------- composer.json | 5 +- etc/module.xml | 2 +- 3 files changed, 79 insertions(+), 35 deletions(-) diff --git a/Plugin/ConfigFieldPlugin.php b/Plugin/ConfigFieldPlugin.php index d71cc58..3a7f5f0 100644 --- a/Plugin/ConfigFieldPlugin.php +++ b/Plugin/ConfigFieldPlugin.php @@ -10,6 +10,7 @@ use Magento\Store\Api\StoreRepositoryInterface; use Magento\Store\Api\Data\WebsiteInterface; use Magento\Store\Api\Data\StoreInterface; +use Magento\Store\Model\ScopeInterface; class ConfigFieldPlugin { @@ -59,17 +60,25 @@ public function afterGetTooltip(Subject $subject, $result) { $lines = [$result]; foreach($this->websiteRepository->getList() as $website) { - if (!$this->isWebsiteSelected($website)) { - if ($scopeLine = $this->getScopeHint($this->getPath($subject), self::SCOPE_TYPE_WEBSITES, $website)) { - $lines[] = $scopeLine; - } + if ($this->getWebsiteParam() || $this->getStoreParam()) { + continue; + } + // Only show website specific values in default scope + if ($scopeLine = $this->getScopeHint($subject, self::SCOPE_TYPE_WEBSITES, $website)) { + $lines[] = $scopeLine; } } foreach($this->storeRepository->getList() as $store) { - if (!$this->isStoreSelected($store)) { - if ($scopeLine = $this->getScopeHint($this->getPath($subject), self::SCOPE_TYPE_STORES, $store)) { - $lines[] = $scopeLine; - } + if ($this->getStoreParam($store)) { + + continue; + } + if (($websiteId = $this->getWebsiteParam()) && ($store->getWebsiteId() != $websiteId)) { + continue; + } + // Only show store specific values in default scope and in parent website scope + if ($scopeLine = $this->getScopeHint($subject, self::SCOPE_TYPE_STORES, $store)) { + $lines[] = $scopeLine; } } return implode('
', array_filter($lines)); @@ -111,44 +120,78 @@ private function getPath(Subject $subject) } /** - * @param WebsiteInterface $website - * @return bool - */ - private function isWebsiteSelected($website) - { - return $website->getId() == $this->request->getParam('website'); - } - - /** - * @param StoreInterface $store - * @return bool - */ - private function isStoreSelected($store) - { - return $store->getId() == $this->request->getParam('store'); - } - - /** - * @param string $path + * @param Subject $field * @param string $scopeType * @param WebsiteInterface|StoreInterface $scope * @return string */ - private function getScopeHint($path, $scopeType, $scope) + private function getScopeHint(Subject $field, $scopeType, $scope) { + $path = $this->getPath($field); $scopeLine = ''; - $currentValue = $this->scopeConfig->getValue($path); - $scopeValue = $this->scopeConfig->getValue($path, $scopeType, $scope->getId()); + if ($websiteId = $this->getWebsiteParam()) { + $currentValue = (string) $this->scopeConfig->getValue( + $path, + ScopeInterface::SCOPE_WEBSITE, + $websiteId + ); + } else { + $currentValue = (string) $this->scopeConfig->getValue($path); + } + $scopeValue = (string) $this->scopeConfig->getValue($path, $scopeType, $scope->getId()); + if ($scopeValue != $currentValue) { $scopeValue = $this->escaper->escapeHtml($scopeValue); switch($scopeType) { case self::SCOPE_TYPE_STORES: - return __('Store %1: "%2"', $scope->getCode(), $scopeValue); + return __( + 'Store %1: "%2"', + $scope->getCode(), + $this->getValueLabel($field, $scopeValue) + ); case self::SCOPE_TYPE_WEBSITES: - return __('Website %1: "%2"', $scope->getCode(), $scopeValue); + return __( + 'Website %1: "%2"', + $scope->getCode(), + $this->getValueLabel($field, $scopeValue) + ); } } return $scopeLine; } + + private function getValueLabel(Subject $field, string $scopeValue): string + { + $scopeValue = trim($scopeValue); + if ($field->hasOptions()) { + if ($field->getType() == 'multiselect' && strpos($scopeValue, ',') !== false) { + return implode( + ', ', + array_map( + function ($key) use ($field) { + return $this->getValueLabel($field, $key); + }, + explode(',', $scopeValue) + ) + ); + } + foreach ($field->getOptions() as $option) { + if ($option['value'] == $scopeValue) { + return $option['label']; + } + } + } + return $scopeValue; + } + + private function getWebsiteParam(): ?string + { + return $this->request->getParam('website'); + } + + private function getStoreParam(): ?string + { + return $this->request->getParam('store'); + } } diff --git a/composer.json b/composer.json index e27b49e..c6aeaf2 100644 --- a/composer.json +++ b/composer.json @@ -1,8 +1,9 @@ { "name": "avstudnitz/scopehint2", - "description": "N/A", + "description": "Displays a hint when a configuration value is overwritten on a lower scope (website or store view).", "require": { - "magento/framework": "~100.0|~101.0|~102.0|~103.0" + "magento/framework": "~100.0|~101.0|~102.0|~103.0", + "magento/module-config": "~100.0|~101.0" }, "type": "magento2-module", "license": [ diff --git a/etc/module.xml b/etc/module.xml index c53941f..6a407f3 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,6 +1,6 @@ - + From 1f45d7f6b9c41fd1ae52f6ac78e2ae597f113c50 Mon Sep 17 00:00:00 2001 From: Andreas von Studnitz Date: Tue, 4 Oct 2022 14:04:49 +0000 Subject: [PATCH 2/4] Make comparison type safe --- Plugin/ConfigFieldPlugin.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Plugin/ConfigFieldPlugin.php b/Plugin/ConfigFieldPlugin.php index 3a7f5f0..8d63425 100644 --- a/Plugin/ConfigFieldPlugin.php +++ b/Plugin/ConfigFieldPlugin.php @@ -70,7 +70,6 @@ public function afterGetTooltip(Subject $subject, $result) } foreach($this->storeRepository->getList() as $store) { if ($this->getStoreParam($store)) { - continue; } if (($websiteId = $this->getWebsiteParam()) && ($store->getWebsiteId() != $websiteId)) { @@ -165,7 +164,7 @@ private function getValueLabel(Subject $field, string $scopeValue): string { $scopeValue = trim($scopeValue); if ($field->hasOptions()) { - if ($field->getType() == 'multiselect' && strpos($scopeValue, ',') !== false) { + if ($field->getType() === 'multiselect' && strpos($scopeValue, ',') !== false) { return implode( ', ', array_map( From 34a94ff72ba43c082f5dc1c44268cb1ca97972f8 Mon Sep 17 00:00:00 2001 From: Andreas von Studnitz Date: Thu, 6 Oct 2022 13:33:40 +0000 Subject: [PATCH 3/4] Undo removing the setup version in order to restore compatibility to Magento < 2.3.0 --- etc/module.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/etc/module.xml b/etc/module.xml index 6a407f3..c53941f 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,6 +1,6 @@ - + From dae2005f683f5962878055393c6ee5b2ce938ce9 Mon Sep 17 00:00:00 2001 From: Andreas von Studnitz Date: Thu, 6 Oct 2022 13:36:03 +0000 Subject: [PATCH 4/4] Mark as incompatible to Magento < 2.3 --- composer.json | 2 +- etc/module.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index c6aeaf2..9ae01c2 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "avstudnitz/scopehint2", "description": "Displays a hint when a configuration value is overwritten on a lower scope (website or store view).", "require": { - "magento/framework": "~100.0|~101.0|~102.0|~103.0", + "magento/framework": "~102.0|~103.0", "magento/module-config": "~100.0|~101.0" }, "type": "magento2-module", diff --git a/etc/module.xml b/etc/module.xml index c53941f..6a407f3 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,6 +1,6 @@ - +