diff --git a/Plugin/CategoryFieldPlugin.php b/Plugin/CategoryFieldPlugin.php new file mode 100644 index 0000000..786c8cd --- /dev/null +++ b/Plugin/CategoryFieldPlugin.php @@ -0,0 +1,113 @@ +storeManager = $storeManager; + $this->registry = $registry; + $this->categoryRepository = $categoryRepository; + $this->escaper = $escaper; + } + + /** + * @param FieldComponent $subject + * @param $config + * @return array + */ + public function afterGetConfiguration(FieldComponent $subject, $config) + { + if ($subject->getContext()->getNamespace() == 'category_form' && + $config['formElement'] != 'hidden' && + strpos($config['dataScope'], 'use_config.') === false + ) { + $scopeHints = []; + $attributeCode = $config['dataScope']; + $storeViews = $this->storeManager->getStores(); + $category = $this->registry->registry('current_category'); + + if ($category === null || $category->getId() === null) { + return $config; + } + + foreach ($storeViews as $storeView) { + $categoryByStoreCode = $this->getCategoryInStoreView($category->getId(), $storeView->getId()); + $currentScopeValueForCode = $value = $categoryByStoreCode->getData($attributeCode); + + if ($config['dataScope'] == 'select' && !is_array($currentScopeValueForCode)) { + $value = $categoryByStoreCode + ->getResource() + ->getAttribute($attributeCode) + ->getSource() + ->getOptionText($currentScopeValueForCode); + } + + try { + $valueAsString = (string) $value; + } catch (\Throwable $ex) { + $valueAsString = json_encode($value); + if ($valueAsString === false) { + $valueAsString = null; + } + } + + if ($valueAsString !== null && $category->getData($attributeCode) !== $currentScopeValueForCode) { + $scopeHints[] = '' . $storeView->getName() . ' : ' . $this->escaper->escapeHtml($valueAsString); + } + + if (!empty($scopeHints)) { + $config['tooltip']['description'] = implode('
', $scopeHints); + } + } + } + return $config; + } + + /** + * @param $categoryId + * @param $storeViewId + * @return \Magento\Catalog\Api\Data\CategoryInterface + */ + private function getCategoryInStoreView($categoryId, $storeViewId) + { + return $this->categoryRepository->get($categoryId, $storeViewId); + } +} diff --git a/etc/adminhtml/di.xml b/etc/adminhtml/di.xml index da15744..107a455 100644 --- a/etc/adminhtml/di.xml +++ b/etc/adminhtml/di.xml @@ -4,7 +4,11 @@ + + + + - \ No newline at end of file +