Skip to content

Commit f291b99

Browse files
author
Onischenko, Yaroslav(yonischenko)
committed
Merge pull request #8 from magento-goinc/MAGETWO-45113
MAGETWO-45113
2 parents a4ea0e7 + 837aaa7 commit f291b99

File tree

4 files changed

+62
-10
lines changed

4 files changed

+62
-10
lines changed

app/code/Magento/Catalog/Model/CategoryRepository.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ public function save(\Magento\Catalog\Api\Data\CategoryInterface $category)
106106
*/
107107
public function get($categoryId, $storeId = null)
108108
{
109-
if (!isset($this->instances[$categoryId])) {
109+
$cacheKey = null !== $storeId ? $storeId : 'all';
110+
if (!isset($this->instances[$categoryId][$cacheKey])) {
110111
/** @var Category $category */
111112
$category = $this->categoryFactory->create();
112113
if (null !== $storeId) {
@@ -116,9 +117,9 @@ public function get($categoryId, $storeId = null)
116117
if (!$category->getId()) {
117118
throw NoSuchEntityException::singleField('id', $categoryId);
118119
}
119-
$this->instances[$categoryId] = $category;
120+
$this->instances[$categoryId][$cacheKey] = $category;
120121
}
121-
return $this->instances[$categoryId];
122+
return $this->instances[$categoryId][$cacheKey];
122123
}
123124

124125
/**

app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\CatalogUrlRewrite\Model\Category\CurrentUrlRewritesRegenerator;
1212
use Magento\CatalogUrlRewrite\Service\V1\StoreViewService;
1313
use Magento\Store\Model\Store;
14+
use Magento\Catalog\Api\CategoryRepositoryInterface;
1415

1516
class CategoryUrlRewriteGenerator
1617
{
@@ -32,30 +33,39 @@ class CategoryUrlRewriteGenerator
3233
/** @var \Magento\CatalogUrlRewrite\Model\Category\ChildrenUrlRewriteGenerator */
3334
protected $childrenUrlRewriteGenerator;
3435

36+
/**
37+
* @var bool
38+
*/
39+
protected $overrideStoreUrls;
40+
3541
/**
3642
* @param \Magento\CatalogUrlRewrite\Model\Category\CanonicalUrlRewriteGenerator $canonicalUrlRewriteGenerator
3743
* @param \Magento\CatalogUrlRewrite\Model\Category\CurrentUrlRewritesRegenerator $currentUrlRewritesRegenerator
3844
* @param \Magento\CatalogUrlRewrite\Model\Category\ChildrenUrlRewriteGenerator $childrenUrlRewriteGenerator
3945
* @param \Magento\CatalogUrlRewrite\Service\V1\StoreViewService $storeViewService
46+
* @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository
4047
*/
4148
public function __construct(
4249
CanonicalUrlRewriteGenerator $canonicalUrlRewriteGenerator,
4350
CurrentUrlRewritesRegenerator $currentUrlRewritesRegenerator,
4451
ChildrenUrlRewriteGenerator $childrenUrlRewriteGenerator,
45-
StoreViewService $storeViewService
52+
StoreViewService $storeViewService,
53+
CategoryRepositoryInterface $categoryRepository
4654
) {
4755
$this->storeViewService = $storeViewService;
4856
$this->canonicalUrlRewriteGenerator = $canonicalUrlRewriteGenerator;
4957
$this->childrenUrlRewriteGenerator = $childrenUrlRewriteGenerator;
5058
$this->currentUrlRewritesRegenerator = $currentUrlRewritesRegenerator;
59+
$this->categoryRepository = $categoryRepository;
5160
}
5261

5362
/**
5463
* {@inheritdoc}
5564
*/
56-
public function generate($category)
65+
public function generate($category, $overrideStoreUrls = false)
5766
{
5867
$this->category = $category;
68+
$this->overrideStoreUrls = $overrideStoreUrls;
5969

6070
$storeId = $this->category->getStoreId();
6171
$urls = $this->isGlobalScope($storeId)
@@ -75,16 +85,44 @@ protected function generateForGlobalScope()
7585
{
7686
$urls = [];
7787
$categoryId = $this->category->getId();
78-
foreach ($this->category->getStoreIds() as $id) {
79-
if (!$this->isGlobalScope($id)
80-
&& !$this->storeViewService->doesEntityHaveOverriddenUrlKeyForStore($id, $categoryId, Category::ENTITY)
88+
foreach ($this->category->getStoreIds() as $storeId) {
89+
if (!$this->isGlobalScope($storeId)
90+
&& $this->isOverrideUrlsForStore($storeId, $categoryId)
8191
) {
82-
$urls = array_merge($urls, $this->generateForSpecificStoreView($id));
92+
$this->updateCategoryUrlForStore($storeId);
93+
$urls = array_merge($urls, $this->generateForSpecificStoreView($storeId));
8394
}
8495
}
8596
return $urls;
8697
}
8798

99+
/**
100+
* @param int $storeId
101+
* @param int $categoryId
102+
* @return bool
103+
*/
104+
protected function isOverrideUrlsForStore($storeId, $categoryId)
105+
{
106+
return $this->overrideStoreUrls
107+
|| !$this->storeViewService->doesEntityHaveOverriddenUrlKeyForStore(
108+
$storeId,
109+
$categoryId,
110+
Category::ENTITY
111+
);
112+
}
113+
114+
/**
115+
* Override url key and url path for category in specific Store
116+
*
117+
* @param int $storeId
118+
* @return void
119+
*/
120+
protected function updateCategoryUrlForStore($storeId)
121+
{
122+
$category = $this->categoryRepository->get($this->category->getId(), $storeId);
123+
$this->category->addData(['url_key' => $category->getUrlKey(), 'url_path' => $category->getUrlPath()]);
124+
}
125+
88126
/**
89127
* Check is global scope
90128
*

app/code/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteMovingObserver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
6161
);
6262
$category->setData('save_rewrites_history', $saveRewritesHistory);
6363
$urlRewrites = array_merge(
64-
$this->categoryUrlRewriteGenerator->generate($category),
64+
$this->categoryUrlRewriteGenerator->generate($category, true),
6565
$this->urlRewriteHandler->generateProductUrlRewrites($category)
6666
);
6767
$this->urlRewriteHandler->deleteCategoryRewritesForChildren($category);

app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/CategoryUrlRewriteGeneratorTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class CategoryUrlRewriteGeneratorTest extends \PHPUnit_Framework_TestCase
3131
/** @var \Magento\Catalog\Model\Category|\PHPUnit_Framework_MockObject_MockObject */
3232
protected $category;
3333

34+
/** @var \Magento\Catalog\Api\CategoryRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */
35+
protected $categoryRepository;
36+
3437
/**
3538
* Test method
3639
*/
@@ -48,6 +51,7 @@ protected function setUp()
4851
$this->storeViewService = $this->getMockBuilder('Magento\CatalogUrlRewrite\Service\V1\StoreViewService')
4952
->disableOriginalConstructor()->getMock();
5053
$this->category = $this->getMock('Magento\Catalog\Model\Category', [], [], '', false);
54+
$this->categoryRepository = $this->getMock('Magento\Catalog\Api\CategoryRepositoryInterface');
5155

5256
$this->categoryUrlRewriteGenerator = (new ObjectManager($this))->getObject(
5357
'Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator',
@@ -56,6 +60,7 @@ protected function setUp()
5660
'childrenUrlRewriteGenerator' => $this->childrenUrlRewriteGenerator,
5761
'currentUrlRewritesRegenerator' => $this->currentUrlRewritesRegenerator,
5862
'storeViewService' => $this->storeViewService,
63+
'categoryRepository' => $this->categoryRepository,
5964
]
6065
);
6166
}
@@ -84,6 +89,14 @@ public function testGenerationForGlobalScope()
8489
->setStoreId(3);
8590
$this->currentUrlRewritesRegenerator->expects($this->any())->method('generate')
8691
->will($this->returnValue([$current]));
92+
$categoryForSpecificStore = $this->getMock(
93+
'Magento\Catalog\Model\Category',
94+
['getUrlKey', 'getUrlPath'],
95+
[],
96+
'',
97+
false
98+
);
99+
$this->categoryRepository->expects($this->once())->method('get')->willReturn($categoryForSpecificStore);
87100

88101
$this->assertEquals(
89102
[$canonical, $children, $current],

0 commit comments

Comments
 (0)