Skip to content

Commit 8d11456

Browse files
committed
MAGETWO-99091: Name of categories in the Category tree on Product Edit page is not displayed according to the selected store view scope
1 parent d81b399 commit 8d11456

File tree

2 files changed

+64
-48
lines changed

2 files changed

+64
-48
lines changed

app/code/Magento/Catalog/Test/Unit/Ui/DataProvider/Product/Form/Modifier/CategoriesTest.php

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -154,38 +154,4 @@ public function modifyMetaLockedDataProvider()
154154
{
155155
return [[true], [false]];
156156
}
157-
158-
public function testModifyMetaWithCaching()
159-
{
160-
$this->arrayManagerMock->expects($this->exactly(2))
161-
->method('findPath')
162-
->willReturn(true);
163-
$cacheManager = $this->getMockBuilder(CacheInterface::class)
164-
->getMockForAbstractClass();
165-
$cacheManager->expects($this->once())
166-
->method('load')
167-
->with(Categories::CATEGORY_TREE_ID . '_');
168-
$cacheManager->expects($this->once())
169-
->method('save');
170-
171-
$modifier = $this->createModel();
172-
$cacheContextProperty = new \ReflectionProperty(
173-
Categories::class,
174-
'cacheManager'
175-
);
176-
$cacheContextProperty->setAccessible(true);
177-
$cacheContextProperty->setValue($modifier, $cacheManager);
178-
179-
$groupCode = 'test_group_code';
180-
$meta = [
181-
$groupCode => [
182-
'children' => [
183-
'category_ids' => [
184-
'sortOrder' => 10,
185-
],
186-
],
187-
],
188-
];
189-
$modifier->modifyMeta($meta);
190-
}
191157
}

app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/Categories.php

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\App\CacheInterface;
1212
use Magento\Framework\DB\Helper as DbHelper;
1313
use Magento\Catalog\Model\Category as CategoryModel;
14+
use Magento\Framework\Exception\LocalizedException;
1415
use Magento\Framework\Serialize\SerializerInterface;
1516
use Magento\Framework\UrlInterface;
1617
use Magento\Framework\Stdlib\ArrayManager;
@@ -202,6 +203,7 @@ protected function createNewCategoryModal(array $meta)
202203
*
203204
* @param array $meta
204205
* @return array
206+
* @throws LocalizedException
205207
* @since 101.0.0
206208
*/
207209
protected function customizeCategoriesField(array $meta)
@@ -306,20 +308,64 @@ protected function customizeCategoriesField(array $meta)
306308
*
307309
* @param string|null $filter
308310
* @return array
311+
* @throws LocalizedException
309312
* @since 101.0.0
310313
*/
311314
protected function getCategoriesTree($filter = null)
312315
{
313-
$categoryTree = $this->getCacheManager()->load(self::CATEGORY_TREE_ID . '_' . $filter);
314-
if ($categoryTree) {
315-
return $this->serializer->unserialize($categoryTree);
316+
$storeId = $this->locator->getStore()->getId();
317+
318+
$cachedCategoriesTree = $this->getCacheManager()
319+
->load($this->getCategoriesTreeCacheId($storeId, (string) $filter));
320+
if (!empty($cachedCategoriesTree)) {
321+
return $this->serializer->unserialize($cachedCategoriesTree);
316322
}
317323

318-
$storeId = $this->locator->getStore()->getId();
324+
$categoriesTree = $this->retrieveCategoriesTree(
325+
$storeId,
326+
$this->retrieveShownCategoriesIds($storeId, (string) $filter)
327+
);
328+
329+
$this->getCacheManager()->save(
330+
$this->serializer->serialize($categoriesTree),
331+
$this->getCategoriesTreeCacheId($storeId, (string) $filter),
332+
[
333+
\Magento\Catalog\Model\Category::CACHE_TAG,
334+
\Magento\Framework\App\Cache\Type\Block::CACHE_TAG
335+
]
336+
);
337+
338+
return $categoriesTree;
339+
}
340+
341+
/**
342+
* Get cache id for categories tree.
343+
*
344+
* @param int $storeId
345+
* @param string $filter
346+
* @return string
347+
*/
348+
private function getCategoriesTreeCacheId($storeId, $filter = '')
349+
{
350+
return self::CATEGORY_TREE_ID
351+
. '_' . (string) $storeId
352+
. '_' . $filter;
353+
}
354+
355+
/**
356+
* Retrieve filtered list of categories id.
357+
*
358+
* @param int $storeId
359+
* @param string $filter
360+
* @return array
361+
* @throws LocalizedException
362+
*/
363+
private function retrieveShownCategoriesIds($storeId, $filter = '')
364+
{
319365
/* @var $matchingNamesCollection \Magento\Catalog\Model\ResourceModel\Category\Collection */
320366
$matchingNamesCollection = $this->categoryCollectionFactory->create();
321367

322-
if ($filter !== null) {
368+
if (!empty($filter)) {
323369
$matchingNamesCollection->addAttributeToFilter(
324370
'name',
325371
['like' => $this->dbHelper->addLikeEscape($filter, ['position' => 'any'])]
@@ -339,6 +385,19 @@ protected function getCategoriesTree($filter = null)
339385
}
340386
}
341387

388+
return $shownCategoriesIds;
389+
}
390+
391+
/**
392+
* Retrieve tree of categories with attributes.
393+
*
394+
* @param int $storeId
395+
* @param array $shownCategoriesIds
396+
* @return array
397+
* @throws LocalizedException
398+
*/
399+
private function retrieveCategoriesTree($storeId, array $shownCategoriesIds = [])
400+
{
342401
/* @var $collection \Magento\Catalog\Model\ResourceModel\Category\Collection */
343402
$collection = $this->categoryCollectionFactory->create();
344403

@@ -365,15 +424,6 @@ protected function getCategoriesTree($filter = null)
365424
$categoryById[$category->getParentId()]['optgroup'][] = &$categoryById[$category->getId()];
366425
}
367426

368-
$this->getCacheManager()->save(
369-
$this->serializer->serialize($categoryById[CategoryModel::TREE_ROOT_ID]['optgroup']),
370-
self::CATEGORY_TREE_ID . '_' . $filter,
371-
[
372-
\Magento\Catalog\Model\Category::CACHE_TAG,
373-
\Magento\Framework\App\Cache\Type\Block::CACHE_TAG
374-
]
375-
);
376-
377427
return $categoryById[CategoryModel::TREE_ROOT_ID]['optgroup'];
378428
}
379429
}

0 commit comments

Comments
 (0)