From 738d977fea986e9ab13145c53b25ee00ed84f454 Mon Sep 17 00:00:00 2001 From: Sergiy Osipchuk Date: Wed, 26 Apr 2023 14:10:37 +0300 Subject: [PATCH 1/4] Adjusted locale switcher for internal shop pages without translations --- .../Resources/config/services.yml | 1 + .../Twig/LocaleSwitcherExtension.php | 23 +++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/CoreShop/Bundle/FrontendBundle/Resources/config/services.yml b/src/CoreShop/Bundle/FrontendBundle/Resources/config/services.yml index 86a76c49ba..8248ab9324 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' + - '@service_container' tags: - { name: twig.extension } diff --git a/src/CoreShop/Bundle/FrontendBundle/Twig/LocaleSwitcherExtension.php b/src/CoreShop/Bundle/FrontendBundle/Twig/LocaleSwitcherExtension.php index 6ec58c300b..2abbc87911 100644 --- a/src/CoreShop/Bundle/FrontendBundle/Twig/LocaleSwitcherExtension.php +++ b/src/CoreShop/Bundle/FrontendBundle/Twig/LocaleSwitcherExtension.php @@ -24,6 +24,7 @@ use Pimcore\Model\Document; use Pimcore\Model\Site; use Pimcore\Tool; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; use Twig\Extension\AbstractExtension; @@ -35,6 +36,7 @@ public function __construct( private Document\Service $documentService, private ShopperContextInterface $shopperContext, private RequestStack $requestStack, + protected ContainerInterface $container, ) { } @@ -84,16 +86,27 @@ 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'); + $link = $this->container->get('router')->generate($route, ['_locale' => $language]); + } 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), ]; } From 985045e1e90c256b2039be302f5c3b5cce738e4f Mon Sep 17 00:00:00 2001 From: Sergiy Osipchuk Date: Wed, 26 Apr 2023 15:33:23 +0300 Subject: [PATCH 2/4] Adjusted locale switcher for internal shop pages without translations --- .../FrontendBundle/Twig/LocaleSwitcherExtension.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/CoreShop/Bundle/FrontendBundle/Twig/LocaleSwitcherExtension.php b/src/CoreShop/Bundle/FrontendBundle/Twig/LocaleSwitcherExtension.php index 2abbc87911..9fcec1e0b3 100644 --- a/src/CoreShop/Bundle/FrontendBundle/Twig/LocaleSwitcherExtension.php +++ b/src/CoreShop/Bundle/FrontendBundle/Twig/LocaleSwitcherExtension.php @@ -23,6 +23,7 @@ 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; @@ -89,8 +90,12 @@ public function getLocalizedLinks(Document $document): array $link = ''; if ($this->getMainRequest()->attributes->get('pimcore_request_source') === 'staticroute') { $route = $this->getMainRequest()->attributes->get('_route'); - $link = $this->container->get('router')->generate($route, ['_locale' => $language]); - + $staticRoute = Staticroute::getByName($route); + $params = []; + if (strpos($staticRoute->getVariables(), '_locale')) { + $params = ['_locale' => $language]; + } + $link = $this->container->get('router')->generate($route, $params); } else { if ( isset($translations[$language]) ) { $localizedDocument = Document::getById($translations[$language]); From 7f966ab627c4d87ee159819c1bc5f455fad4ed95 Mon Sep 17 00:00:00 2001 From: Sergiy Osipchuk Date: Thu, 27 Apr 2023 10:53:01 +0300 Subject: [PATCH 3/4] Adjusted locale switcher for internal shop pages without translations --- .../Bundle/FrontendBundle/Twig/LocaleSwitcherExtension.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CoreShop/Bundle/FrontendBundle/Twig/LocaleSwitcherExtension.php b/src/CoreShop/Bundle/FrontendBundle/Twig/LocaleSwitcherExtension.php index 9fcec1e0b3..34856256dd 100644 --- a/src/CoreShop/Bundle/FrontendBundle/Twig/LocaleSwitcherExtension.php +++ b/src/CoreShop/Bundle/FrontendBundle/Twig/LocaleSwitcherExtension.php @@ -92,7 +92,7 @@ public function getLocalizedLinks(Document $document): array $route = $this->getMainRequest()->attributes->get('_route'); $staticRoute = Staticroute::getByName($route); $params = []; - if (strpos($staticRoute->getVariables(), '_locale')) { + if ( str_contains($staticRoute->getVariables(), '_locale') ) { $params = ['_locale' => $language]; } $link = $this->container->get('router')->generate($route, $params); From e1ee76a889c5daeb58029693cff1ede0dd40d268 Mon Sep 17 00:00:00 2001 From: Dominik Pfaffenbauer Date: Thu, 4 May 2023 07:39:45 +0200 Subject: [PATCH 4/4] [FrontendBundle] remove container injection and use router directly --- .../Bundle/FrontendBundle/Resources/config/services.yml | 2 +- .../Bundle/FrontendBundle/Twig/LocaleSwitcherExtension.php | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/CoreShop/Bundle/FrontendBundle/Resources/config/services.yml b/src/CoreShop/Bundle/FrontendBundle/Resources/config/services.yml index 8248ab9324..c513fc7723 100755 --- a/src/CoreShop/Bundle/FrontendBundle/Resources/config/services.yml +++ b/src/CoreShop/Bundle/FrontendBundle/Resources/config/services.yml @@ -21,7 +21,7 @@ services: - '@Pimcore\Model\Document\Service' - '@CoreShop\Component\Core\Context\ShopperContextInterface' - '@Symfony\Component\HttpFoundation\RequestStack' - - '@service_container' + - '@router' tags: - { name: twig.extension } diff --git a/src/CoreShop/Bundle/FrontendBundle/Twig/LocaleSwitcherExtension.php b/src/CoreShop/Bundle/FrontendBundle/Twig/LocaleSwitcherExtension.php index 34856256dd..3c607fc74c 100644 --- a/src/CoreShop/Bundle/FrontendBundle/Twig/LocaleSwitcherExtension.php +++ b/src/CoreShop/Bundle/FrontendBundle/Twig/LocaleSwitcherExtension.php @@ -28,6 +28,7 @@ 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; @@ -37,7 +38,7 @@ public function __construct( private Document\Service $documentService, private ShopperContextInterface $shopperContext, private RequestStack $requestStack, - protected ContainerInterface $container, + private RouterInterface $router, ) { } @@ -95,7 +96,7 @@ public function getLocalizedLinks(Document $document): array if ( str_contains($staticRoute->getVariables(), '_locale') ) { $params = ['_locale' => $language]; } - $link = $this->container->get('router')->generate($route, $params); + $link = $this->router->generate($route, $params); } else { if ( isset($translations[$language]) ) { $localizedDocument = Document::getById($translations[$language]);