You are here

public function LanguageSwitcherLinksAlterer::alter in Facets 8

Alters the language switcher links.

Parameters

array $links: The links.

$type: The language type.

\Drupal\Core\Url $url: The URL the switch links will be relative to.

See also

facets_language_switch_links_alter()

File

src/LanguageSwitcherLinksAlterer.php, line 88

Class

LanguageSwitcherLinksAlterer
Helper service that alters the language switcher links.

Namespace

Drupal\facets

Code

public function alter(array &$links, $type, Url $url) {
  if (!$this->data) {
    $this
      ->initializeData();
  }
  $current_language = $this->languageManager
    ->getCurrentLanguage();
  foreach ($links as &$link) {
    if (empty($link['language']) || !$link['language'] instanceof LanguageInterface || $link['language']
      ->getId() === $current_language
      ->getId()) {
      continue;
    }
    foreach ($this->data as $facet_id => $facet_info) {
      $filter_key = $facet_info['filter_key'];
      $separator = $facet_info['separator'];
      $url_aliases = $facet_info['url_aliases'];
      $original_language = $url_aliases['original'];
      if (!isset($link['query'][$filter_key]) || !is_array($link['query'][$filter_key])) {
        continue;
      }
      $untranslated_alias = $url_aliases[$this->languageManager
        ->getCurrentLanguage()
        ->getId()];
      $translated_alias = $url_aliases[$link['language']
        ->getId()];

      // If we don't have a translated alias, that means we're trying to create
      // a link to the original language.
      if ($translated_alias === NULL) {
        $translated_alias = $url_aliases[$original_language];
      }

      // If we don't have an untranslated alias, we're trying to create a link
      // from the original language.
      if ($untranslated_alias === NULL) {
        $untranslated_alias = $url_aliases[$original_language];
      }
      foreach ($link['query'][$filter_key] as &$filters) {
        $filters = preg_replace('/(' . $untranslated_alias . "){$separator}/", $translated_alias . $separator, $filters);
      }
    }
  }
}