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-3866: Update available flag options in paragraphs. #1368

Merged
merged 2 commits into from
Jan 12, 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
4 changes: 4 additions & 0 deletions modules/oe_theme_helper/oe_theme_helper.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ services:
oe_theme_helper.external_links:
class: Drupal\oe_theme_helper\ExternalLinks
arguments: ['@config.factory']
oe_theme_helper.flag_options_subscriber:
class: Drupal\oe_theme_helper\EventSubscriber\FlagOptionsEventSubscriber
tags:
- { name: event_subscriber }
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types = 1);

namespace Drupal\oe_theme_helper\EventSubscriber;

use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\oe_paragraphs\Event\FlagOptionsEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Event subscriber to alter the list of allowed flag icons.
*/
class FlagOptionsEventSubscriber implements EventSubscriberInterface {

use StringTranslationTrait;

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
FlagOptionsEvent::class => 'alterFlagOptions',
];
}

/**
* Alter the list of allowed flag icons.
*
* @param \Drupal\oe_paragraphs\Event\FlagOptionsEvent $event
* The event.
*/
public function alterFlagOptions(FlagOptionsEvent $event): void {
$options = $event->getFlagOptions();
$options['albania'] = $this->t('Albania');
$options['bosnia-and-herzegovina'] = $this->t('Bosnia and Herzegovina');
$options['georgia'] = $this->t('Georgia');
$options['iceland'] = $this->t('Iceland');
$options['moldova'] = $this->t('Moldova');
$options['montenegro'] = $this->t('Montenegro');
$options['north-macedonia'] = $this->t('North Macedonia');
$options['norway'] = $this->t('Norway');
$options['serbia'] = $this->t('Serbia');
$options['switzerland'] = $this->t('Switzerland');
$options['turkey'] = $this->t('Turkey');
$options['ukraine'] = $this->t('Ukraine');
ksort($options);
$event->setFlagOptions($options);
}

}
24 changes: 24 additions & 0 deletions modules/oe_theme_helper/src/TwigExtension/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,21 @@ protected function getIconPath(array $context, string $icon): string {
'spain',
'sweden',
];
// Flag icon names for non EU members.
$flag_icons_non_members = [
'albania',
'bosnia-and-herzegovina',
'georgia',
'iceland',
'moldova',
'montenegro',
'north-macedonia',
'norway',
'serbia',
'switzerland',
'turkey',
'ukraine',
];
// Flag icons can have a -square string appended, so check if the icon name
// starts with a country name.
$found_icon = array_filter($flag_icons, function ($var) use ($icon) {
Expand All @@ -415,6 +430,15 @@ protected function getIconPath(array $context, string $icon): string {
if ($found_icon) {
return $context['ecl_icon_flag_path'];
}
$found_icon = array_filter($flag_icons_non_members, function ($var) use ($icon) {
if (strpos($icon, $var) === 0) {
return TRUE;
};
return FALSE;
});
if ($found_icon) {
return $context['ecl_icon_flag_non_members_path'];
}

// Social media icon names.
$social_icons = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ public function testToEclIcon(string $icon_name, array $expected_icon_array, str
'ecl_icon_path' => '/path/to/theme/resources/icons/',
'ecl_icon_social_media_path' => '/path/to/theme/resources/social-media-icons/',
'ecl_icon_flag_path' => '/path/to/theme/resources/flag-icons/',
'ecl_icon_flag_non_members_path' => '/path/to/theme/resources/non-members-flag-icons/',
];
// We join the resulting array from to_ecl_icon() function so that we have
// a visual representation of the array being returned by the function.
Expand Down
1 change: 1 addition & 0 deletions oe_theme.theme
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ function oe_theme_preprocess(&$variables) {
$variables['ecl_social_icon_path'] = base_path() . \Drupal::service('extension.list.theme')->getPath('oe_theme') . '/dist/ec/images/social-icons/sprites/icons-social.svg';
$variables['ecl_icon_social_media_path'] = $variables['ecl_images_path'] . '/icons-social-media/sprites/icons-social-media.svg';
$variables['ecl_icon_flag_path'] = $variables['ecl_images_path'] . '/icons-flag/sprites/icons-flag.svg';
$variables['ecl_icon_flag_non_members_path'] = $variables['ecl_images_path'] . '/icons-flag/sprites/icons-flag-non-members.svg';
$variables['current_language_id'] = \Drupal::languageManager()->getCurrentLanguage()->getId();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function testIllustrationListFlagsRendering(): void {

$paragraph = Paragraph::create([
'type' => 'oe_illustration_item_flag',
'field_oe_flag' => 'finland',
'field_oe_flag' => 'north-macedonia',
]);
$paragraph->save();
$items[] = $paragraph;
Expand Down Expand Up @@ -135,7 +135,7 @@ public function testIllustrationListFlagsRendering(): void {
'icon' => 'france',
'value' => 'Highlighted Term 3',
], [
'icon' => 'finland',
'icon' => 'north-macedonia',
],
],
'centered' => FALSE,
Expand Down Expand Up @@ -164,7 +164,7 @@ public function testIllustrationListFlagsRendering(): void {
'icon' => 'france-square',
'value' => 'Highlighted Term 3',
], [
'icon' => 'finland-square',
'icon' => 'north-macedonia-square',
],
],
];
Expand Down