Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjusted locale switcher for internal shop pages without translations #2261

Merged
merged 4 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ services:
- '@Pimcore\Model\Document\Service'
- '@CoreShop\Component\Core\Context\ShopperContextInterface'
- '@Symfony\Component\HttpFoundation\RequestStack'
- '@router'
tags:
- { name: twig.extension }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -35,6 +38,7 @@ public function __construct(
private Document\Service $documentService,
private ShopperContextInterface $shopperContext,
private RequestStack $requestStack,
private RouterInterface $router,
) {
}

Expand Down Expand Up @@ -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),
];
}
Expand Down