Skip to content

Commit 9950da0

Browse files
committed
MAGETWO-93061: CMS page of second website with same URLkey as first website, show content of First website instead of second website content.
- Add switch store to url
1 parent 4a28abb commit 9950da0

File tree

3 files changed

+92
-11
lines changed

3 files changed

+92
-11
lines changed

app/code/Magento/Cms/Block/Adminhtml/Page/Grid/Renderer/Action/UrlBuilder.php

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Cms\Block\Adminhtml\Page\Grid\Renderer\Action;
79

10+
use Magento\Framework\App\ObjectManager;
11+
use Magento\Framework\Url\EncoderInterface;
12+
use Magento\Framework\App\ActionInterface;
13+
use Magento\Store\Model\StoreManagerInterface;
14+
815
/**
916
* Url builder class used to compose dynamic urls.
1017
*/
@@ -15,12 +22,31 @@ class UrlBuilder
1522
*/
1623
protected $frontendUrlBuilder;
1724

25+
/**
26+
* @var EncoderInterface
27+
*/
28+
private $urlEncoder;
29+
30+
/**
31+
* @var StoreManagerInterface
32+
*/
33+
private $storeManager;
34+
1835
/**
1936
* @param \Magento\Framework\UrlInterface $frontendUrlBuilder
37+
* @param EncoderInterface|null $urlEncoder
38+
* @param StoreManagerInterface|null $storeManager
2039
*/
21-
public function __construct(\Magento\Framework\UrlInterface $frontendUrlBuilder)
22-
{
40+
public function __construct(
41+
\Magento\Framework\UrlInterface $frontendUrlBuilder,
42+
EncoderInterface $urlEncoder = null,
43+
StoreManagerInterface $storeManager = null
44+
) {
2345
$this->frontendUrlBuilder = $frontendUrlBuilder;
46+
$this->urlEncoder = $urlEncoder ?: ObjectManager::getInstance()
47+
->get(EncoderInterface::class);
48+
$this->storeManager = $storeManager?: ObjectManager::getInstance()
49+
->get(StoreManagerInterface::class);
2450
}
2551

2652
/**
@@ -35,12 +61,22 @@ public function getUrl($routePath, $scope, $store)
3561
{
3662
if ($scope) {
3763
$this->frontendUrlBuilder->setScope($scope);
38-
$href = $this->frontendUrlBuilder->getUrl(
64+
$targetUrl = $this->frontendUrlBuilder->getUrl(
3965
$routePath,
4066
[
4167
'_current' => false,
4268
'_nosid' => true,
43-
'_query' => [\Magento\Store\Model\StoreManagerInterface::PARAM_NAME => $store]
69+
'_query' => [
70+
StoreManagerInterface::PARAM_NAME => $store
71+
]
72+
]
73+
);
74+
$href = $this->frontendUrlBuilder->getUrl(
75+
'stores/store/switch',
76+
[
77+
'_current' => false,
78+
'_nosid' => true,
79+
'_query' => $this->prepareRequestQuery($store, $targetUrl)
4480
]
4581
);
4682
} else {
@@ -55,4 +91,25 @@ public function getUrl($routePath, $scope, $store)
5591

5692
return $href;
5793
}
94+
95+
/**
96+
* Prepare request query
97+
*
98+
* @param string $store
99+
* @param string $href
100+
* @return array
101+
*/
102+
private function prepareRequestQuery(string $store, string $href) : array
103+
{
104+
$storeView = $this->storeManager->getDefaultStoreView();
105+
$query = [
106+
StoreManagerInterface::PARAM_NAME => $store,
107+
ActionInterface::PARAM_NAME_URL_ENCODED => $this->urlEncoder->encode($href)
108+
];
109+
if ($storeView->getCode() !== $store) {
110+
$query['___from_store'] = $storeView->getCode();
111+
}
112+
113+
return $query;
114+
}
58115
}

app/code/Magento/Store/Controller/Store/SwitchAction.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Copyright © Magento, Inc. All rights reserved.
55
* See COPYING.txt for license details.
66
*/
7+
declare(strict_types=1);
78

89
namespace Magento\Store\Controller\Store;
910

@@ -18,13 +19,15 @@
1819
use Magento\Store\Model\StoreManagerInterface;
1920
use Magento\Store\Model\StoreSwitcher;
2021
use Magento\Store\Model\StoreSwitcherInterface;
22+
use Magento\Framework\App\Action\HttpPostActionInterface;
23+
use Magento\Framework\App\Action\HttpGetActionInterface;
2124

2225
/**
2326
* Handles store switching url and makes redirect.
2427
*
2528
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2629
*/
27-
class SwitchAction extends Action
30+
class SwitchAction extends Action implements HttpGetActionInterface, HttpPostActionInterface
2831
{
2932
/**
3033
* @var StoreCookieManagerInterface
@@ -89,10 +92,12 @@ public function __construct(
8992
public function execute()
9093
{
9194
$targetStoreCode = $this->_request->getParam(
92-
\Magento\Store\Model\StoreManagerInterface::PARAM_NAME,
95+
\Magento\Store\Model\StoreManagerInterface::PARAM_NAME
96+
);
97+
$fromStoreCode = $this->_request->getParam(
98+
'___from_store',
9399
$this->storeCookieManager->getStoreCodeFromCookie()
94100
);
95-
$fromStoreCode = $this->_request->getParam('___from_store');
96101

97102
$requestedUrlToRedirect = $this->_redirect->getRedirectUrl();
98103
$redirectUrl = $requestedUrlToRedirect;

app/code/Magento/UrlRewrite/Model/StoreSwitcher/RewriteUrl.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,7 @@ public function switch(StoreInterface $fromStore, StoreInterface $targetStore, s
7070
if ($oldRewrite) {
7171
$targetUrl = $targetStore->getBaseUrl();
7272
// look for url rewrite match on the target store
73-
$currentRewrite = $this->urlFinder->findOneByData([
74-
UrlRewrite::TARGET_PATH => $oldRewrite->getTargetPath(),
75-
UrlRewrite::STORE_ID => $targetStore->getId(),
76-
]);
73+
$currentRewrite = $this->findCurrentRewrite($oldRewrite, $targetStore);
7774
if ($currentRewrite) {
7875
$targetUrl .= $currentRewrite->getRequestPath();
7976
}
@@ -93,4 +90,26 @@ public function switch(StoreInterface $fromStore, StoreInterface $targetStore, s
9390
}
9491
return $targetUrl;
9592
}
93+
94+
/**
95+
* Look for url rewrite match on the target store
96+
*
97+
* @param UrlRewrite $oldRewrite
98+
* @param StoreInterface $targetStore
99+
* @return UrlRewrite|null
100+
*/
101+
private function findCurrentRewrite(UrlRewrite $oldRewrite, StoreInterface $targetStore)
102+
{
103+
$currentRewrite = $this->urlFinder->findOneByData([
104+
UrlRewrite::TARGET_PATH => $oldRewrite->getTargetPath(),
105+
UrlRewrite::STORE_ID => $targetStore->getId(),
106+
]);
107+
if (!$currentRewrite) {
108+
$currentRewrite = $this->urlFinder->findOneByData([
109+
UrlRewrite::REQUEST_PATH => $oldRewrite->getTargetPath(),
110+
UrlRewrite::STORE_ID => $targetStore->getId(),
111+
]);
112+
}
113+
return $currentRewrite;
114+
}
96115
}

0 commit comments

Comments
 (0)