Skip to content

Commit

Permalink
Merge pull request #28 from avstudnitz/support-config-select-and-mult…
Browse files Browse the repository at this point in the history
…iselect

Support config options and multiselect
  • Loading branch information
avstudnitz authored Oct 17, 2022
2 parents 8c2373f + dae2005 commit 9de7822
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 35 deletions.
106 changes: 74 additions & 32 deletions Plugin/ConfigFieldPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -59,17 +60,24 @@ 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('<br />', array_filter($lines));
Expand Down Expand Up @@ -111,44 +119,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 <code>%1</code>: "%2"', $scope->getCode(), $scopeValue);
return __(
'Store <code>%1</code>: "%2"',
$scope->getCode(),
$this->getValueLabel($field, $scopeValue)
);
case self::SCOPE_TYPE_WEBSITES:
return __('Website <code>%1</code>: "%2"', $scope->getCode(), $scopeValue);
return __(
'Website <code>%1</code>: "%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');
}
}
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": "~102.0|~103.0",
"magento/module-config": "~100.0|~101.0"
},
"type": "magento2-module",
"license": [
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="AvS_ScopeHint" setup_version="1.0.0">
<module name="AvS_ScopeHint">
<sequence>
<module name="Magento_Config"/>
</sequence>
Expand Down

0 comments on commit 9de7822

Please sign in to comment.