diff --git a/documentation/default-languages.md b/documentation/default-languages.md index b4309930..1bebf880 100644 --- a/documentation/default-languages.md +++ b/documentation/default-languages.md @@ -33,3 +33,24 @@ $language_resolver->getFallbackLangAttributes(); $language_resolver->getCurrentLangAttributes(); ``` + +## Twig + +The current language object, the alternative language boolean and possible fallback language attributes are added to each template via `helfi_api_base_template_preprocess_default_variables_alter()`. + +### Usage + +```twig +{# Get the language ID or "langcode" #} +{{ language.id }} + +{# Get the language human readable name #} +{{ language.name }} + +{# Check for alternative language. (true|false) #} +{{ alternative_language }} + +{# Get the alternative language fallback attributes. #} +{% set lang = lang_attributes.fallback_lang %} +{% set dir = lang_attributes.fallback_dir %} +``` diff --git a/helfi_api_base.module b/helfi_api_base.module index 74dd0924..178a1296 100644 --- a/helfi_api_base.module +++ b/helfi_api_base.module @@ -8,6 +8,7 @@ declare(strict_types=1); use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Language\LanguageInterface; use Drupal\helfi_api_base\Features\FeatureManager; use Drupal\helfi_api_base\Link\LinkProcessor; use Drupal\helfi_api_base\UserExpire\UserExpireManager; @@ -103,3 +104,22 @@ function helfi_api_base_cron() : void { $userExpireManager->cancelExpiredUsers(); } } + +/** + * Implements hook_template_preprocess_default_variables_alter(). + */ +function helfi_api_base_template_preprocess_default_variables_alter(array &$variables): void { + // Set the language object and alternative language attributes. + $variables['language'] = $language = Drupal::languageManager() + ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT); + + // Set alternative language fallback attributes. + $defaultLanguageResolver = Drupal::service('helfi_api_base.default_language_resolver'); + $variables['alternative_language'] = $defaultLanguageResolver->isAltLanguage($language->getId()); + + if ($variables['alternative_language']) { + $attributes = $defaultLanguageResolver->getFallbackLangAttributes(); + $variables['lang_attributes']['fallback_lang'] = $attributes['lang']; + $variables['lang_attributes']['fallback_dir'] = $attributes['dir']; + } +} diff --git a/src/Language/DefaultLanguageResolver.php b/src/Language/DefaultLanguageResolver.php index 4b11c9b2..03cc46c0 100644 --- a/src/Language/DefaultLanguageResolver.php +++ b/src/Language/DefaultLanguageResolver.php @@ -48,7 +48,7 @@ public function getDefaultLanguages() : array { * * Non-default languages use this for certain elements. * Can be configured by overriding the - * 'helfi_api_base.fallback_language' parameter in servies.yml file. + * 'helfi_api_base.fallback_language' parameter in services.yml file. * * @return string * The fallback language ID. diff --git a/tests/src/Kernel/Language/DefaultLanguageVariablesTest.php b/tests/src/Kernel/Language/DefaultLanguageVariablesTest.php new file mode 100644 index 00000000..31142243 --- /dev/null +++ b/tests/src/Kernel/Language/DefaultLanguageVariablesTest.php @@ -0,0 +1,40 @@ + 'page', + '#content' => ['#markup' => 'Test content'], + ]; + \Drupal::moduleHandler()->alter('template_preprocess_default_variables', $render_array); + \Drupal::moduleHandler()->invokeAll('preprocess_page', [&$render_array]); + + // Check if variables are set correctly. + $this->assertInstanceOf('Drupal\Core\Language\Language', $render_array['language']); + $this->assertNotNull($render_array['alternative_language']); + } + +} diff --git a/tests/src/Unit/ApiClient/ApiClientTest.php b/tests/src/Unit/ApiClient/ApiClientTest.php index 6df26736..b01d2a3a 100644 --- a/tests/src/Unit/ApiClient/ApiClientTest.php +++ b/tests/src/Unit/ApiClient/ApiClientTest.php @@ -67,7 +67,8 @@ protected function setUp() : void { parent::setUp(); $this->fixture = sprintf('%s/../../../fixtures/response.json', __DIR__); - $this->cache = new MemoryBackend(); + $time = $this->prophesize(TimeInterface::class)->reveal(); + $this->cache = new MemoryBackend($time); } /**