Skip to content

Instantly share code, notes, and snippets.

@hadl
Last active January 15, 2019 11:14
Show Gist options
  • Save hadl/394a54ea4d3f9edfe0d8abd16e98bae4 to your computer and use it in GitHub Desktop.
Save hadl/394a54ea4d3f9edfe0d8abd16e98bae4 to your computer and use it in GitHub Desktop.

Revisions

  1. hadl revised this gist Jan 15, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion LanguageSwitcherExtension.php
    Original file line number Diff line number Diff line change
    @@ -62,7 +62,7 @@ public function getLocalizedLinks(Document $document): array

    $links = [];
    foreach (Tool::getValidLanguages() as $language) {
    $target = '';
    $target = '/' . $language;
    if (isset($translations[$language])) {
    $localizedDocument = Document::getById($translations[$language]);
    if ($localizedDocument) {
  2. hadl created this gist Jan 15, 2019.
    165 changes: 165 additions & 0 deletions LanguageSwitcherExtension.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,165 @@
    /**
    * 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 = '';
    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;
    }
    }
    4 changes: 4 additions & 0 deletions language-switcher.html.twig
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    {% 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 %}