Skip to content
Merged
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
33 changes: 33 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace CustomGento\DefaultStoreCodeRemover\Model;

use Magento\Framework\App\Config\ScopeConfigInterface;

class Config
{
public const XML_PATH_USE_PER_STORE_CONFIG = 'web/url/use_per_store_config';
Comment thread
norgeindian marked this conversation as resolved.
public const XML_PATH_STORES_WITHOUT_STORE_CODE = 'web/url/stores_without_store_code';

public function __construct(private readonly ScopeConfigInterface $scopeConfig)
{
}

public function isPerStoreConfigEnabled(): bool
{
return $this->scopeConfig->isSetFlag(self::XML_PATH_USE_PER_STORE_CONFIG);
}

public function getStoreIdsWithoutStoreCode(): array
{
$value = $this->scopeConfig->getValue(self::XML_PATH_STORES_WITHOUT_STORE_CODE);

if (empty($value)) {
return [];
}

return array_map('intval', explode(',', $value));
}
}
24 changes: 21 additions & 3 deletions Plugin/Store/HideDefaultStoreCodePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,34 @@

namespace CustomGento\DefaultStoreCodeRemover\Plugin\Store;

use CustomGento\DefaultStoreCodeRemover\Model\Config;
use Magento\Store\Model\Store;

class HideDefaultStoreCodePlugin
{
public function afterIsUseStoreInUrl(Store $subject, bool $resultIsUseInUrl): bool
public function __construct(private readonly Config $config)
{
if ($subject->getCode() !== Store::ADMIN_CODE && $subject->isDefault()) {
}

public function afterIsUseStoreInUrl(Store $subject, bool $storeCodeShallBeIncludedInUrl): bool
{
if (!$storeCodeShallBeIncludedInUrl) {
return $storeCodeShallBeIncludedInUrl;
}

if ($subject->getCode() === Store::ADMIN_CODE) {
return $storeCodeShallBeIncludedInUrl;
}

if ($this->config->isPerStoreConfigEnabled()) {
Comment thread
norgeindian marked this conversation as resolved.
return !in_array((int)$subject->getId(), $this->config->getStoreIdsWithoutStoreCode(), true);
}

// Default / legacy behaviour: only strip the code for the default store
if ($subject->isDefault()) {
return false;
}

return $resultIsUseInUrl;
return $storeCodeShallBeIncludedInUrl;
}
}
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

Default Store Code Remover for Magento 2 hides the store code in the default store from the URL. The module is in effect only when <code>web/url/use_store</code> is enabled and is meant to be used for multistore setups, where the default shop should not contain any store code, whereas all other stores should.

The module also provides an optional, extended configuration to control store code removal **per store view**:

- Configuration path: **Store > Configuration > Genral > Web**
- Setting: **Configure Store Code Removal Per Store** (Yes/No)
- Default value: **No** (backwards compatible)
- Uses the original behavior: the store code is removed from the **default store view** URL, while all other store views keep their store code in the URL.
- If set to **Yes**
- An additional setting becomes available with a list of **store views**.
- Only the **selected store views** will have the store code removed from the URL.
- All **non-selected store views** will keep the store code in the URL.

## Installation

* <code>composer require customgento/module-default-store-code-remover-m2</code>
Expand All @@ -16,13 +27,13 @@ When the store codes in the url are disabled, the URL of the stores looks like t
When the store codes in the url are enabled, the URL of the stores looks like this: `https://website.com/store_code/`.

When this extension is installed and enabled, and the store codes are enabled to be shown in the url under
Store > Configuration > General > Web >Add Store Code, the module will not show the store code for the default store
Store > Configuration > General > Web >Add Store Code, the module will not show the store code for the default store
but will show it for any other store.

So, for the default store the url will be `https://website.com/` while for all other store, the URL will be : `https://website.com/store_code`

## License
[OSL - Open Software Licence 3.0](https://opensource.org/licenses/osl-3.0.php)
[OSL - Open Software Licence 3.0](https://opensource.org/licenses/osl--3.0.php)

## Copyright
&copy; 2021 - present CustomGento GmbH
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "customgento/module-default-store-code-remover-m2",
"description": "Default Store Code Remover for Magento 2 hides the store code in the default store from the URL.",
"type": "magento2-module",
"version":"3.0.0",
"license": "OSL-3.0",
"authors": [
{
Expand All @@ -10,7 +11,7 @@
}
],
"require": {
"php": "~7.3.0||~7.4.0||~8.1.0||~8.2.0||~8.3.0||~8.4.0",
"php": "~8.1.0||~8.2.0||~8.3.0||~8.4.0",
"magento/module-store": "~101.0"
},
"suggest": {
Expand Down
27 changes: 27 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<section id="web">
<group id="url">
<field id="use_per_store_config" translate="label comment" type="select" sortOrder="15" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Configure Store Code Removal Per Store</label>
<comment><![CDATA[<strong>No (default):</strong> The default store has its store code removed from the URL; all other stores keep it.<br/>
<strong>Yes:</strong> You choose individually which stores have their store code removed.]]></comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<depends>
<field id="use_store">1</field>
</depends>
</field>
<field id="stores_without_store_code" translate="label comment" type="multiselect" sortOrder="16" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Stores Without Store Code in URL</label>
<comment>Select every store whose store code should be removed from the URL.</comment>
<source_model>Magento\Config\Model\Config\Source\Store</source_model>
<depends>
<field id="use_store">1</field>
<field id="use_per_store_config">1</field>
</depends>
</field>
</group>
</section>
</system>
</config>
11 changes: 11 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<web>
<url>
<use_per_store_config>0</use_per_store_config>
<stores_without_store_code></stores_without_store_code>
</url>
</web>
</default>
</config>
Loading