-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '2.x' into EWPP-1278-2.x-merge-in
- Loading branch information
Showing
3 changed files
with
117 additions
and
2 deletions.
There are no files selected for viewing
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
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
98 changes: 98 additions & 0 deletions
98
modules/oe_theme_helper/src/LanguageSwitcherBuilderCallback.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Drupal\oe_theme_helper; | ||
|
||
use Drupal\Core\Cache\CacheableMetadata; | ||
use Drupal\Core\Entity\ContentEntityInterface; | ||
use Drupal\Core\Security\TrustedCallbackInterface; | ||
|
||
/** | ||
* Provides a trusted callback to alter the content language switcher. | ||
* | ||
* @see oe_theme_helper_block_view_oe_theme_helper_page_header_alter() | ||
*/ | ||
class LanguageSwitcherBuilderCallback implements TrustedCallbackInterface { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function trustedCallbacks(): array { | ||
return ['preRender']; | ||
} | ||
|
||
/** | ||
* Pre-render callback for the Page Header block alteration. | ||
* | ||
* We use this to add the language switcher | ||
* to the page header if OpenEuropa Theme is being used. | ||
* | ||
* @param array $build | ||
* The built render array of the block. | ||
* | ||
* @see \Drupal\oe_theme_helper\Plugin\Block\PageHeaderBlock | ||
* | ||
* @return array | ||
* The built render array of the block. | ||
*/ | ||
public static function preRender(array $build): array { | ||
// Get required services. | ||
$multilingual_helper = \Drupal::service('oe_multilingual.helper'); | ||
$content_language_switcher_provider = \Drupal::service('oe_multilingual.content_language_switcher_provider'); | ||
$language_manager = \Drupal::languageManager(); | ||
$cache = CacheableMetadata::createFromRenderArray($build); | ||
$cache->addCacheContexts(['languages:language_content']); | ||
|
||
$entity = $multilingual_helper->getEntityFromCurrentRoute(); | ||
// Bail out if there is no entity or if it's not a content entity. | ||
if (!$entity || !$entity instanceof ContentEntityInterface) { | ||
$cache->applyTo($build); | ||
return $build; | ||
} | ||
|
||
$cache->addCacheableDependency($entity); | ||
$cache->applyTo($build); | ||
|
||
// Render the links only if the current entity translation language is not | ||
// the same as the current site language. | ||
/** @var \Drupal\Core\Entity\EntityInterface $translation */ | ||
$translation = $multilingual_helper->getCurrentLanguageEntityTranslation($entity); | ||
$current_language = $language_manager->getCurrentLanguage(); | ||
if ($translation->language()->getId() === $current_language->getId()) { | ||
return $build; | ||
} | ||
|
||
$content = &$build['content']; | ||
|
||
$content['#language_switcher']['current'] = $translation->language()->getName(); | ||
|
||
/** @var \Drupal\Core\Language\LanguageInterface[] $languages */ | ||
$languages = $language_manager->getNativeLanguages(); | ||
$content['#language_switcher']['unavailable'] = $languages[$current_language->getId()]->getName(); | ||
|
||
// Normalize the links to an array of options suitable for the ECL | ||
// "ecl-lang-select-pages" template. | ||
$content['#language_switcher']['options'] = []; | ||
foreach ($content_language_switcher_provider->getAvailableEntityLanguages($entity) as $language_code => $link) { | ||
/** @var \Drupal\Core\Url $url */ | ||
$url = $link['url']; | ||
$href = $url | ||
->setOptions(['language' => $link['language']]) | ||
->setAbsolute(TRUE) | ||
->toString(); | ||
|
||
$content['#language_switcher']['options'][] = [ | ||
'href' => $href, | ||
'hreflang' => $language_code, | ||
'label' => $link['title'], | ||
'lang' => $language_code, | ||
]; | ||
} | ||
|
||
$content['#language_switcher']['is_primary'] = TRUE; | ||
|
||
return $build; | ||
} | ||
|
||
} |