From a96ec435c165d5e097d61dcb90253a7455133d3f Mon Sep 17 00:00:00 2001
From: escuriola <escuriola@gmail.com>
Date: Wed, 16 Feb 2022 18:27:40 +0100
Subject: [PATCH] OEL-1160: Add language filter to twig.

---
 .../src/TwigExtension/TwigExtension.php       | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/modules/oe_whitelabel_helper/src/TwigExtension/TwigExtension.php b/modules/oe_whitelabel_helper/src/TwigExtension/TwigExtension.php
index d900fe6a..e91d12f9 100644
--- a/modules/oe_whitelabel_helper/src/TwigExtension/TwigExtension.php
+++ b/modules/oe_whitelabel_helper/src/TwigExtension/TwigExtension.php
@@ -6,6 +6,7 @@ namespace Drupal\oe_whitelabel_helper\TwigExtension;
 
 use Drupal\Core\Cache\CacheableDependencyInterface;
 use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
+use Drupal\oe_whitelabel_helper\EuropeanUnionLanguages;
 use Twig\Extension\AbstractExtension;
 use Twig\TwigFilter;
 use Twig\TwigFunction;
@@ -35,6 +36,7 @@ class TwigExtension extends AbstractExtension {
   public function getFilters(): array {
     return [
       new TwigFilter('bcl_timeago', [$this, 'bclTimeAgo']),
+      new TwigFilter('to_internal_language_id', [$this, 'toInternalLanguageId']),
     ];
   }
 
@@ -176,4 +178,21 @@ class TwigExtension extends AbstractExtension {
     return $block_plugin->build();
   }
 
+  /**
+   * Get an internal language ID given its code.
+   *
+   * @param string $language_code
+   *   The language code as defined by the W3C language tags document.
+   *
+   * @return string
+   *   The internal language ID, or the given language code if none found.
+   */
+  public function toInternalLanguageId($language_code): string {
+    if (EuropeanUnionLanguages::hasLanguage($language_code)) {
+      return EuropeanUnionLanguages::getInternalLanguageCode($language_code);
+    }
+
+    return $language_code;
+  }
+
 }
-- 
GitLab