|
| 1 | +<?php |
| 2 | +namespace AvS\ScopeHint\Plugin; |
| 3 | + |
| 4 | +use Magento\Catalog\Api\Data\ProductAttributeInterface; |
| 5 | +use Magento\Eav\Api\Data\AttributeInterface; |
| 6 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 7 | +use Magento\Framework\Stdlib\ArrayManager; |
| 8 | +use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Eav as EavModifier; |
| 9 | + |
| 10 | +class UIScopeLabel |
| 11 | +{ |
| 12 | + private const CONFIG_SHOW_ATTRIBUTE_CODE = 'dev/debug/show_attribute_code_in_adminhtml'; |
| 13 | + |
| 14 | + /** |
| 15 | + * @var ArrayManager |
| 16 | + */ |
| 17 | + private $arrayManager; |
| 18 | + |
| 19 | + /** |
| 20 | + * @var ScopeConfigInterface |
| 21 | + */ |
| 22 | + private $scopeConfig; |
| 23 | + |
| 24 | + public function __construct( |
| 25 | + ArrayManager $arrayManager, |
| 26 | + ScopeConfigInterface $scopeConfig, |
| 27 | + ) { |
| 28 | + $this->arrayManager = $arrayManager; |
| 29 | + $this->scopeConfig = $scopeConfig; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @param EavModifier $subject |
| 34 | + * @param array $result |
| 35 | + * @param ProductAttributeInterface $attribute |
| 36 | + * @return array |
| 37 | + */ |
| 38 | + public function afterSetupAttributeMeta(EavModifier $subject, $result, ProductAttributeInterface $attribute) |
| 39 | + { |
| 40 | + if (!$this->scopeConfig->isSetFlag(self::CONFIG_SHOW_ATTRIBUTE_CODE)) { |
| 41 | + return $result; |
| 42 | + } |
| 43 | + |
| 44 | + $configPath = ltrim(EavModifier::META_CONFIG_PATH, ArrayManager::DEFAULT_PATH_DELIMITER); |
| 45 | + $scopeLabel = $this->arrayManager->get( |
| 46 | + $configPath . ArrayManager::DEFAULT_PATH_DELIMITER . 'scopeLabel', |
| 47 | + $result |
| 48 | + ); |
| 49 | + |
| 50 | + if ($attribute->getFrontendInput() !== AttributeInterface::FRONTEND_INPUT) { |
| 51 | + $scopeLabel = $attribute->getAttributeCode() . ' ' . (string)$scopeLabel; |
| 52 | + $result = $this->arrayManager->merge( |
| 53 | + $configPath, |
| 54 | + $result, |
| 55 | + ['scopeLabel' => trim($scopeLabel)] |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + return $result; |
| 60 | + } |
| 61 | +} |
0 commit comments