Skip to content

Commit

Permalink
Merge pull request #173 from City-of-Helsinki/UHF-8909
Browse files Browse the repository at this point in the history
UHF-8909: Preprocess refactoring
  • Loading branch information
khalima authored Jul 30, 2024
2 parents 53fd2f2 + 115ce4d commit a4bf1a6
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 2 deletions.
21 changes: 21 additions & 0 deletions documentation/default-languages.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
```
20 changes: 20 additions & 0 deletions helfi_api_base.module
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'];
}
}
2 changes: 1 addition & 1 deletion src/Language/DefaultLanguageResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
40 changes: 40 additions & 0 deletions tests/src/Kernel/Language/DefaultLanguageVariablesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Drupal\Tests\helfi_api_base\Kernel\Language;

use Drupal\KernelTests\KernelTestBase;

/**
* Tests helfi_api_base_template_preprocess_default_variables_alter().
*
* @group helfi_api_base
*/
class DefaultLanguageVariablesTest extends KernelTestBase {

/**
* {@inheritdoc}
*/
protected static $modules = [
'helfi_api_base',
];

/**
* Test helfi_api_base_template_preprocess_default_variables_alter().
*/
public function testDefaultLanguageVariables(): void {
// Prepare variables for the page template.
$render_array = [
'#theme' => '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']);
}

}
3 changes: 2 additions & 1 deletion tests/src/Unit/ApiClient/ApiClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit a4bf1a6

Please sign in to comment.