diff --git a/src/Twig/Extension/LanguageSwitcherExtension.php b/src/Twig/Extension/LanguageSwitcherExtension.php index db508d17..a4168393 100644 --- a/src/Twig/Extension/LanguageSwitcherExtension.php +++ b/src/Twig/Extension/LanguageSwitcherExtension.php @@ -17,6 +17,10 @@ namespace App\Twig\Extension; +use App\Website\LinkGenerator\AbstractProductLinkGenerator; +use App\Website\LinkGenerator\CategoryLinkGenerator; +use App\Website\LinkGenerator\NewsLinkGenerator; +use App\Website\LinkGenerator\ProductLinkGenerator; use Pimcore\Model\Document; use Pimcore\Model\Document\Service; use Pimcore\Tool; @@ -24,6 +28,7 @@ use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Twig\Extension\AbstractExtension; use Twig\TwigFunction; +use Pimcore\Model\DataObject; class LanguageSwitcherExtension extends AbstractExtension { @@ -32,25 +37,39 @@ class LanguageSwitcherExtension extends AbstractExtension */ private $documentService; - /** - * @var UrlGeneratorInterface $urlGenerator - */ private UrlGeneratorInterface $urlGenerator; - - /** - * @var RequestStack $requestStack - */ private RequestStack $requestStack; + private CategoryLinkGenerator $categoryLinkGenerator; + private NewsLinkGenerator $newsLinkGenerator; + private ProductLinkGenerator $productLinkGenerator; - public function __construct(Service $documentService, UrlGeneratorInterface $urlGenerator, RequestStack $requestStack) + public function __construct(Service $documentService, UrlGeneratorInterface $urlGenerator, RequestStack $requestStack, CategoryLinkGenerator $categoryLinkGenerator, NewsLinkGenerator $newsLinkGenerator, ProductLinkGenerator $productLinkGenerator) { $this->documentService = $documentService; $this->urlGenerator = $urlGenerator; $this->requestStack = $requestStack; + $this->categoryLinkGenerator = $categoryLinkGenerator; + $this->newsLinkGenerator = $newsLinkGenerator; + $this->productLinkGenerator = $productLinkGenerator; } public function getLocalizedLinks(Document $document): array { + $dynamicRoutesMapping = [ + 'shop-detail' => [ + 'generator' => 'productLinkGenerator', + 'requiredField' => 'product' + ], + 'shop-category' => [ + 'generator' => 'categoryLinkGenerator', + 'requiredField' => 'category' + ], + 'news-detail' => [ + 'generator' => 'newsLinkGenerator', + 'requiredField' => 'news' + ] + ]; + $translations = $this->documentService->getTranslations($document); $request = $this->requestStack->getCurrentRequest(); @@ -71,12 +90,26 @@ public function getLocalizedLinks(Document $document): array } $route = $request->get('_route'); - $routeParams = $request->get('_route_params'); - if ($route && $routeParams) { - $routeParams['_locale'] = \Locale::getPrimaryLanguage($language); - $route = $this->urlGenerator->generate($route, $routeParams); - $target = $route; + if ($route && array_key_exists($route, $dynamicRoutesMapping)) { + $routeParams = $request->get('_route_params', []); + $requiredField = $dynamicRoutesMapping[$route]['requiredField']; + + if (!array_key_exists($requiredField, $routeParams)){ + continue; + } + + $generator = $dynamicRoutesMapping[$route]['generator']; + $object = $request->get($requiredField); + + if (!is_object($object)) { + $object = DataObject::getById($object); + } + + $linkGeneratorService = $this->$generator; + if ($linkGeneratorService instanceof AbstractProductLinkGenerator) { + $target = $linkGeneratorService->generate($object, ['locale' => \Locale::getPrimaryLanguage($language)]); + } } $links[$language] = [ diff --git a/src/Website/LinkGenerator/AbstractProductLinkGenerator.php b/src/Website/LinkGenerator/AbstractProductLinkGenerator.php index 4f16ca4a..3932eada 100644 --- a/src/Website/LinkGenerator/AbstractProductLinkGenerator.php +++ b/src/Website/LinkGenerator/AbstractProductLinkGenerator.php @@ -59,7 +59,7 @@ public function __construct(DocumentResolver $documentResolver, RequestStack $re * @param Category|null $rootCategory * @return string */ - public function getNavigationPath(?Category $category, ?Category $rootCategory = null) + public function getNavigationPath(?Category $category, ?Category $rootCategory = null, $locale = null) { if (empty($rootCategory)) { if (!$this->document) { @@ -83,7 +83,7 @@ public function getNavigationPath(?Category $category, ?Category $rootCategory = } foreach ($categories as $categoryInPath) { - $path .= Text::toUrl($categoryInPath->getName()).'/'; + $path .= Text::toUrl($categoryInPath->getName($locale)).'/'; } return $path; diff --git a/src/Website/LinkGenerator/CategoryLinkGenerator.php b/src/Website/LinkGenerator/CategoryLinkGenerator.php index 38f87074..eb59a88c 100644 --- a/src/Website/LinkGenerator/CategoryLinkGenerator.php +++ b/src/Website/LinkGenerator/CategoryLinkGenerator.php @@ -40,12 +40,15 @@ public function generate(Concrete $object, array $params = [], $reset = false): $this->document = $params['document']; } + $locale = $params['locale'] ?? null; + return $this->pimcoreUrl->__invoke( [ - 'categoryname' => Text::toUrl($object->getName() ? $object->getName() : 'elements'), + 'categoryname' => Text::toUrl($object->getName($locale) ? $object->getName($locale) : 'elements'), 'category' => $object->getId(), - 'path' => $this->getNavigationPath($object, $params['rootCategory'] ?? null), - 'page' => null + 'path' => $this->getNavigationPath($object, $params['rootCategory'] ?? null, $locale), + 'page' => null, + '_locale' => $locale, ], 'shop-category', $reset diff --git a/src/Website/LinkGenerator/NewsLinkGenerator.php b/src/Website/LinkGenerator/NewsLinkGenerator.php index ca544a76..5bb80b85 100644 --- a/src/Website/LinkGenerator/NewsLinkGenerator.php +++ b/src/Website/LinkGenerator/NewsLinkGenerator.php @@ -94,11 +94,14 @@ public function generate(Concrete $object, array $params = []): string $fullPath = $document->getProperty('news_default_document') ? substr($document->getProperty('news_default_document')->getFullPath(), strlen($localeUrlPart)) : ''; } + $locale = $params['locale'] ?? null; + return $this->pimcoreUrl->__invoke( [ - 'newstitle' => Text::toUrl($object->getTitle() ? $object->getTitle() : 'news'), + 'newstitle' => Text::toUrl($object->getTitle($locale) ? $object->getTitle($locale) : 'news'), 'news' => $object->getId(), - 'path' => $fullPath + 'path' => $fullPath, + '_locale' => $locale, ], 'news-detail', true diff --git a/src/Website/LinkGenerator/ProductLinkGenerator.php b/src/Website/LinkGenerator/ProductLinkGenerator.php index 387fdc68..7b5fe6a7 100644 --- a/src/Website/LinkGenerator/ProductLinkGenerator.php +++ b/src/Website/LinkGenerator/ProductLinkGenerator.php @@ -69,12 +69,15 @@ protected function doGenerate($object, $params): string return current($object->getUrlSlug())->getSlug(); } + $locale = $params['locale'] ?? null; + return $this->pimcoreUrl->__invoke( [ 'productname' => Text::toUrl($object->getOSName() ?? 'product'), 'product' => $object->getId(), - 'path' => $this->getNavigationPath($object->getMainCategory(), $params['rootCategory'] ?? null), - 'page' => null + 'path' => $this->getNavigationPath($object->getMainCategory(), $params['rootCategory'] ?? null, $locale), + 'page' => null, + '_locale' => $locale, ], 'shop-detail', true