diff --git a/src/CoreShop/Bundle/FrontendBundle/Resources/config/services.yml b/src/CoreShop/Bundle/FrontendBundle/Resources/config/services.yml index 86a76c49ba..c513fc7723 100755 --- a/src/CoreShop/Bundle/FrontendBundle/Resources/config/services.yml +++ b/src/CoreShop/Bundle/FrontendBundle/Resources/config/services.yml @@ -21,6 +21,7 @@ services: - '@Pimcore\Model\Document\Service' - '@CoreShop\Component\Core\Context\ShopperContextInterface' - '@Symfony\Component\HttpFoundation\RequestStack' + - '@router' tags: - { name: twig.extension } diff --git a/src/CoreShop/Bundle/FrontendBundle/Twig/LocaleSwitcherExtension.php b/src/CoreShop/Bundle/FrontendBundle/Twig/LocaleSwitcherExtension.php index 6ec58c300b..3c607fc74c 100644 --- a/src/CoreShop/Bundle/FrontendBundle/Twig/LocaleSwitcherExtension.php +++ b/src/CoreShop/Bundle/FrontendBundle/Twig/LocaleSwitcherExtension.php @@ -23,9 +23,12 @@ use Pimcore\Model\DataObject\Data\UrlSlug; use Pimcore\Model\Document; use Pimcore\Model\Site; +use Pimcore\Model\Staticroute; use Pimcore\Tool; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Component\Routing\RouterInterface; use Twig\Extension\AbstractExtension; use Twig\TwigFunction; @@ -35,6 +38,7 @@ public function __construct( private Document\Service $documentService, private ShopperContextInterface $shopperContext, private RequestStack $requestStack, + private RouterInterface $router, ) { } @@ -84,16 +88,31 @@ public function getLocalizedLinks(Document $document): array continue; } - if (isset($translations[$language])) { - $localizedDocument = Document::getById($translations[$language]); + $link = ''; + if ($this->getMainRequest()->attributes->get('pimcore_request_source') === 'staticroute') { + $route = $this->getMainRequest()->attributes->get('_route'); + $staticRoute = Staticroute::getByName($route); + $params = []; + if ( str_contains($staticRoute->getVariables(), '_locale') ) { + $params = ['_locale' => $language]; + } + $link = $this->router->generate($route, $params); } else { - $localizedDocument = Document::getByPath($target); + if ( isset($translations[$language]) ) { + $localizedDocument = Document::getById($translations[$language]); + } else { + $localizedDocument = Document::getByPath($target); + } + + if ( $localizedDocument instanceof Document && $localizedDocument->getPublished() ) { + $link = $localizedDocument->getFullPath(); + } } - if ($localizedDocument instanceof Document && $localizedDocument->getPublished()) { + if (!empty($link)) { $links[] = [ 'language' => $language, - 'target' => $localizedDocument->getFullPath(), + 'target' => $link, 'displayLanguage' => \Locale::getDisplayLanguage($language, $language), ]; }