Skip to content

Commit

Permalink
Merge pull request #267 from openeuropa/contribution/EPHEH-318
Browse files Browse the repository at this point in the history
EPHEH-318: Add Color Scheme.
  • Loading branch information
drishu authored Aug 6, 2024
2 parents 2ce6006 + 99fcedf commit 8967b69
Show file tree
Hide file tree
Showing 15 changed files with 371 additions and 6 deletions.
10 changes: 9 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
"drupal/daterange_compact": "^2.0.1",
"drupal/twig_field_value": "^2.0.2",
"openeuropa/composer-artifacts": "^1.0.0-alpha1",
"openeuropa/oe_bootstrap_theme": "^1.10.0"
"openeuropa/oe_bootstrap_theme": "0.1.202408051724"
},
"require-dev": {
"composer/installers": "^1.11",
"drupal/better_exposed_filters": "^6.0.3",
"drupal/color_scheme_field": "1.x-dev",
"drupal/composite_reference": "^2.2",
"drupal/config_devel": "~1.9",
"drupal/core-composer-scaffold": "^10",
Expand Down Expand Up @@ -71,6 +72,13 @@
"extra": {
"composer-exit-on-patch-failure": true,
"enable-patching": true,
"patches-ignore": {
"openeuropa/oe_content": {
"drupal/field_group": {
"https://www.drupal.org/project/field_group/issues/2787179": "https://www.drupal.org/files/issues/2023-04-07/2787179-highlight-html5-validation-85.patch"
}
}
},
"artifacts": {
"openeuropa/oe_bootstrap_theme": {
"dist": {
Expand Down
83 changes: 80 additions & 3 deletions modules/oe_whitelabel_paragraphs/oe_whitelabel_paragraphs.module
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use Drupal\oe_bootstrap_theme\ConfigImporter;
use Drupal\oe_bootstrap_theme\ValueObject\ImageValueObject;
use Drupal\oe_bootstrap_theme\ValueObject\ImageValueObjectInterface;
use Drupal\oe_media_iframe\Plugin\media\Source\Iframe;
use Drupal\oe_whitelabel\ColorSchemePreprocess;
use Drupal\paragraphs\Entity\Paragraph;

/**
Expand Down Expand Up @@ -67,6 +68,12 @@ function oe_whitelabel_paragraphs_theme_suggestions_field_alter(array &$suggesti
* Implements hook_preprocess_paragraph().
*/
function oe_whitelabel_preprocess_paragraph__oe_links_block(array &$variables): void {
/** @var \Drupal\oe_whitelabel\ColorSchemePreprocess $color_scheme_preprocess */
$color_scheme_preprocess = \Drupal::classResolver(ColorSchemePreprocess::class);
$color_scheme_preprocess->injectColorScheme($variables, [
'text_colored' => TRUE,
]);

/** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */
$paragraph = $variables['paragraph'];
$variables['orientation'] = $paragraph->get('oe_w_links_block_orientation')->value;
Expand Down Expand Up @@ -118,6 +125,12 @@ function oe_whitelabel_preprocess_paragraph__oe_social_media_follow(array &$vari
* Implements hook_preprocess_paragraph__oe_accordion().
*/
function oe_whitelabel_preprocess_paragraph__oe_accordion(array &$variables): void {
/** @var \Drupal\oe_whitelabel\ColorSchemePreprocess $color_scheme_preprocess */
$color_scheme_preprocess = \Drupal::classResolver(ColorSchemePreprocess::class);
$color_scheme_preprocess->injectColorScheme($variables, [
'text_colored' => TRUE,
]);

// Massage data to be compliant with OE Bootstrap Theme accordion pattern
// data structure.
$builder = \Drupal::entityTypeManager()->getViewBuilder('paragraph');
Expand Down Expand Up @@ -188,6 +201,12 @@ function oe_whitelabel_preprocess_paragraph__oe_av_media(array &$variables): voi
* Implements hook_preprocess_paragraph() for paragraph--oe-list-item-block.html.twig.
*/
function oe_whitelabel_preprocess_paragraph__oe_list_item_block(array &$variables): void {
/** @var \Drupal\oe_whitelabel\ColorSchemePreprocess $color_scheme_preprocess */
$color_scheme_preprocess = \Drupal::classResolver(ColorSchemePreprocess::class);
$color_scheme_preprocess->injectColorScheme($variables, [
'text_colored' => TRUE,
]);

/** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */
$paragraph = $variables['paragraph'];

Expand Down Expand Up @@ -247,8 +266,28 @@ function oe_whitelabel_preprocess_paragraph__oe_list_item_block(array &$variable
* Implements hook_preprocess_paragraph() for oe_banner paragraph.
*/
function oe_whitelabel_preprocess_paragraph__oe_banner(array &$variables): void {
/** @var \Drupal\oe_whitelabel\ColorSchemePreprocess $color_scheme_preprocess */
$color_scheme_preprocess = \Drupal::classResolver(ColorSchemePreprocess::class);
/** @var Drupal\paragraphs\Entity\Paragraph $paragraph */
$paragraph = $variables['paragraph'];

$variant = $paragraph->get('oe_paragraphs_variant')->value ?? 'default';
$variables['variant'] = str_replace('oe_banner_', '', $variant);

match($variables['variant']) {
'image' => $color_scheme_preprocess->injectColorScheme($variables, [
'text_colored' => TRUE,
]),
'image_shade' => $color_scheme_preprocess->injectColorScheme($variables),
'primary' => $color_scheme_preprocess->injectColorScheme($variables, [
'background' => 'primary',
]),
default => $color_scheme_preprocess->injectColorScheme($variables, [
'background' => 'default',
'text_colored' => TRUE,
]),
};

$variables['title'] = $paragraph->get('field_oe_title')->value;
$variables['description'] = $paragraph->get('field_oe_text')->value;
$variables['full_width'] = (bool) $paragraph->get('field_oe_banner_full_width')->value;
Expand Down Expand Up @@ -278,9 +317,6 @@ function oe_whitelabel_preprocess_paragraph__oe_banner(array &$variables): void
$variables['centered'] = $banner_alignment === 'center';
}

$variant = $paragraph->get('oe_paragraphs_variant')->value ?? 'default';
$variables['variant'] = str_replace('oe_banner_', '', $variant);

if ($variables['variant'] === 'default' || $variables['variant'] === 'primary') {
return;
}
Expand Down Expand Up @@ -342,6 +378,12 @@ function oe_whitelabel_preprocess_paragraph__oe_banner(array &$variables): void
* Implements hook_preprocess_paragraph() for timeline paragraph.
*/
function oe_whitelabel_preprocess_paragraph__oe_timeline(array &$variables): void {
/** @var \Drupal\oe_whitelabel\ColorSchemePreprocess $color_scheme_preprocess */
$color_scheme_preprocess = \Drupal::classResolver(ColorSchemePreprocess::class);
$color_scheme_preprocess->injectColorScheme($variables, [
'text_colored' => TRUE,
]);

$paragraph = $variables['paragraph'];
if (!$paragraph->get('field_oe_title')->isEmpty()) {
$variables['heading'] = $paragraph->get('field_oe_title')->value;
Expand Down Expand Up @@ -406,6 +448,12 @@ function oe_whitelabel_preprocess_paragraph__oe_content_row__variant_inpage_navi
* Implements hook_preprocess_paragraph() for oe_description-list paragraph.
*/
function oe_whitelabel_preprocess_paragraph__oe_description_list(array &$variables): void {
/** @var \Drupal\oe_whitelabel\ColorSchemePreprocess $color_scheme_preprocess */
$color_scheme_preprocess = \Drupal::classResolver(ColorSchemePreprocess::class);
$color_scheme_preprocess->injectColorScheme($variables, [
'text_colored' => TRUE,
]);

/** @var Drupal\paragraphs\Entity\Paragraph $paragraph */
$paragraph = $variables['paragraph'];
$variables['title'] = $paragraph->get('field_oe_title')->value ?? '';
Expand Down Expand Up @@ -433,6 +481,12 @@ function oe_whitelabel_preprocess_paragraph__oe_description_list(array &$variabl
* Implements hook_preprocess_paragraph().
*/
function oe_whitelabel_preprocess_paragraph__oe_facts_figures(array &$variables): void {
/** @var \Drupal\oe_whitelabel\ColorSchemePreprocess $color_scheme_preprocess */
$color_scheme_preprocess = \Drupal::classResolver(ColorSchemePreprocess::class);
$color_scheme_preprocess->injectColorScheme($variables, [
'text_colored' => TRUE,
]);

/** @var \Drupal\paragraphs\ParagraphInterface $paragraph */
$paragraph = $variables['paragraph'];
if (!$paragraph->get('field_oe_title')->isEmpty()) {
Expand Down Expand Up @@ -696,3 +750,26 @@ function _oe_whitelabel_paragraphs_preprocess_paragraph_media(array &$variables)

$cacheability->applyTo($variables);
}

/**
* Implements hook_preprocess_paragraph() for oe_document paragraph.
*/
function oe_whitelabel_paragraphs_preprocess_paragraph__oe_document(array &$variables): void {
/** @var \Drupal\oe_whitelabel\ColorSchemePreprocess $color_scheme_preprocess */
$color_scheme_preprocess = \Drupal::classResolver(ColorSchemePreprocess::class);
$color_scheme_preprocess->injectColorScheme($variables, [
'text_colored' => TRUE,
]);
}

/**
* Implements hook_preprocess_paragraph() for oe_quote paragraph.
*/
function oe_whitelabel_paragraphs_preprocess_paragraph__oe_quote(array &$variables): void {
/** @var \Drupal\oe_whitelabel\ColorSchemePreprocess $color_scheme_preprocess */
$color_scheme_preprocess = \Drupal::classResolver(ColorSchemePreprocess::class);
$color_scheme_preprocess->injectColorScheme($variables, [
'background' => 'default',
'text_colored' => TRUE,
]);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
<?php

declare(strict_types=1);

namespace Drupal\Tests\oe_whitelabel_paragraphs\Kernel\Paragraphs;

use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\paragraphs\Entity\Paragraph;
use Symfony\Component\DomCrawler\Crawler;

/**
* Test the color scheme are present in the paragraphs.
*/
class ColorSchemeTest extends ParagraphsTestBase {

/**
* {@inheritdoc}
*/
protected static $modules = [
'composite_reference',
'color_scheme_field',
'oe_content_timeline_field',
'oe_paragraphs_carousel',
'oe_paragraphs_document',
'oe_paragraphs_gallery',
'oe_paragraphs_timeline',
];

/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();

$this->container->get('module_handler')->loadInclude('oe_paragraphs_media_field_storage', 'install');
oe_paragraphs_media_field_storage_install(FALSE);
$this->installConfig([
'media',
'oe_media',
'oe_paragraphs_banner',
'oe_paragraphs_carousel',
'oe_paragraphs_document',
'oe_paragraphs_gallery',
'oe_paragraphs_media',
'oe_paragraphs_timeline',
]);
}

/**
* Tests that the color scheme is present in the paragraphs.
*/
public function testColorSchemeInParagraphs(): void {
FieldStorageConfig::create([
'field_name' => 'oe_w_colorscheme',
'entity_type' => 'paragraph',
'type' => 'color_scheme_field',
])->save();

foreach ($this->paragraphSettingsProvider() as $data) {
$field_instance = FieldConfig::loadByName('paragraph', $data['values']['type'], 'oe_w_colorscheme');

if (!$field_instance) {
FieldConfig::create([
'label' => 'ColorScheme field',
'field_name' => 'oe_w_colorscheme',
'entity_type' => 'paragraph',
'bundle' => $data['values']['type'],
])->save();
}

$paragraph = Paragraph::create($data['values'] + [
'oe_w_colorscheme' => [
'name' => 'foo_bar',
],
]);
$paragraph->save();

$html = $this->renderParagraph($paragraph);
$crawler = new Crawler($html);

$element = $crawler->filter($data['wrapper_selector'] . '.foo-bar');
$this->assertCount(1, $element, sprintf('Element "%s" has color scheme applied.', $data['wrapper_selector']));
}
}

/**
* Data provider for testColorSchemeInParagraphs.
*
* @return \Generator
* The test data.
*/
protected function paragraphSettingsProvider(): \Generator {
yield [
'values' => [
'type' => 'oe_accordion',
'field_oe_paragraphs' => Paragraph::create([
'type' => 'oe_accordion_item',
'field_oe_icon' => 'box-arrow-up',
'field_oe_text' => 'Accordion item',
'field_oe_text_long' => 'Accordion text',
]),
],
'wrapper_selector' => '.accordion.text-color-default',
];
yield [
'values' => [
'type' => 'oe_banner',
'oe_paragraphs_variant' => 'default',
'field_oe_title' => 'Banner',
],
'wrapper_selector' => '.bcl-banner.text-color-default',
];
yield [
'values' => [
'type' => 'oe_banner',
'oe_paragraphs_variant' => 'oe_banner_image',
'field_oe_title' => 'Banner',
],
'wrapper_selector' => '.bcl-banner.text-color-default',
];
yield [
'values' => [
'type' => 'oe_banner',
'oe_paragraphs_variant' => 'oe_banner_image_shade',
'field_oe_title' => 'Banner',
],
'wrapper_selector' => '.bcl-banner',
];
yield [
'values' => [
'type' => 'oe_banner',
'oe_paragraphs_variant' => 'oe_banner_primary',
'field_oe_title' => 'Banner',
],
'wrapper_selector' => '.bcl-banner.text-bg-primary',
];
yield [
'values' => [
'type' => 'oe_quote',
'field_oe_text' => 'This is a test quote',
'field_oe_plain_text_long' => 'Quote text',
],
'wrapper_selector' => 'figure.bg-default.text-color-default',
];
yield [
'values' => [
'type' => 'oe_description_list',
'field_oe_title' => 'Description list paragraph',
'oe_w_orientation' => 'horizontal',
'field_oe_description_list_items' => [
[
'term' => 'Term',
'description' => 'Description',
],
],
],
'wrapper_selector' => '.bcl-description-list.text-color-default',
];
yield [
'values' => [
'type' => 'oe_document',
],
'wrapper_selector' => '.paragraph--type--oe-document.text-color-default',
];
yield [
'values' => [
'type' => 'oe_facts_figures',
'field_oe_title' => 'Facts and Figures test',
'field_oe_link' => [
'uri' => 'https://www.readmore.com',
'title' => 'Read more',
],
],
'wrapper_selector' => '.bcl-fact-figures.text-color-default',
];
yield [
'values' => [
'type' => 'oe_list_item_block',
'oe_paragraphs_variant' => 'default',
'field_oe_list_item_block_layout' => 'one_column',
'field_oe_title' => 'Listing item block title',
],
'wrapper_selector' => '.bcl-listing.text-color-default',
];
yield [
'values' => [
'type' => 'oe_links_block',
'field_oe_text' => 'Info',
'oe_bt_links_block_orientation' => 'vertical',
'oe_bt_links_block_background' => 'gray',
],
'wrapper_selector' => '.bcl-links-block.text-color-default',
];
yield [
'values' => [
'type' => 'oe_timeline',
'field_oe_timeline_expand' => '3',
'field_oe_timeline' => [
[
'label' => 'Label 1',
'title' => 'Title 1',
'body' => 'Description 1',
],
],
],
'wrapper_selector' => '.bcl-timeline.text-color-default',
];
}

}
Loading

0 comments on commit 8967b69

Please sign in to comment.