Skip to content

Commit bdce1b6

Browse files
committed
ENGCOM-1928: Refactoring.
1 parent 38b91b7 commit bdce1b6

File tree

4 files changed

+79
-22
lines changed

4 files changed

+79
-22
lines changed

app/code/Magento/Store/Block/Switcher.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -265,24 +265,4 @@ public function getTargetStorePostData(Store $store, $data = [])
265265
$data
266266
);
267267
}
268-
269-
/**
270-
* Returns target store redirect url.
271-
*
272-
* @param Store $store
273-
* @return string
274-
*/
275-
public function getTargetStoreRedirectUrl(Store $store)
276-
{
277-
return $this->getUrl(
278-
'stores/store/redirect',
279-
[
280-
'___store' => $store->getCode(),
281-
'___from_store' => $this->getCurrentStoreCode(),
282-
ActionInterface::PARAM_NAME_URL_ENCODED => $this->urlHelper->getEncodedUrl(
283-
$store->getCurrentUrl(false)
284-
),
285-
]
286-
);
287-
}
288268
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Store\ViewModel;
10+
11+
use Magento\Framework\App\ActionInterface;
12+
use Magento\Framework\Url\EncoderInterface;
13+
use Magento\Framework\UrlInterface;
14+
use Magento\Store\Model\Store;
15+
use Magento\Store\Model\StoreManagerInterface;
16+
17+
/**
18+
* Provides target store redirect url.
19+
*/
20+
class SwitcherUrlProvider implements \Magento\Framework\View\Element\Block\ArgumentInterface
21+
{
22+
/**
23+
* @var EncoderInterface
24+
*/
25+
private $encoder;
26+
27+
/**
28+
* @var StoreManagerInterface
29+
*/
30+
private $storeManager;
31+
32+
/**
33+
* @var UrlInterface
34+
*/
35+
private $urlBuilder;
36+
37+
/**
38+
* @param EncoderInterface $encoder
39+
* @param StoreManagerInterface $storeManager
40+
* @param UrlInterface $urlBuilder
41+
*/
42+
public function __construct(
43+
EncoderInterface $encoder,
44+
StoreManagerInterface $storeManager,
45+
UrlInterface $urlBuilder
46+
) {
47+
$this->encoder = $encoder;
48+
$this->storeManager = $storeManager;
49+
$this->urlBuilder = $urlBuilder;
50+
}
51+
52+
/**
53+
* Returns target store redirect url.
54+
*
55+
* @param Store $store
56+
* @return string
57+
* @throws \Magento\Framework\Exception\NoSuchEntityException
58+
*/
59+
public function getTargetStoreRedirectUrl(Store $store): string
60+
{
61+
return $this->urlBuilder->getUrl(
62+
'stores/store/redirect',
63+
[
64+
'___store' => $store->getCode(),
65+
'___from_store' => $this->storeManager->getStore()->getCode(),
66+
ActionInterface::PARAM_NAME_URL_ENCODED => $this->encoder->encode(
67+
$store->getCurrentUrl(false)
68+
),
69+
]
70+
);
71+
}
72+
}

app/code/Magento/Store/view/frontend/templates/switch/languages.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<?php foreach ($block->getStores() as $_lang): ?>
2828
<?php if ($_lang->getId() != $block->getCurrentStoreId()): ?>
2929
<li class="view-<?= $block->escapeHtml($_lang->getCode()) ?> switcher-option">
30-
<a href="<?= $block->escapeUrl($block->getTargetStoreRedirectUrl($_lang)) ?>">
30+
<a href="<?= $block->escapeUrl($block->getViewModel()->getTargetStoreRedirectUrl($_lang)) ?>">
3131
<?= $block->escapeHtml($_lang->getName()) ?>
3232
</a>
3333
</li>

app/code/Magento/Theme/view/frontend/layout/default.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@
3939
<argument name="label" translate="true" xsi:type="string">Skip to Content</argument>
4040
</arguments>
4141
</block>
42-
<block class="Magento\Store\Block\Switcher" name="store_language" as="store_language" template="Magento_Store::switch/languages.phtml"/>
42+
<block class="Magento\Store\Block\Switcher" name="store_language" as="store_language" template="Magento_Store::switch/languages.phtml">
43+
<arguments>
44+
<argument name="view_model" xsi:type="object">Magento\Store\ViewModel\SwitcherUrlProvider</argument>
45+
</arguments>
46+
</block>
4347
<block class="Magento\Customer\Block\Account\Navigation" name="top.links">
4448
<arguments>
4549
<argument name="css_class" xsi:type="string">header links</argument>
@@ -82,6 +86,7 @@
8286
<block class="Magento\Store\Block\Switcher" name="store.settings.language" template="Magento_Store::switch/languages.phtml">
8387
<arguments>
8488
<argument name="id_modifier" xsi:type="string">nav</argument>
89+
<argument name="view_model" xsi:type="object">Magento\Store\ViewModel\SwitcherUrlProvider</argument>
8590
</arguments>
8691
</block>
8792
<block class="Magento\Directory\Block\Currency" name="store.settings.currency" template="Magento_Directory::currency.phtml">

0 commit comments

Comments
 (0)