Skip to content

Commit

Permalink
Merge pull request #1492 from openeuropa/EWPP-4321
Browse files Browse the repository at this point in the history
EWPP-4321: Ensure oe_theme favicons are used if the subtheme is configured to use them and doesn't provide them.
  • Loading branch information
upchuk authored Dec 9, 2024
2 parents 247d806 + 90e8d9f commit 568bf87
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 7 deletions.
18 changes: 15 additions & 3 deletions oe_theme.theme
Original file line number Diff line number Diff line change
Expand Up @@ -2559,28 +2559,40 @@ function oe_theme_preprocess_select(&$variables) {
* Implements hook_page_attachments_alter().
*/
function oe_theme_page_attachments_alter(array &$attachments) {
// If the theme is configured to not use the theme favicon, bail out.
if (!theme_get_setting('favicon.use_default')) {
return;
}

// Use different favicon based on theme component library.
$active_theme = \Drupal::theme()->getActiveTheme();
if ($active_theme->getName() === 'oe_theme' || array_key_exists('oe_theme', $active_theme->getBaseThemeExtensions())) {
$oe_theme_path = \Drupal::service('extension.list.theme')->getPath('oe_theme');
$active_theme_path = $active_theme->getPath();
$component_library = theme_get_setting('component_library') ?? 'ec';
$favicon = DRUPAL_ROOT . '/' . $active_theme_path . "/images/favicons/$component_library/favicon.ico";
foreach ($attachments['#attached']['html_head_link'] as &$link) {
if ($link[0]['rel'] !== 'icon') {
continue;
}
$link[0]['href'] = base_path() . $active_theme->getPath() . "/images/favicons/$component_library/favicon.ico";
// If subtheme file exists on the server, use its base path, otherwise
// use the base path of the base theme file.
$link[0]['href'] = file_exists($favicon) ? base_path() . $active_theme_path . "/images/favicons/$component_library/favicon.ico" : base_path() . $oe_theme_path . "/images/favicons/$component_library/favicon.ico";
}
// Add the svg and png favicons.
$svg_favicon = DRUPAL_ROOT . '/' . $active_theme_path . "/images/favicons/$component_library/favicon.svg";
$attachments['#attached']['html_head_link'][] = [
[
'rel' => 'icon',
'href' => base_path() . $active_theme->getPath() . "/images/favicons/$component_library/favicon.svg",
'href' => file_exists($svg_favicon) ? base_path() . $active_theme_path . "/images/favicons/$component_library/favicon.svg" : base_path() . $oe_theme_path . "/images/favicons/$component_library/favicon.svg",
'type' => 'image/svg+xml',
],
];
$png_favicon = DRUPAL_ROOT . '/' . $active_theme_path . "/images/favicons/$component_library/favicon.png";
$attachments['#attached']['html_head_link'][] = [
[
'rel' => 'apple-touch-icon',
'href' => base_path() . $active_theme->getPath() . "/images/favicons/$component_library/favicon.png",
'href' => file_exists($png_favicon) ? base_path() . $active_theme_path . "/images/favicons/$component_library/favicon.png" : base_path() . $oe_theme_path . "/images/favicons/$component_library/favicon.png",
],
];
}
Expand Down
39 changes: 35 additions & 4 deletions tests/src/Functional/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ public function testChangeComponentLibrary(): void {

// Assert that the favicon provided by the theme is being used.
$this->assertSession()->responseContains("/$active_theme/images/favicons/eu/favicon.ico");
$this->assertSession()->responseContains("$active_theme/images/favicons/eu/favicon.png");
$this->assertSession()->responseContains("/$active_theme/images/favicons/eu/favicon.svg");
$this->assertSession()->responseContains('/oe_theme/images/favicons/eu/favicon.png');
$this->assertSession()->responseContains('/oe_theme/images/favicons/eu/favicon.svg');

// Assert that we do not load the EC component library.
$this->assertLinkNotContainsHref('/oe_theme/dist/ec/styles/ecl-ec.css');
Expand Down Expand Up @@ -170,8 +170,8 @@ public function testChangeComponentLibrary(): void {

// Assert that the favicon provided by the theme is being used.
$this->assertSession()->responseContains("/$active_theme/images/favicons/ec/favicon.ico");
$this->assertSession()->responseContains("$active_theme/images/favicons/ec/favicon.png");
$this->assertSession()->responseContains("/$active_theme/images/favicons/ec/favicon.svg");
$this->assertSession()->responseContains('/oe_theme/images/favicons/ec/favicon.png');
$this->assertSession()->responseContains('/oe_theme/images/favicons/ec/favicon.svg');

// Assert that we do not load the EU component library by default.
$this->assertLinkNotContainsHref('/oe_theme/dist/eu/styles/ecl-eu.css');
Expand Down Expand Up @@ -245,6 +245,37 @@ public function testChangeEclBranding(): void {
}
}

/**
* Test that the correct favicon is used based on theme configuration.
*/
public function testUseEclFavicon(): void {
foreach (['oe_theme', 'oe_theme_subtheme_test'] as $active_theme) {
$this->config('system.theme')->set('default', $active_theme)->save();
$this->container->set('theme.registry', NULL);
$assert_session = $this->assertSession();
// Assert that the favicon provided by the base theme is being used.
$this->drupalGet('<front>');
$assert_session->responseContains("/$active_theme/images/favicons/ec/favicon.ico");
$assert_session->responseContains('/oe_theme/images/favicons/ec/favicon.png');
$assert_session->responseContains('/oe_theme/images/favicons/ec/favicon.svg');

// Configure theme to not use default favicon.
$this->config("$active_theme.settings")->set('favicon', [
'mimetype' => 'image/vnd.microsoft.icon',
'path' => 'https://www.w3schools.com/images/favicon.ico',
'use_default' => FALSE,
])->save();
$this->container->set('theme.registry', NULL);

// Assert that the favicon provided by the base theme is not being used.
$this->drupalGet('<front>');
$assert_session->responseNotContains("/$active_theme/images/favicons/ec/favicon.ico");
$assert_session->responseNotContains('/oe_theme/images/favicons/ec/favicon.png');
$assert_session->responseNotContains('/oe_theme/images/favicons/ec/favicon.svg');
$assert_session->responseContains("https://www.w3schools.com/images/favicon.ico");
}
}

/**
* Assert that current response contians a link tag with given href.
*
Expand Down
Binary file removed tests/themes/oe_theme_subtheme_test/favicon.ico
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 568bf87

Please sign in to comment.