Skip to content

Commit 463b97a

Browse files
committed
Allows to unset path for websites with single store
1 parent 9114294 commit 463b97a

File tree

6 files changed

+66
-25
lines changed

6 files changed

+66
-25
lines changed

Model/Config.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
use Magento\Directory\Helper\Data;
1010
use Magento\Framework\App\Config\ScopeConfigInterface;
11-
use Magento\Framework\App\ScopeInterface as AppScopeInterface;
1211
use Magento\Framework\Serialize\SerializerInterface;
1312
use Magento\Store\Api\Data\StoreInterface;
1413
use Magento\Store\Model\ScopeInterface;
@@ -17,8 +16,9 @@
1716

1817
class Config
1918
{
20-
private const CONFIG_PATH_USE_STORE_PATH = 'web/url/use_store_path';
19+
private const CONFIG_PATH_STORE_PATH_URL = 'web/url/store_path_url';
2120
private const CONFIG_PATH_CUSTOM_PATH_MAPPER = 'web/url/custom_path_mapper';
21+
private const CONFIG_PATH_UNSET_SINGLE_STORE_PATH = 'web/url/unset_single_store_path';
2222

2323
private ?array $customPathMapper = null;
2424

@@ -35,24 +35,29 @@ public function isEnabled(): bool
3535

3636
public function getStorePathType(): PathType
3737
{
38-
return PathType::from($this->scopeConfig->getValue(self::CONFIG_PATH_USE_STORE_PATH));
38+
return PathType::from($this->scopeConfig->getValue(self::CONFIG_PATH_STORE_PATH_URL));
3939
}
4040

41-
public function getCountry(AppScopeInterface|StoreInterface $scope): string
41+
public function isUnsetSingleStorePath(): bool
42+
{
43+
return $this->scopeConfig->isSetFlag(self::CONFIG_PATH_UNSET_SINGLE_STORE_PATH);
44+
}
45+
46+
public function getCountry(StoreInterface $store): string
4247
{
4348
return (string)$this->scopeConfig->getValue(
4449
Data::XML_PATH_DEFAULT_COUNTRY,
4550
ScopeInterface::SCOPE_STORE,
46-
$scope->getId()
51+
$store->getId()
4752
);
4853
}
4954

50-
public function getLocale(AppScopeInterface|StoreInterface $scope): string
55+
public function getLocale(StoreInterface $store): string
5156
{
5257
return (string)$this->scopeConfig->getValue(
5358
Data::XML_PATH_DEFAULT_LOCALE,
5459
ScopeInterface::SCOPE_STORE,
55-
$scope->getId()
60+
$store->getId()
5661
);
5762
}
5863

Plugin/Url/Scope.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Magento\Framework\Url\ScopeInterface;
1010
use Magento\Framework\UrlInterface;
11+
use Magento\Store\Api\Data\StoreInterface;
1112
use Opengento\StorePathUrl\Model\Config;
1213
use Opengento\StorePathUrl\Service\UriUtils;
1314

@@ -24,7 +25,7 @@ public function afterGetBaseUrl(
2425
string $type = UrlInterface::URL_TYPE_LINK,
2526
?bool $secure = null
2627
): string {
27-
return $type === UrlInterface::URL_TYPE_LINK && $this->config->isEnabled()
28+
return $type === UrlInterface::URL_TYPE_LINK && $subject instanceof StoreInterface && $this->config->isEnabled()
2829
? $this->uriUtils->replaceScopeCode($baseUrl, $subject)
2930
: $baseUrl;
3031
}

Service/PathResolver.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
namespace Opengento\StorePathUrl\Service;
88

9-
use Magento\Framework\App\ScopeInterface;
9+
use Magento\Framework\Exception\NoSuchEntityException;
1010
use Magento\Store\Api\Data\StoreInterface;
11+
use Magento\Store\Model\Store;
1112
use Opengento\StorePathUrl\Model\Config;
1213
use Opengento\StorePathUrl\Model\Config\PathType;
1314

@@ -19,15 +20,28 @@ class PathResolver
1920
{
2021
public function __construct(private Config $config) {}
2122

22-
public function resolve(ScopeInterface|StoreInterface $scope): string
23+
public function resolve(StoreInterface $store): string
2324
{
25+
if ($store instanceof Store && $this->isSingleStore($store) && $this->config->isUnsetSingleStorePath()) {
26+
return '';
27+
}
28+
2429
return strtolower(match ($this->config->getStorePathType()) {
25-
PathType::StoreCode => $scope->getCode(),
26-
PathType::CountryCode => $this->config->getCountry($scope),
27-
PathType::LanguageCode => strtok($this->config->getLocale($scope), '_'),
28-
PathType::LocaleUnderscore => $this->config->getLocale($scope),
29-
PathType::LocaleHyphen => str_replace('_', '-', $this->config->getLocale($scope)),
30-
PathType::Custom => $this->config->getCustomPathMapper()[(int)$scope->getId()] ?? $scope->getCode(),
30+
PathType::StoreCode => $store->getCode(),
31+
PathType::CountryCode => $this->config->getCountry($store),
32+
PathType::LanguageCode => strtok($this->config->getLocale($store), '_'),
33+
PathType::LocaleUnderscore => $this->config->getLocale($store),
34+
PathType::LocaleHyphen => str_replace('_', '-', $this->config->getLocale($store)),
35+
PathType::Custom => $this->config->getCustomPathMapper()[(int)$store->getId()] ?? '',
3136
});
3237
}
38+
39+
private function isSingleStore(Store $store): bool
40+
{
41+
try {
42+
return $store->getWebsiteId() && $store->getWebsite()->getStoresCount() === 1;
43+
} catch (NoSuchEntityException) {
44+
return true;
45+
}
46+
}
3347
}

Service/UriUtils.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
namespace Opengento\StorePathUrl\Service;
88

9-
use Magento\Framework\App\ScopeInterface;
109
use Magento\Store\Api\Data\StoreInterface;
1110

1211
use function ltrim;
@@ -22,12 +21,12 @@ class UriUtils
2221
{
2322
public function __construct(private PathResolver $pathResolver) {}
2423

25-
public function replaceScopeCode(string $url, ScopeInterface|StoreInterface $scope): string
24+
public function replaceScopeCode(string $url, StoreInterface $scope): string
2625
{
2726
return $this->replaceLeadingPath($scope->getCode(), $this->pathResolver->resolve($scope), $url);
2827
}
2928

30-
public function replacePathCode(string $url, ScopeInterface|StoreInterface $scope): string
29+
public function replacePathCode(string $url, StoreInterface $scope): string
3130
{
3231
return $this->replaceLeadingPath($this->pathResolver->resolve($scope), $scope->getCode(), $url);
3332
}
@@ -37,7 +36,12 @@ private function replaceLeadingPath(string $search, string $replace, string $uri
3736
$path = parse_url($uri, PHP_URL_PATH) ?? '/';
3837

3938
return $path !== '/' && str_starts_with(ltrim($path, '/'), ltrim($search, '/'))
40-
? str_replace($path, substr_replace($path, $replace, (int)str_starts_with($path, '/'), strlen($search)), $uri)
39+
? str_replace($path, $this->replacePath($search, $replace, $path), $uri)
4140
: $uri;
4241
}
42+
43+
private function replacePath(string $search, string $replace, string $path): string
44+
{
45+
return str_replace('//', '/', substr_replace($path, $replace, (int)str_starts_with($path, '/'), strlen($search)));
46+
}
4347
}

etc/adminhtml/system.xml

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@
99
<system>
1010
<section id="web">
1111
<group id="url">
12-
<field id="use_store_path" type="select" translate="label comment" showInDefault="1" showInWebsite="0" showInStore="0" sortOrder="15" canRestore="1">
13-
<label>Custom Store Path Url</label>
12+
<field id="use_store" translate="label comment">
13+
<label>Add Store Path to Urls</label>
14+
<comment>
15+
<![CDATA[<strong class="colorRed">Warning!</strong> When using Store Path in URLs, in some cases system may not work properly if URLs without Store Path are specified in the third-party services (e.g. PayPal etc.).]]>
16+
</comment>
17+
</field>
18+
<field id="store_path_url" type="select" translate="label" showInDefault="1" showInWebsite="0" showInStore="0" sortOrder="15" canRestore="1">
19+
<label>Store Path Url</label>
1420
<source_model>Opengento\StorePathUrl\Model\Config\Source\PathTypes</source_model>
15-
<config_path>web/url/use_store_path</config_path>
21+
<config_path>web/url/store_path_url</config_path>
1622
<depends>
1723
<field id="use_store">1</field>
1824
</depends>
@@ -24,8 +30,18 @@
2430
<config_path>web/url/custom_path_mapper</config_path>
2531
<depends>
2632
<field id="use_store">1</field>
27-
<field id="use_store_path">custom</field>
33+
<field id="store_path_url">custom</field>
34+
</depends>
35+
</field>
36+
<field id="unset_single_store_path" type="select" translate="label comment" showInDefault="1" showInWebsite="0" showInStore="0" sortOrder="15" canRestore="1">
37+
<label>Unset Path for Single Store</label>
38+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
39+
<config_path>web/url/unset_single_store_path</config_path>
40+
<depends>
41+
<field id="use_store">1</field>
42+
<field id="store_path_url" negative="1">custom</field>
2843
</depends>
44+
<comment>When enabled, websites with a single store won't use the Store Path in URLs.</comment>
2945
</field>
3046
</group>
3147
</section>

etc/config.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
<default>
1010
<web>
1111
<url>
12-
<use_store_path>store_code</use_store_path>
12+
<store_path_url>store_code</store_path_url>
13+
<unset_single_store_path>1</unset_single_store_path>
1314
</url>
1415
</web>
1516
</default>

0 commit comments

Comments
 (0)