Skip to content

Commit 9de7822

Browse files
authored
Merge pull request #28 from avstudnitz/support-config-select-and-multiselect
Support config options and multiselect
2 parents 8c2373f + dae2005 commit 9de7822

File tree

3 files changed

+78
-35
lines changed

3 files changed

+78
-35
lines changed

Plugin/ConfigFieldPlugin.php

Lines changed: 74 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Store\Api\StoreRepositoryInterface;
1111
use Magento\Store\Api\Data\WebsiteInterface;
1212
use Magento\Store\Api\Data\StoreInterface;
13+
use Magento\Store\Model\ScopeInterface;
1314

1415
class ConfigFieldPlugin
1516
{
@@ -59,17 +60,24 @@ public function afterGetTooltip(Subject $subject, $result)
5960
{
6061
$lines = [$result];
6162
foreach($this->websiteRepository->getList() as $website) {
62-
if (!$this->isWebsiteSelected($website)) {
63-
if ($scopeLine = $this->getScopeHint($this->getPath($subject), self::SCOPE_TYPE_WEBSITES, $website)) {
64-
$lines[] = $scopeLine;
65-
}
63+
if ($this->getWebsiteParam() || $this->getStoreParam()) {
64+
continue;
65+
}
66+
// Only show website specific values in default scope
67+
if ($scopeLine = $this->getScopeHint($subject, self::SCOPE_TYPE_WEBSITES, $website)) {
68+
$lines[] = $scopeLine;
6669
}
6770
}
6871
foreach($this->storeRepository->getList() as $store) {
69-
if (!$this->isStoreSelected($store)) {
70-
if ($scopeLine = $this->getScopeHint($this->getPath($subject), self::SCOPE_TYPE_STORES, $store)) {
71-
$lines[] = $scopeLine;
72-
}
72+
if ($this->getStoreParam($store)) {
73+
continue;
74+
}
75+
if (($websiteId = $this->getWebsiteParam()) && ($store->getWebsiteId() != $websiteId)) {
76+
continue;
77+
}
78+
// Only show store specific values in default scope and in parent website scope
79+
if ($scopeLine = $this->getScopeHint($subject, self::SCOPE_TYPE_STORES, $store)) {
80+
$lines[] = $scopeLine;
7381
}
7482
}
7583
return implode('<br />', array_filter($lines));
@@ -111,44 +119,78 @@ private function getPath(Subject $subject)
111119
}
112120

113121
/**
114-
* @param WebsiteInterface $website
115-
* @return bool
116-
*/
117-
private function isWebsiteSelected($website)
118-
{
119-
return $website->getId() == $this->request->getParam('website');
120-
}
121-
122-
/**
123-
* @param StoreInterface $store
124-
* @return bool
125-
*/
126-
private function isStoreSelected($store)
127-
{
128-
return $store->getId() == $this->request->getParam('store');
129-
}
130-
131-
/**
132-
* @param string $path
122+
* @param Subject $field
133123
* @param string $scopeType
134124
* @param WebsiteInterface|StoreInterface $scope
135125
* @return string
136126
*/
137-
private function getScopeHint($path, $scopeType, $scope)
127+
private function getScopeHint(Subject $field, $scopeType, $scope)
138128
{
129+
$path = $this->getPath($field);
139130
$scopeLine = '';
140-
$currentValue = $this->scopeConfig->getValue($path);
141-
$scopeValue = $this->scopeConfig->getValue($path, $scopeType, $scope->getId());
131+
if ($websiteId = $this->getWebsiteParam()) {
132+
$currentValue = (string) $this->scopeConfig->getValue(
133+
$path,
134+
ScopeInterface::SCOPE_WEBSITE,
135+
$websiteId
136+
);
137+
} else {
138+
$currentValue = (string) $this->scopeConfig->getValue($path);
139+
}
140+
$scopeValue = (string) $this->scopeConfig->getValue($path, $scopeType, $scope->getId());
141+
142142
if ($scopeValue != $currentValue) {
143143
$scopeValue = $this->escaper->escapeHtml($scopeValue);
144144

145145
switch($scopeType) {
146146
case self::SCOPE_TYPE_STORES:
147-
return __('Store <code>%1</code>: "%2"', $scope->getCode(), $scopeValue);
147+
return __(
148+
'Store <code>%1</code>: "%2"',
149+
$scope->getCode(),
150+
$this->getValueLabel($field, $scopeValue)
151+
);
148152
case self::SCOPE_TYPE_WEBSITES:
149-
return __('Website <code>%1</code>: "%2"', $scope->getCode(), $scopeValue);
153+
return __(
154+
'Website <code>%1</code>: "%2"',
155+
$scope->getCode(),
156+
$this->getValueLabel($field, $scopeValue)
157+
);
150158
}
151159
}
152160
return $scopeLine;
153161
}
162+
163+
private function getValueLabel(Subject $field, string $scopeValue): string
164+
{
165+
$scopeValue = trim($scopeValue);
166+
if ($field->hasOptions()) {
167+
if ($field->getType() === 'multiselect' && strpos($scopeValue, ',') !== false) {
168+
return implode(
169+
', ',
170+
array_map(
171+
function ($key) use ($field) {
172+
return $this->getValueLabel($field, $key);
173+
},
174+
explode(',', $scopeValue)
175+
)
176+
);
177+
}
178+
foreach ($field->getOptions() as $option) {
179+
if ($option['value'] == $scopeValue) {
180+
return $option['label'];
181+
}
182+
}
183+
}
184+
return $scopeValue;
185+
}
186+
187+
private function getWebsiteParam(): ?string
188+
{
189+
return $this->request->getParam('website');
190+
}
191+
192+
private function getStoreParam(): ?string
193+
{
194+
return $this->request->getParam('store');
195+
}
154196
}

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"name": "avstudnitz/scopehint2",
3-
"description": "N/A",
3+
"description": "Displays a hint when a configuration value is overwritten on a lower scope (website or store view).",
44
"require": {
5-
"magento/framework": "~100.0|~101.0|~102.0|~103.0"
5+
"magento/framework": "~102.0|~103.0",
6+
"magento/module-config": "~100.0|~101.0"
67
},
78
"type": "magento2-module",
89
"license": [

etc/module.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0"?>
22
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
3-
<module name="AvS_ScopeHint" setup_version="1.0.0">
3+
<module name="AvS_ScopeHint">
44
<sequence>
55
<module name="Magento_Config"/>
66
</sequence>

0 commit comments

Comments
 (0)