Skip to content

Commit

Permalink
[Bug]: Fix language switcher (#524)
Browse files Browse the repository at this point in the history
* fix language switcher

* improve readibility

* fix translated links

* improve params

* refactor to not use constructor propertypromotion

* get localized category name

* refactor to use constructor injection
  • Loading branch information
kingjia90 authored Jul 20, 2023
1 parent 1e02d20 commit 9573b1e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 22 deletions.
59 changes: 46 additions & 13 deletions src/Twig/Extension/LanguageSwitcherExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@

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;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
use Pimcore\Model\DataObject;

class LanguageSwitcherExtension extends AbstractExtension
{
Expand All @@ -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();

Expand All @@ -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] = [
Expand Down
4 changes: 2 additions & 2 deletions src/Website/LinkGenerator/AbstractProductLinkGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
9 changes: 6 additions & 3 deletions src/Website/LinkGenerator/CategoryLinkGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions src/Website/LinkGenerator/NewsLinkGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions src/Website/LinkGenerator/ProductLinkGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9573b1e

Please sign in to comment.