Skip to content

Commit

Permalink
Merge pull request #292 from City-of-Helsinki/UHF-8909
Browse files Browse the repository at this point in the history
UHF-8909 Preprocess function refactor
  • Loading branch information
khalima authored Aug 30, 2024
2 parents f5a07d2 + d1e78d4 commit ec3dd35
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 20 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"conflict": {
"drupal/gin": ">3.0.0-rc11",
"drupal/helfi_platform_config": "<4.3"
"drupal/helfi_platform_config": "<4.3",
"drupal/helfi_api_base": "<2.7.6"
}
}
19 changes: 5 additions & 14 deletions hdbt_admin.theme
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,9 @@ declare(strict_types=1);

use Drupal\Core\Entity\EntityFormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\media\MediaInterface;

/**
* Implements hook_preprocess().
*/
function hdbt_admin_preprocess(array &$variables):void {
$language = Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT);
$variables['current_langcode'] = $language->getId();
$variables['current_language'] = $language->getName();
}

/**
* Implements hook_preprocess_input().
*/
Expand Down Expand Up @@ -423,16 +413,17 @@ function hdbt_admin_preprocess_media(array &$variables): void {
$variables['media'] instanceof MediaInterface &&
$variables['media']->bundle() === 'image'
) {
$current_language = $variables['current_langcode'];
$language_id = $variables['language']->getId();
$route = \Drupal::routeMatch();
if (!empty($route->getParameter('target'))) {
$current_language = $route->getParameter('target')->getId();
$language_id = $route->getParameter('target')->getId();
}

if ($variables['media']->hasTranslation($current_language)) {
if ($variables['media']->hasTranslation($language_id)) {
// @phpstan-ignore-next-line
$alt = $variables['media']
->getTranslation($current_language)->field_media_image->alt;
->getTranslation($language_id)
->field_media_image->alt;
$variables['alt_text'] = $alt !== '""' ? $alt : FALSE;
}
}
Expand Down
10 changes: 5 additions & 5 deletions templates/navigation/links--language-block.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
{% if links %}
<div {{ attributes.addClass('language-switcher') }}>
<button id="language-switcher-toggle" class="language-switcher__button" aria-expanded="false" aria-controls="language-switcher-dropdown" aria-expanded="false" aria-haspopup="true" aria-label="{{ 'Select language'|t }}">
{% if current_language %}
<span class="label" aria-label="{{ 'Current language is'|t }} {{ current_language|capitalize }}">{{ current_language|capitalize }}</span>
{% if language.name %}
<span class="label" aria-label="{{ 'Current language is'|t }} {{ language.name|capitalize }}">{{ language.name|capitalize }}</span>
{% endif %}
</button>
<div aria-hidden="true" id="language-switcher-dropdown" class="language-switcher__dropdown">
Expand All @@ -51,12 +51,12 @@
{# Check if link is part of alternative menu and set class accordingly. #}
{% set classes = classes|merge([alternative_language ? 'is-alternative' : '']) %}

{% if not untranslated and lang != current_langcode %}
{% if not untranslated and lang != language.id %}
{% set language_link = path('<current>', {}, {'language': item.link['#options']['language']}) %}
{% set element = 'a' %}
{% elseif lang == current_langcode %}
{% elseif lang == language.id %}
{% set element = 'span' %}
{% set classes = classes|merge([lang == current_langcode ? 'is-active' : '']) %}
{% set classes = classes|merge([lang == language.id ? 'is-active' : '']) %}
{% else %}
{% set element = 'span' %}
{% set classes = classes|merge(['is-disabled']) %}
Expand Down

0 comments on commit ec3dd35

Please sign in to comment.