LanguageSwitcherLinksAlterer.php in Facets 8
File
src/LanguageSwitcherLinksAlterer.php
View source
<?php
namespace Drupal\facets;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Url;
use Drupal\facets\UrlProcessor\UrlProcessorPluginManager;
class LanguageSwitcherLinksAlterer {
protected $languageManager;
protected $cacheBackend;
protected $entityTypeManager;
protected $urlProcessorManager;
protected $data = [];
public function __construct(LanguageManagerInterface $languageManager, CacheBackendInterface $cacheBackend, EntityTypeManagerInterface $entityTypeManager, UrlProcessorPluginManager $urlProcessorManager) {
$this->languageManager = $languageManager;
$this->cacheBackend = $cacheBackend;
$this->entityTypeManager = $entityTypeManager;
$this->urlProcessorManager = $urlProcessorManager;
}
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 ($translated_alias === NULL) {
$translated_alias = $url_aliases[$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);
}
}
}
}
protected function initializeData() {
$cache = $this->cacheBackend
->get('facets_language_switcher_links');
if ($cache) {
$this->data = $cache->data;
return;
}
$data = [];
$facets = $this->entityTypeManager
->getStorage('facets_facet')
->loadMultipleOverrideFree();
$cache_tags = [];
foreach ($facets as $facet) {
$cache_tags = Cache::mergeTags($cache_tags, $facet
->getCacheTags());
$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()) {
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);
}
}