Skip to content
Merged
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
26 changes: 24 additions & 2 deletions src/Model/UrlRewriteService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
use Emico\AttributeLanding\Api\LandingPageRepositoryInterface;
use Emico\AttributeLanding\Api\UrlRewriteGeneratorInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
use Magento\UrlRewrite\Model\UrlFinderInterface;
use Magento\UrlRewrite\Model\UrlPersistInterface;
Expand Down Expand Up @@ -58,14 +60,16 @@ class UrlRewriteService
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param UrlFinderInterface $urlFinder
* @param LandingPageRepositoryInterface $landingPageRepository
* @param Config $config
*/
public function __construct(
UrlRewriteFactory $urlRewriteFactory,
StoreManagerInterface $storeManager,
UrlPersistInterface $urlPersist,
SearchCriteriaBuilder $searchCriteriaBuilder,
UrlFinderInterface $urlFinder,
LandingPageRepositoryInterface $landingPageRepository
LandingPageRepositoryInterface $landingPageRepository,
private readonly Config $config,
) {
$this->urlRewriteFactory = $urlRewriteFactory;
$this->storeManager = $storeManager;
Expand Down Expand Up @@ -244,13 +248,31 @@ private function createUrlRewrite(
? $page->getUrlRewriteRequestPath()
: $page->getUrlPath() . $suffix; // @phpstan-ignore-line

$requestPath = trim($requestPath, '/');
if ($this->shouldStripRequestPath($suffix)) {
$requestPath = trim($requestPath, '/');
}

$urlRewrite->setRequestPath($requestPath);

return $urlRewrite;
}

/**
* @param string|null $newSuffix
* @return bool
* @throws NoSuchEntityException
*/
private function shouldStripRequestPath(?string $newSuffix): bool
{
/** @var Store $store */
$store = $this->storeManager->getStore();
$suffix = $newSuffix ?? $this->config->getCategoryUrlSuffix($store);
return !(
$suffix === '/' &&
$this->config->isAppendCategoryUrlSuffix($store)
);
}

protected function getActiveStoreIds(UrlRewriteGeneratorInterface $landingPage): array
{
// phpcs:disable SlevomatCodingStandard.Functions.StrictCall.NonStrictComparison
Expand Down