Skip to content

Commit

Permalink
Merge pull request #9 from iMi-digital/direct-link-support
Browse files Browse the repository at this point in the history
Direct link support
  • Loading branch information
DanieliMi authored May 23, 2023
2 parents 591e028 + ad1051b commit 5ac177f
Show file tree
Hide file tree
Showing 10 changed files with 435 additions and 70 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ This would allow switching between store views of websites with the id 1 and 2,
By default (if the configuration variable is not set), you can switch to all other websites. Set it to an empty string or
null to disallow switching to another website.

### Use direct links instead of redirects

You can configure the switcher to use direct links instead of the redirect method. This however only works with stores
that do not share an url. You can enable this feature either via the adminpanel under *Stores > Settings > Configuration >
IMI > Store Switch > General > Use direct Links* or by cli:

```bash
n98-magerun2 config:store:set imi_store_switch/general/use_direct_links 1
```

# Installing

The easiest way to install the module is from [packagist](https://packagist.org/packages/imi/magento2-store-switch-all-store-views) by running
Expand Down Expand Up @@ -77,11 +87,17 @@ You might want to try this layout update in `default.xml` of your child theme:
<arguments>
<argument name="view_model" xsi:type="object">IMI\StoreSwitch\ViewModel\StoreSwitchModel</argument>
</arguments>
<action method="setTemplate" ifconfig="imi_store_switch/general/use_direct_links">
<argument name="template" xsi:type="string">IMI_StoreSwitch::switch/direct/languages-porto.phtml</argument>
</action>
</block>
</referenceContainer>
</body>
</page>
```
## Hyvä Theme

This has partial support for the Hyvä Theme, without warranty.

# License

Expand Down
164 changes: 94 additions & 70 deletions ViewModel/StoreSwitchModel.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php


namespace IMI\StoreSwitch\ViewModel;


use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Locale\TranslatedLists;
use Magento\Framework\View\Element\Block\ArgumentInterface;
Expand All @@ -13,6 +11,7 @@
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManager;
use Magento\UrlRewrite\Model\StoreSwitcher\RewriteUrl;

class StoreSwitchModel implements ArgumentInterface
{
Expand All @@ -26,6 +25,10 @@ class StoreSwitchModel implements ArgumentInterface

const AVAILABLE_WEB_SITES_CONFIG_PATH = 'imi_store_switch/general/available_web_sites';

private const USE_DIRECT_LINKS = 'imi_store_switch/general/use_direct_links';

protected RewriteUrl $storeSwitcher;

/**
* @var WebsiteCollectionFactory
*/
Expand All @@ -46,42 +49,18 @@ class StoreSwitchModel implements ArgumentInterface
*/
private $storeManager;

/**
* LanguageSwitchModel constructor.
*
* @param WebsiteCollectionFactory $websiteCollectionFactory
* @param ScopeConfigInterface $scopeConfig
* @param TranslatedLists $translatedLists
* @param StoreManager $storeManager
*/
public function __construct(
WebsiteCollectionFactory $websiteCollectionFactory,
ScopeConfigInterface $scopeConfig,
TranslatedLists $translatedLists,
StoreManager $storeManager
StoreManager $storeManager,
RewriteUrl $storeSwitcher,
) {
$this->websiteCollectionFactory = $websiteCollectionFactory;
$this->scopeConfig = $scopeConfig;
$this->translatedLists = $translatedLists;
$this->storeManager = $storeManager;
}

/**
* Is the module functionality enabled for current or passed store.
*
* @param null $store
*
* @return bool
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function isEnabled($store = null): bool
{
$configValue = $this->scopeConfig->getValue(
self::MODULE_ENABLED_CONFIG_PATH,
ScopeInterface::SCOPE_STORE,
$store ?? $this->getCurrentStore()->getId());

return boolval($configValue);
$this->scopeConfig = $scopeConfig;
$this->translatedLists = $translatedLists;
$this->storeManager = $storeManager;
$this->storeSwitcher = $storeSwitcher;
}

/**
Expand All @@ -100,47 +79,106 @@ public function isEnabledOnOtherStores(): bool
}
}
}

return false;
}

/**
* @return StoreInterface|string|null
* @throws \Magento\Framework\Exception\NoSuchEntityException
* Get collection of websites.
*
* @return WebsiteCollection
*/
public function getCurrentStore(): StoreInterface
public function getWebsites(): WebsiteCollection
{
return $this->storeManager->getStore();
$collection = $this->websiteCollectionFactory->create();

$enabledIds = $this->getEnabledWebsitesForCurrentWebsite();

if ($enabledIds === null) {
return $collection;
}

return $collection->addIdFilter($enabledIds);
}

private function getEnabledWebsitesForCurrentWebsite(): ?array
{
$enabledWebsites = $this->scopeConfig->getValue(
self::AVAILABLE_WEB_SITES_CONFIG_PATH,
ScopeInterface::SCOPE_WEBSITE);
ScopeInterface::SCOPE_WEBSITE
);

if($enabledWebsites === null) {
if ($enabledWebsites === null) {
return null;
}

return explode(',', $enabledWebsites);
}

/**
* Get collection of websites.
* Is the module functionality enabled for current or passed store.
*
* @return WebsiteCollection
* @param null $store
*
* @return bool
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getWebsites(): WebsiteCollection
public function isEnabled($store = null): bool
{
$collection = $this->websiteCollectionFactory->create();
$configValue = $this->scopeConfig->getValue(
self::MODULE_ENABLED_CONFIG_PATH,
ScopeInterface::SCOPE_STORE,
$store ?? $this->getCurrentStore()->getId()
);

$enabledIds = $this->getEnabledWebsitesForCurrentWebsite();
return boolval($configValue);
}

if($enabledIds === null) {
return $collection;
/**
* @return StoreInterface|string|null
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getCurrentStore(): StoreInterface
{
return $this->storeManager->getStore();
}

/**
* Get the formatted label for the dropdown, based on the format configuration.
*
* @param StoreInterface $store
*
* @return string
* @throws \Exception
*/
public function getStoreSwitchLabel(StoreInterface $store): string
{
$showCountryOnly = $this->scopeConfig->getValue(
self::MODULE_SHOW_COUNTRY_ONLY_CONFIG_PATH,
ScopeInterface::SCOPE_STORE,
$store->getId()
);
if ($showCountryOnly) {
return $this->getStoreCountyCode($store);
}

return $collection->addIdFilter($enabledIds);
return $this->getParsedLanguage($store) . '&nbsp;(' . $this->getStoreCountyCode($store) . ')';
}

/**
* Get country code for given store.
*
* @param StoreInterface $store
*
* @return string
*/
public function getStoreCountyCode(StoreInterface $store): string
{
return $this->scopeConfig->getValue(
self::DEFAULT_COUNTRY_CONFIG_PATH,
ScopeInterface::SCOPE_STORE,
$store->getId()
);
}

/**
Expand Down Expand Up @@ -168,7 +206,7 @@ public function getParsedLanguage(StoreInterface $store): string
*/
private function getStoreLocaleLabel(StoreInterface $store): string
{
$locales = $this->translatedLists->getOptionLocales();
$locales = $this->translatedLists->getOptionLocales();
$storeLocale = $this->getStoreLocale($store);

foreach ($locales as $locale) {
Expand All @@ -191,33 +229,19 @@ private function getStoreLocale(StoreInterface $store): string
return $this->scopeConfig->getValue(self::LOCALE_CONFIG_PATH, ScopeInterface::SCOPE_STORE, $store->getId());
}

/**
* Get country code for given store.
*
* @param StoreInterface $store
*
* @return string
*/
public function getStoreCountyCode(StoreInterface $store): string
public function useDirectLinks(): bool
{
return $this->scopeConfig->getValue(self::DEFAULT_COUNTRY_CONFIG_PATH, ScopeInterface::SCOPE_STORE,
$store->getId());
return boolval($this->scopeConfig->getValue(self::USE_DIRECT_LINKS, ScopeInterface::SCOPE_STORES));
}

/**
* Get the formatted label for the dropdown, based on the format configuration.
*
* @param StoreInterface $store
*
* @return string
* @throws \Exception
*/
public function getStoreSwitchLabel(StoreInterface $store): string
public function getDirectLink(Store $_store, string $currentUrl): string
{
$showCountryOnly = $this->scopeConfig->getValue(self::MODULE_SHOW_COUNTRY_ONLY_CONFIG_PATH, ScopeInterface::SCOPE_STORE, $store->getId());
if($showCountryOnly) {
return $this->getStoreCountyCode($store);
$newUrl = $this->storeSwitcher->switch($this->storeManager->getStore(), $_store, $currentUrl);

if ($newUrl === $currentUrl) {
return $_store->getBaseUrl();
}
return $this->getParsedLanguage($store).'&nbsp;('.$this->getStoreCountyCode($store).')';

return $newUrl;
}
}
5 changes: 5 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
<source_model>Magento\Config\Model\Config\Source\Website</source_model>
<comment>This option lets you restrict the which store views can be switched between (on a web site level). It specifies all available web sites for the current scope. If you don't select any website, switching will not be restricted, letting you switch between all store views of all web sites.</comment>
</field>
<field id="use_direct_links" translate="label" type="select" sortOrder="10" showInDefault="1" showInStore="1" showInWebsite="1" canRestore="1">
<label>Use direct Links</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>Use direct links instead of Magentos redirect chains.</comment>
</field>
</group>
</section>
</system>
Expand Down
1 change: 1 addition & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<enable>0</enable>
<show_country_only>0</show_country_only>
<available_web_sites></available_web_sites>
<use_direct_links>0</use_direct_links>
</general>
</imi_store_switch>
</default>
Expand Down
3 changes: 3 additions & 0 deletions view/frontend/layout/default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
<argument name="id_modifier" xsi:type="string">nav</argument>
<argument name="view_model" xsi:type="object">IMI\StoreSwitch\ViewModel\StoreSwitchModel</argument>
</arguments>
<action method="setTemplate" ifconfig="imi_store_switch/general/use_direct_links">
<argument name="template" xsi:type="string">IMI_StoreSwitch::switch/direct/languages.phtml</argument>
</action>
</referenceBlock>
</referenceBlock>
</referenceBlock>
Expand Down
10 changes: 10 additions & 0 deletions view/frontend/layout/hyva_default.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="store-language-switcher" template="IMI_StoreSwitch::switch/languages-hyva.phtml">
<action method="setTemplate" ifconfig="imi_store_switch/general/use_direct_links">
<argument name="template" xsi:type="string">IMI_StoreSwitch::switch/direct/languages-hyva.phtml</argument>
</action>
</referenceBlock>
</body>
</page>
Loading

0 comments on commit 5ac177f

Please sign in to comment.