Skip to content

Commit

Permalink
Merge branch '2.x' into EWPP-1278-2.x-merge-in
Browse files Browse the repository at this point in the history
  • Loading branch information
ademarco committed Jul 26, 2021
2 parents 907bb09 + 34e648d commit fb17554
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 2 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"openeuropa/oe_corporate_blocks": "~4.0",
"openeuropa/oe_corporate_countries": "~2.0",
"openeuropa/oe_media": "~1.12",
"openeuropa/oe_multilingual": "dev-master",
"openeuropa/oe_multilingual": "~1.8",
"openeuropa/oe_paragraphs": "dev-master",
"openeuropa/oe_search": "~1.0",
"openeuropa/oe_webtools": "~1.10",
Expand All @@ -66,7 +66,8 @@
"We explicitly require consolidation/annotated-command to allow lower 'composer update --prefer-lowest' to complete successfully."
],
"conflict": {
"openeuropa/oe_corporate_blocks": "<3.0.0"
"openeuropa/oe_corporate_blocks": "<3.0.0",
"openeuropa/oe_multilingual": "<1.8.0"
},
"scripts": {
"post-install-cmd": "./vendor/bin/run drupal:site-setup",
Expand Down
16 changes: 16 additions & 0 deletions modules/oe_theme_helper/oe_theme_helper.module
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\image\Plugin\Field\FieldType\ImageItem;
use Drupal\media\MediaInterface;
use Drupal\oe_theme\ValueObject\ImageValueObject;
use Drupal\oe_theme_helper\LanguageSwitcherBuilderCallback;
use Drupal\Core\Block\BlockPluginInterface;

/**
* Implements hook_page_attachments().
Expand Down Expand Up @@ -307,3 +309,17 @@ function oe_theme_helper_form_entity_view_display_edit_form_alter(&$form, FormSt
}
}
}

/**
* Implements hook_block_view_BASE_BLOCK_ID_alter().
*/
function oe_theme_helper_block_view_oe_theme_helper_page_header_alter(array &$build, BlockPluginInterface $block) {
// Add pre-render callback to build and inject the content language switcher
// within the page header block.
if (\Drupal::service('module_handler')->moduleExists('oe_multilingual')) {
$build['#pre_render'][] = [
LanguageSwitcherBuilderCallback::class,
'preRender',
];
}
}
98 changes: 98 additions & 0 deletions modules/oe_theme_helper/src/LanguageSwitcherBuilderCallback.php
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;
}

}

0 comments on commit fb17554

Please sign in to comment.