You are here

protected function LanguageSwitcherLinksAlterer::initializeData in Facets 8

Initializes the data needed for altering the language switcher links.

It runs through all the facets on the site and all the languages and creates a cache of the URL aliases for all the languages.

1 call to LanguageSwitcherLinksAlterer::initializeData()
LanguageSwitcherLinksAlterer::alter in src/LanguageSwitcherLinksAlterer.php
Alters the language switcher links.

File

src/LanguageSwitcherLinksAlterer.php, line 141

Class

LanguageSwitcherLinksAlterer
Helper service that alters the language switcher links.

Namespace

Drupal\facets

Code

protected function initializeData() {
  $cache = $this->cacheBackend
    ->get('facets_language_switcher_links');
  if ($cache) {
    $this->data = $cache->data;
    return;
  }
  $data = [];

  /** @var \Drupal\facets\FacetInterface[] $facets */
  $facets = $this->entityTypeManager
    ->getStorage('facets_facet')
    ->loadMultipleOverrideFree();
  $cache_tags = [];
  foreach ($facets as $facet) {
    $cache_tags = Cache::mergeTags($cache_tags, $facet
      ->getCacheTags());

    /** @var \Drupal\facets\UrlProcessor\UrlProcessorInterface $urlProcessor */
    $id = $facet
      ->getFacetSourceConfig()
      ->getUrlProcessorName();
    $url_processor = $this->urlProcessorManager
      ->createInstance($id, [
      'facet' => $facet,
    ]);
    if (!isset($data[$facet
      ->id()])) {
      $data[$facet
        ->id()] = [
        'separator' => $url_processor
          ->getSeparator(),
        'filter_key' => $facet
          ->getFacetSourceConfig()
          ->getFilterKey(),
        'url_aliases' => [
          'original' => $facet
            ->language()
            ->getId(),
          $facet
            ->language()
            ->getId() => $facet
            ->getUrlAlias(),
        ],
      ];
    }
    foreach ($this->languageManager
      ->getLanguages() as $language) {
      if ($language
        ->getId() === $facet
        ->language()
        ->getId()) {

        // Skip the original facet language as it's covered above.
        continue;
      }
      $config_name = 'facets.facet.' . $facet
        ->id();
      $translated_alias = $this->languageManager
        ->getLanguageConfigOverride($language
        ->getId(), $config_name)
        ->get('url_alias');
      $data[$facet
        ->id()]['url_aliases'][$language
        ->getId()] = $translated_alias;
    }
  }
  $this->data = $data;
  $this->cacheBackend
    ->set('facets_language_switcher_links', $data, Cache::PERMANENT, $cache_tags);
}