diff --git a/composer.json b/composer.json index 9981d904c..617479dca 100644 --- a/composer.json +++ b/composer.json @@ -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", @@ -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", diff --git a/modules/oe_theme_helper/oe_theme_helper.module b/modules/oe_theme_helper/oe_theme_helper.module index 2884e49d6..a8f7470bc 100644 --- a/modules/oe_theme_helper/oe_theme_helper.module +++ b/modules/oe_theme_helper/oe_theme_helper.module @@ -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(). @@ -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', + ]; + } +} diff --git a/modules/oe_theme_helper/src/LanguageSwitcherBuilderCallback.php b/modules/oe_theme_helper/src/LanguageSwitcherBuilderCallback.php new file mode 100644 index 000000000..725d228bb --- /dev/null +++ b/modules/oe_theme_helper/src/LanguageSwitcherBuilderCallback.php @@ -0,0 +1,98 @@ +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; + } + +}