Last active
January 15, 2019 11:14
-
-
Save hadl/394a54ea4d3f9edfe0d8abd16e98bae4 to your computer and use it in GitHub Desktop.
Pimcore 5 Basic Language Switcher with Twig Extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% for link, text in get_localized_links(document) %} | |
{% set isActive = (app.request.locale is defined and app.request.locale == text) ? 'class="is-active"' : '' %} | |
<a href="{{ link }}" {{ isActive|raw }}>{{ text|upper }}</a>{% if not loop.last %} |{% endif %} | |
{% endfor %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Provides get_localized_links function in the Twig. | |
* | |
* Class LanguageSwitcherExtension | |
*/ | |
class LanguageSwitcherExtension extends \Twig_Extension | |
{ | |
/** | |
* Pimcore documents service which provide needed methods. | |
* | |
* @var Document\Service | |
*/ | |
private $documentService; | |
/** | |
* Request helper instance. | |
* | |
* @var RequestHelper | |
*/ | |
private $requestHelper; | |
/** | |
* Localized links array | |
* | |
* @var array | |
*/ | |
private $localizedLinks = []; | |
/** | |
* Rel alternate languages links array | |
* | |
* @var array | |
*/ | |
private $relAlternateLinks = []; | |
/** | |
* Initialize needed services for extension. | |
* | |
* @param Document\Service $documentService | |
* @param RequestHelper $requestHelper | |
*/ | |
public function __construct(Document\Service $documentService, RequestHelper $requestHelper) | |
{ | |
$this->documentService = $documentService; | |
$this->requestHelper = $requestHelper; | |
} | |
/** | |
* Provides language links array. | |
* | |
* @param Document $document | |
* | |
* @return array | |
*/ | |
public function getLocalizedLinks(Document $document): array | |
{ | |
if (count($this->localizedLinks)) { | |
return $this->localizedLinks; | |
} | |
$translations = $this->documentService->getTranslations($document); | |
$links = []; | |
foreach (Tool::getValidLanguages() as $language) { | |
$target = '/' . $language; | |
if (isset($translations[$language])) { | |
$localizedDocument = Document::getById($translations[$language]); | |
if ($localizedDocument) { | |
$target = $localizedDocument->getFullPath(); | |
} | |
} | |
$links[$target] = $language; | |
} | |
return $links; | |
} | |
/** | |
* Provides alternative versions of document (translated in other languages) | |
* | |
* @param Document $document | |
* | |
* @return array | |
*/ | |
public function getRelAlternate(Document $document) | |
{ | |
if ($this->getRelAlternateLinks()) { | |
return $this->getRelAlternateLinks(); | |
} | |
$translations = $this->documentService->getTranslations($document); | |
$result = []; | |
foreach ($translations as $language => $documentId) { | |
if ($this->requestHelper->getRequest()->getLocale() == $language) { | |
continue; | |
} | |
$localizedDocument = Document::getById($documentId); | |
if ($localizedDocument instanceof Document) { | |
$result[$language] = $localizedDocument->getFullPath(); | |
} | |
} | |
return $result; | |
} | |
/** | |
* Returns a list of functions to add to the existing list. | |
* | |
* @return \Twig_SimpleFunction[] | |
*/ | |
public function getFunctions(): array | |
{ | |
return [ | |
new \Twig_Function('get_localized_links', [$this, 'getLocalizedLinks']), | |
new \Twig_Function('get_rel_alternate', [$this, 'getRelAlternate']) | |
]; | |
} | |
/** | |
* setLocalizedLinks to use in lang switch | |
* | |
* @param array $localizedLinks | |
* | |
* @return LanguageSwitcherExtension | |
*/ | |
public function setLocalizedLinks(array $localizedLinks): LanguageSwitcherExtension | |
{ | |
$this->localizedLinks = $localizedLinks; | |
return $this; | |
} | |
/** | |
* set links for rel alternate | |
* | |
* @param array $relAlternateLinks | |
* | |
* @return LanguageSwitcherExtension | |
*/ | |
public function setRelAlternateLinks(array $relAlternateLinks): LanguageSwitcherExtension | |
{ | |
foreach ($relAlternateLinks as $language => $link) { | |
if ($this->requestHelper->getRequest()->getLocale() == $language) { | |
continue; | |
} | |
$this->relAlternateLinks[$language] = $link; | |
} | |
return $this; | |
} | |
/** | |
* getRelAlternateLinks | |
* | |
* @return array | |
*/ | |
public function getRelAlternateLinks(): array | |
{ | |
return $this->relAlternateLinks; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment