From c2765ae52a0a73682259a61157cbc09438bcb230 Mon Sep 17 00:00:00 2001 From: 22Alexandra Date: Mon, 8 Jan 2024 11:28:10 +0200 Subject: [PATCH 1/3] EWPP-3777: Update twig filter for internal language codes. --- .../src/TwigExtension/TwigExtension.php | 8 ++- .../tests/src/Unit/TwigExtensionTest.php | 58 +++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/modules/oe_theme_helper/src/TwigExtension/TwigExtension.php b/modules/oe_theme_helper/src/TwigExtension/TwigExtension.php index 9446ed6a8..5b5944895 100644 --- a/modules/oe_theme_helper/src/TwigExtension/TwigExtension.php +++ b/modules/oe_theme_helper/src/TwigExtension/TwigExtension.php @@ -149,11 +149,15 @@ public function toNativeLanguageName($language_code): string { * The internal language ID, or the given language code if none found. */ public function toInternalLanguageId($language_code): string { + // Map Norwegian Bokmål and Chinese language codes. + $map_other_language_codes = [ + 'nb' => 'no', + 'zh-hans' => 'zh', + ]; if (EuropeanUnionLanguages::hasLanguage($language_code)) { return EuropeanUnionLanguages::getInternalLanguageCode($language_code); } - - return $language_code; + return $map_other_language_codes[$language_code] ?? $language_code; } /** diff --git a/modules/oe_theme_helper/tests/src/Unit/TwigExtensionTest.php b/modules/oe_theme_helper/tests/src/Unit/TwigExtensionTest.php index 76eb89e65..fb40c3c39 100644 --- a/modules/oe_theme_helper/tests/src/Unit/TwigExtensionTest.php +++ b/modules/oe_theme_helper/tests/src/Unit/TwigExtensionTest.php @@ -408,4 +408,62 @@ public function testCreateMarkup() { $this->assertInstanceOf(MarkupInterface::class, $markup_object); } + /** + * Tests converting a language code to the internal language code. + * + * @param string $language_code + * The language code to filter. + * @param string $expected_internal_language_code + * The internal language code that is expected to be returned. + * + * @covers ::toInternalLanguageId + * @dataProvider toInternalLanguageIdProvider + */ + public function testToInternalLanguageId(string $language_code, string $expected_internal_language_code): void { + $result = $this->twig->render("{{ '$language_code'|to_internal_language_id }}"); + $this->assertEquals($expected_internal_language_code, $result); + } + + /** + * Returns test cases for ::testToInternalLanguageId(). + * + * @return array[] + * An array of test cases, each test case an indexed array with the + * following two values: + * 1. The language code to check. + * 2. The expected internal language code. + * + * @see ::testToInternalLanguageId() + */ + public function toInternalLanguageIdProvider(): array { + return [ + ['bg', 'bg'], + ['cs', 'cs'], + ['da', 'da'], + ['de', 'de'], + ['et', 'et'], + ['el', 'el'], + ['en', 'en'], + ['es', 'es'], + ['fr', 'fr'], + ['ga', 'ga'], + ['hr', 'hr'], + ['it', 'it'], + ['lt', 'lt'], + ['lv', 'lv'], + ['hu', 'hu'], + ['mt', 'mt'], + ['nl', 'nl'], + ['pl', 'pl'], + ['pt-pt', 'pt'], + ['ro', 'ro'], + ['sk', 'sk'], + ['sl', 'sl'], + ['fi', 'fi'], + ['sv', 'sv'], + ['nb', 'no'], + ['zh-hans', 'zh'], + ]; + } + } From a71f9eb44807f302f85fe9117d0227c6549cf150 Mon Sep 17 00:00:00 2001 From: 22Alexandra Date: Tue, 9 Jan 2024 10:59:53 +0200 Subject: [PATCH 2/3] EWPP-3777: Add language code to blockquote pattern. --- .../patterns/blockquote/blockquote.ui_patterns.yml | 5 +++++ .../blockquote/pattern-blockquote.html.twig | 13 ++++++++++--- tests/src/Kernel/fixtures/rendering.yml | 5 +++-- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/templates/patterns/blockquote/blockquote.ui_patterns.yml b/templates/patterns/blockquote/blockquote.ui_patterns.yml index f69a33d76..71887311d 100644 --- a/templates/patterns/blockquote/blockquote.ui_patterns.yml +++ b/templates/patterns/blockquote/blockquote.ui_patterns.yml @@ -19,3 +19,8 @@ blockquote: preview: src: "https://loremflickr.com/1000/500/arch" alt: "Alternative text for featured item image" + language_code: + type: "string" + label: "Language code" + description: "The language code for the blockquote - it changes the type of quotes. (defaults to 'en')" + preview: "pt-pt" diff --git a/templates/patterns/blockquote/pattern-blockquote.html.twig b/templates/patterns/blockquote/pattern-blockquote.html.twig index d9f7b57c5..e4f137556 100644 --- a/templates/patterns/blockquote/pattern-blockquote.html.twig +++ b/templates/patterns/blockquote/pattern-blockquote.html.twig @@ -5,12 +5,19 @@ */ #} {% if image %} - {% set image = { - 'path': image.src, - 'alt': image.alt + {% set _picture = { + 'img': { + 'src': image.src, + 'alt': image.alt + } } %} {% endif %} +{% if language_code|render is not empty %} + {% set _lang = language_code|render|to_internal_language_id %} +{% endif %} {% include '@ecl-twig/blockquote' with { 'author': author, 'citation': body, + 'lang': _lang|default('en'), + 'picture': _picture|default([]) } %} diff --git a/tests/src/Kernel/fixtures/rendering.yml b/tests/src/Kernel/fixtures/rendering.yml index 895fdef00..765de9531 100644 --- a/tests/src/Kernel/fixtures/rendering.yml +++ b/tests/src/Kernel/fixtures/rendering.yml @@ -2041,12 +2041,13 @@ '#fields': body: "Quote text" author: "Quote author" + language_code: "bg" assertions: count: 'img.ecl-blockquote__image': 0 equals: '.ecl-blockquote__author': "Quote author" - '.ecl-blockquote__citation': "Quote text" + '.ecl-blockquote__citation[lang="bg"]': "Quote text" - array: '#type': pattern '#id': blockquote @@ -2062,7 +2063,7 @@ 'img.ecl-blockquote__image[alt="Alternative of image"]': 1 equals: '.ecl-blockquote__author': "Quote author" - '.ecl-blockquote__citation': "Quote text" + '.ecl-blockquote__citation[lang="en"]': "Quote text" - array: '#type': pattern '#id': context_nav From 474e55c46efd9dfcc46ebe21936891633751f4b1 Mon Sep 17 00:00:00 2001 From: 22Alexandra Date: Tue, 9 Jan 2024 11:34:42 +0200 Subject: [PATCH 3/3] EWPP-3777: Add current language id to quote paragraph template. --- templates/paragraphs/paragraph--oe-quote.html.twig | 5 +++-- tests/src/Kernel/Paragraphs/ParagraphsTest.php | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/templates/paragraphs/paragraph--oe-quote.html.twig b/templates/paragraphs/paragraph--oe-quote.html.twig index 35baf2792..764ef0af0 100644 --- a/templates/paragraphs/paragraph--oe-quote.html.twig +++ b/templates/paragraphs/paragraph--oe-quote.html.twig @@ -7,6 +7,7 @@ */ #} {{ pattern('blockquote', { - 'body': content.field_oe_plain_text_long, - 'author': content.field_oe_text, + 'body': content.field_oe_plain_text_long, + 'author': content.field_oe_text, + 'language_code': current_language_id, }) }} diff --git a/tests/src/Kernel/Paragraphs/ParagraphsTest.php b/tests/src/Kernel/Paragraphs/ParagraphsTest.php index 3ef66f3ab..70b8e05fa 100644 --- a/tests/src/Kernel/Paragraphs/ParagraphsTest.php +++ b/tests/src/Kernel/Paragraphs/ParagraphsTest.php @@ -130,7 +130,7 @@ public function testQuote(array $data, array $expected): void { $crawler = new Crawler($html); - $actual = $crawler->filter('figure.ecl-blockquote .ecl-blockquote__body blockquote.ecl-blockquote__quote p.ecl-blockquote__citation')->html(); + $actual = $crawler->filter('figure.ecl-blockquote .ecl-blockquote__body blockquote.ecl-blockquote__quote p.ecl-blockquote__citation[lang="en"]')->html(); $this->assertEquals($expected['body'], trim($actual)); $actual = $crawler->filter('figure.ecl-blockquote footer.ecl-blockquote__attribution cite.ecl-blockquote__author')->text();