Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed category names in product edit for multi language stores. #22156

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,38 @@ public function modifyMetaLockedDataProvider()
{
return [[true], [false]];
}

public function testModifyMetaWithCaching()
{
$this->arrayManagerMock->expects($this->exactly(2))
->method('findPath')
->willReturn(true);
$cacheManager = $this->getMockBuilder(CacheInterface::class)
->getMockForAbstractClass();
$cacheManager->expects($this->once())
->method('load')
->with(Categories::CATEGORY_TREE_ID . '_');
$cacheManager->expects($this->once())
->method('save');

$modifier = $this->createModel();
$cacheContextProperty = new \ReflectionProperty(
Categories::class,
'cacheManager'
);
$cacheContextProperty->setAccessible(true);
$cacheContextProperty->setValue($modifier, $cacheManager);

$groupCode = 'test_group_code';
$meta = [
$groupCode => [
'children' => [
'category_ids' => [
'sortOrder' => 10,
],
],
],
];
$modifier->modifyMeta($meta);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Ui\DataProvider\Product\Form\Modifier;

use Magento\Catalog\Model\Locator\LocatorInterface;
Expand Down Expand Up @@ -337,55 +335,16 @@ protected function customizeCategoriesField(array $meta)
*/
protected function getCategoriesTree($filter = null)
{
$storeId = (int) $this->locator->getStore()->getId();

$cachedCategoriesTree = $this->getCacheManager()
->load($this->getCategoriesTreeCacheId($storeId, (string) $filter));
if (!empty($cachedCategoriesTree)) {
return $this->serializer->unserialize($cachedCategoriesTree);
$storeId = $this->locator->getStore()->getId();
$cacheKey = $storeId
? self::CATEGORY_TREE_ID . '_' . $storeId . '_' . $filter
: self::CATEGORY_TREE_ID . '_' . $filter;
ekalinak marked this conversation as resolved.
Show resolved Hide resolved

$categoryTree = $this->getCacheManager()->load($cacheKey);
if ($categoryTree) {
return $this->serializer->unserialize($categoryTree);
}

$categoriesTree = $this->retrieveCategoriesTree(
$storeId,
$this->retrieveShownCategoriesIds($storeId, (string) $filter)
);

$this->getCacheManager()->save(
$this->serializer->serialize($categoriesTree),
$this->getCategoriesTreeCacheId($storeId, (string) $filter),
[
\Magento\Catalog\Model\Category::CACHE_TAG,
\Magento\Framework\App\Cache\Type\Block::CACHE_TAG
]
);

return $categoriesTree;
}

/**
* Get cache id for categories tree.
*
* @param int $storeId
* @param string $filter
* @return string
*/
private function getCategoriesTreeCacheId(int $storeId, string $filter = '') : string
{
return self::CATEGORY_TREE_ID
. '_' . (string) $storeId
. '_' . $filter;
}

/**
* Retrieve filtered list of categories id.
*
* @param int $storeId
* @param string $filter
* @return array
* @throws LocalizedException
*/
private function retrieveShownCategoriesIds(int $storeId, string $filter = '') : array
{
/* @var $matchingNamesCollection \Magento\Catalog\Model\ResourceModel\Category\Collection */
$matchingNamesCollection = $this->categoryCollectionFactory->create();

Expand All @@ -409,19 +368,6 @@ private function retrieveShownCategoriesIds(int $storeId, string $filter = '') :
}
}

return $shownCategoriesIds;
}

/**
* Retrieve tree of categories with attributes.
*
* @param int $storeId
* @param array $shownCategoriesIds
* @return array|null
* @throws LocalizedException
*/
private function retrieveCategoriesTree(int $storeId, array $shownCategoriesIds) : ?array
{
/* @var $collection \Magento\Catalog\Model\ResourceModel\Category\Collection */
$collection = $this->categoryCollectionFactory->create();

Expand Down Expand Up @@ -449,6 +395,15 @@ private function retrieveCategoriesTree(int $storeId, array $shownCategoriesIds)
$categoryById[$category->getParentId()]['optgroup'][] = &$categoryById[$category->getId()];
}

$this->getCacheManager()->save(
$this->serializer->serialize($categoryById[CategoryModel::TREE_ROOT_ID]['optgroup']),
$cacheKey,
[
\Magento\Catalog\Model\Category::CACHE_TAG,
\Magento\Framework\App\Cache\Type\Block::CACHE_TAG
]
);

return $categoryById[CategoryModel::TREE_ROOT_ID]['optgroup'];
}
}