Skip to content

Commit 36f08f0

Browse files
MAGETWO-48516: Most of category fields don't have Use Default Value option in Store/StoreView scope on the redesigned category page
1 parent 6619bdf commit 36f08f0

File tree

2 files changed

+123
-3
lines changed

2 files changed

+123
-3
lines changed

app/code/Magento/Catalog/Model/Category/DataProvider.php

Lines changed: 118 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55
*/
66
namespace Magento\Catalog\Model\Category;
77

8+
use Magento\Catalog\Api\Data\CategoryInterface;
9+
use Magento\Catalog\Api\Data\EavAttributeInterface;
10+
use Magento\Catalog\Model\Attribute\ScopeOverriddenValue;
811
use Magento\Catalog\Model\Category;
912
use Magento\Catalog\Model\ResourceModel\Eav\Attribute as EavAttribute;
1013
use Magento\Eav\Api\Data\AttributeInterface;
1114
use Magento\Eav\Model\Config;
1215
use Magento\Eav\Model\Entity\Type;
1316
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
17+
use Magento\Framework\Stdlib\ArrayManager;
1418
use Magento\Store\Model\Store;
1519
use Magento\Store\Model\StoreManagerInterface;
1620
use Magento\Ui\Component\Form\Field;
@@ -112,6 +116,16 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
112116
*/
113117
private $categoryFactory;
114118

119+
/**
120+
* @var ScopeOverriddenValue
121+
*/
122+
private $scopeOverriddenValue;
123+
124+
/**
125+
* @var ArrayManager
126+
*/
127+
private $arrayManager;
128+
115129
/**
116130
* DataProvider constructor
117131
*
@@ -151,15 +165,65 @@ public function __construct(
151165
$this->storeManager = $storeManager;
152166
$this->request = $request;
153167
$this->categoryFactory = $categoryFactory;
168+
154169
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;
156219
}
157220

158221
/**
159222
* Prepare meta data
160223
*
161224
* @param array $meta
162225
* @return array
226+
* @deprecated
163227
*/
164228
public function prepareMeta($meta)
165229
{
@@ -204,7 +268,6 @@ public function getData()
204268
$category = $this->getCurrentCategory();
205269
if ($category) {
206270
$categoryData = $category->getData();
207-
$categoryData = $this->addUseDefaultSettings($category, $categoryData);
208271
$categoryData = $this->addUseConfigSettings($categoryData);
209272
$categoryData = $this->filterFields($categoryData);
210273
$categoryData = $this->convertValues($category, $categoryData);
@@ -292,6 +355,7 @@ protected function addUseConfigSettings($categoryData)
292355
* @param \Magento\Catalog\Model\Category $category
293356
* @param array $categoryData
294357
* @return array
358+
* @deprecated
295359
*/
296360
protected function addUseDefaultSettings($category, $categoryData)
297361
{
@@ -484,4 +548,56 @@ protected function getFieldsMap()
484548
],
485549
];
486550
}
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+
}
487603
}

lib/internal/Magento/Framework/Stdlib/ArrayManager.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,18 @@ public function get($path, array $data, $defaultValue = null, $delimiter = self:
5757
/**
5858
* Set value into node and return modified data
5959
*
60-
* @param string $path
60+
* @param array|string $path
6161
* @param array $data
6262
* @param mixed $value
6363
* @param string $delimiter
6464
* @return array
6565
*/
6666
public function set($path, array $data, $value, $delimiter = self::DEFAULT_PATH_DELIMITER)
6767
{
68+
if (is_array($path)) {
69+
$path = implode($delimiter, $path);
70+
}
71+
6872
if ($this->find($path, $data, $delimiter, true)) {
6973
$this->parentNode[$this->nodeIndex] = $value;
7074
}

0 commit comments

Comments
 (0)