diff --git a/oe_theme.theme b/oe_theme.theme index fe9fbf643..65cf58993 100644 --- a/oe_theme.theme +++ b/oe_theme.theme @@ -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", ], ]; } diff --git a/tests/src/Functional/ConfigurationTest.php b/tests/src/Functional/ConfigurationTest.php index b49493f67..e1945094b 100755 --- a/tests/src/Functional/ConfigurationTest.php +++ b/tests/src/Functional/ConfigurationTest.php @@ -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'); @@ -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'); @@ -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(''); + $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(''); + $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. * diff --git a/tests/themes/oe_theme_subtheme_test/favicon.ico b/tests/themes/oe_theme_subtheme_test/favicon.ico deleted file mode 100644 index 18f1b3167..000000000 Binary files a/tests/themes/oe_theme_subtheme_test/favicon.ico and /dev/null differ diff --git a/tests/themes/oe_theme_subtheme_test/images/favicons/ec/favicon.ico b/tests/themes/oe_theme_subtheme_test/images/favicons/ec/favicon.ico new file mode 100644 index 000000000..5b1bb43a3 Binary files /dev/null and b/tests/themes/oe_theme_subtheme_test/images/favicons/ec/favicon.ico differ diff --git a/tests/themes/oe_theme_subtheme_test/images/favicons/eu/favicon.ico b/tests/themes/oe_theme_subtheme_test/images/favicons/eu/favicon.ico new file mode 100644 index 000000000..c076906c0 Binary files /dev/null and b/tests/themes/oe_theme_subtheme_test/images/favicons/eu/favicon.ico differ