Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EWPP-3777: Add language code to quote pattern and paragraph. #1367

Merged
merged 3 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions modules/oe_theme_helper/src/TwigExtension/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
58 changes: 58 additions & 0 deletions modules/oe_theme_helper/tests/src/Unit/TwigExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
];
}

}
5 changes: 3 additions & 2 deletions templates/paragraphs/paragraph--oe-quote.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}) }}
5 changes: 5 additions & 0 deletions templates/patterns/blockquote/blockquote.ui_patterns.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
13 changes: 10 additions & 3 deletions templates/patterns/blockquote/pattern-blockquote.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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([])
} %}
2 changes: 1 addition & 1 deletion tests/src/Kernel/Paragraphs/ParagraphsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 3 additions & 2 deletions tests/src/Kernel/fixtures/rendering.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down