|
5 | 5 | */
|
6 | 6 | namespace Magento\Catalog\Model\Category;
|
7 | 7 |
|
| 8 | +use Magento\Catalog\Api\Data\CategoryInterface; |
| 9 | +use Magento\Catalog\Api\Data\EavAttributeInterface; |
| 10 | +use Magento\Catalog\Model\Attribute\ScopeOverriddenValue; |
8 | 11 | use Magento\Catalog\Model\Category;
|
9 | 12 | use Magento\Catalog\Model\ResourceModel\Eav\Attribute as EavAttribute;
|
10 | 13 | use Magento\Eav\Api\Data\AttributeInterface;
|
11 | 14 | use Magento\Eav\Model\Config;
|
12 | 15 | use Magento\Eav\Model\Entity\Type;
|
13 | 16 | use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
|
| 17 | +use Magento\Framework\Stdlib\ArrayManager; |
14 | 18 | use Magento\Store\Model\Store;
|
15 | 19 | use Magento\Store\Model\StoreManagerInterface;
|
16 | 20 | use Magento\Ui\Component\Form\Field;
|
@@ -112,6 +116,16 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
|
112 | 116 | */
|
113 | 117 | private $categoryFactory;
|
114 | 118 |
|
| 119 | + /** |
| 120 | + * @var ScopeOverriddenValue |
| 121 | + */ |
| 122 | + private $scopeOverriddenValue; |
| 123 | + |
| 124 | + /** |
| 125 | + * @var ArrayManager |
| 126 | + */ |
| 127 | + private $arrayManager; |
| 128 | + |
115 | 129 | /**
|
116 | 130 | * DataProvider constructor
|
117 | 131 | *
|
@@ -151,15 +165,65 @@ public function __construct(
|
151 | 165 | $this->storeManager = $storeManager;
|
152 | 166 | $this->request = $request;
|
153 | 167 | $this->categoryFactory = $categoryFactory;
|
| 168 | + |
154 | 169 | parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
|
155 |
| - $this->meta = $this->prepareMeta($this->meta); |
| 170 | + } |
| 171 | + |
| 172 | + public function getMeta() |
| 173 | + { |
| 174 | + $meta = parent::getMeta(); |
| 175 | + $meta = $this->prepareMeta($meta); |
| 176 | + $meta = $this->addUseDefaultValueCheckbox($this->getCurrentCategory(), $meta); |
| 177 | + |
| 178 | + return $meta; |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * @param Category $category |
| 183 | + * @param array $meta |
| 184 | + * @return array |
| 185 | + */ |
| 186 | + private function addUseDefaultValueCheckbox(Category $category, array $meta) |
| 187 | + { |
| 188 | + /** @var EavAttributeInterface $attribute */ |
| 189 | + foreach ($category->getAttributes() as $attribute) { |
| 190 | + $attributeCode = $attribute->getAttributeCode(); |
| 191 | + $canDisplayUseDefault = $attribute->getScope() != EavAttributeInterface::SCOPE_GLOBAL_TEXT |
| 192 | + && $category->getId() |
| 193 | + && $category->getStoreId(); |
| 194 | + $sectionName = $this->getSectionName($meta, $attributeCode); |
| 195 | + |
| 196 | + if (!$sectionName || !$canDisplayUseDefault) { |
| 197 | + continue; |
| 198 | + } |
| 199 | + |
| 200 | + $meta = $this->getArrayManager()->set( |
| 201 | + [$sectionName, 'children', $attributeCode, 'arguments/data/config'], |
| 202 | + $meta, |
| 203 | + [ |
| 204 | + 'service' => [ |
| 205 | + 'template' => 'ui/form/element/helper/service', |
| 206 | + ], |
| 207 | + 'disabled' => !$this->getScopeOverriddenValue()->containsValue( |
| 208 | + CategoryInterface::class, |
| 209 | + $category, |
| 210 | + $attributeCode, |
| 211 | + $this->request->getParam($this->requestScopeFieldName, Store::DEFAULT_STORE_ID) |
| 212 | + ) |
| 213 | + ] |
| 214 | + ); |
| 215 | + |
| 216 | + } |
| 217 | + |
| 218 | + return $meta; |
156 | 219 | }
|
157 | 220 |
|
158 | 221 | /**
|
159 | 222 | * Prepare meta data
|
160 | 223 | *
|
161 | 224 | * @param array $meta
|
162 | 225 | * @return array
|
| 226 | + * @deprecated |
163 | 227 | */
|
164 | 228 | public function prepareMeta($meta)
|
165 | 229 | {
|
@@ -204,7 +268,6 @@ public function getData()
|
204 | 268 | $category = $this->getCurrentCategory();
|
205 | 269 | if ($category) {
|
206 | 270 | $categoryData = $category->getData();
|
207 |
| - $categoryData = $this->addUseDefaultSettings($category, $categoryData); |
208 | 271 | $categoryData = $this->addUseConfigSettings($categoryData);
|
209 | 272 | $categoryData = $this->filterFields($categoryData);
|
210 | 273 | $categoryData = $this->convertValues($category, $categoryData);
|
@@ -292,6 +355,7 @@ protected function addUseConfigSettings($categoryData)
|
292 | 355 | * @param \Magento\Catalog\Model\Category $category
|
293 | 356 | * @param array $categoryData
|
294 | 357 | * @return array
|
| 358 | + * @deprecated |
295 | 359 | */
|
296 | 360 | protected function addUseDefaultSettings($category, $categoryData)
|
297 | 361 | {
|
@@ -484,4 +548,56 @@ protected function getFieldsMap()
|
484 | 548 | ],
|
485 | 549 | ];
|
486 | 550 | }
|
| 551 | + |
| 552 | + /** |
| 553 | + * Retrieve section name by attribute code |
| 554 | + * |
| 555 | + * @param array $meta |
| 556 | + * @param string $attributeCode |
| 557 | + * @return string|null |
| 558 | + */ |
| 559 | + private function getSectionName(array $meta, $attributeCode) |
| 560 | + { |
| 561 | + foreach ($meta as $name => $section) { |
| 562 | + if (isset($section['children'][$attributeCode])) { |
| 563 | + return $name; |
| 564 | + } |
| 565 | + } |
| 566 | + |
| 567 | + return null; |
| 568 | + } |
| 569 | + |
| 570 | + /** |
| 571 | + * Retrieve scope overridden value |
| 572 | + * |
| 573 | + * @return ScopeOverriddenValue |
| 574 | + * @deprecated |
| 575 | + */ |
| 576 | + private function getScopeOverriddenValue() |
| 577 | + { |
| 578 | + if (null === $this->scopeOverriddenValue) { |
| 579 | + $this->scopeOverriddenValue = \Magento\Framework\App\ObjectManager::getInstance()->get( |
| 580 | + ScopeOverriddenValue::class |
| 581 | + ); |
| 582 | + } |
| 583 | + |
| 584 | + return $this->scopeOverriddenValue; |
| 585 | + } |
| 586 | + |
| 587 | + /** |
| 588 | + * Retrieve array manager |
| 589 | + * |
| 590 | + * @return ArrayManager |
| 591 | + * @deprecated |
| 592 | + */ |
| 593 | + private function getArrayManager() |
| 594 | + { |
| 595 | + if (null === $this->arrayManager) { |
| 596 | + $this->arrayManager = \Magento\Framework\App\ObjectManager::getInstance()->get( |
| 597 | + ArrayManager::class |
| 598 | + ); |
| 599 | + } |
| 600 | + |
| 601 | + return $this->arrayManager; |
| 602 | + } |
487 | 603 | }
|
0 commit comments