From 018baee42a9ac79cc5d8cd85bb8ae1a51141637b Mon Sep 17 00:00:00 2001
From: Lauri Eskola <lauriii@1078742.no-reply.drupal.org>
Date: Fri, 11 May 2018 12:54:22 +0300
Subject: [PATCH] Issue #1507896 by b0unty, tuutti, Jeff Burnz, LewisNyman,
 tameeshb, realityloop, cbanman, umarzaffer, manumilou, mgifford, mducharme,
 oakulm, lauriii, alexpott, joelpittet, MaskyS, markcarver, xjm: Allow theme
 developers to add the default logo filename to the theme's .info.yml

---
 core/includes/theme.inc                       |  3 +-
 core/lib/Drupal/Core/Theme/ActiveTheme.php    | 19 +++++++
 .../Drupal/Core/Theme/ThemeInitialization.php |  9 ++++
 .../themes/test_theme/test_theme.info.yml     |  1 +
 .../Core/Theme/ThemeSettingsTest.php          | 49 +++++++++++++++++++
 5 files changed, 80 insertions(+), 1 deletion(-)

diff --git a/core/includes/theme.inc b/core/includes/theme.inc
index a6fffa76ae20..30e5aafc6c71 100644
--- a/core/includes/theme.inc
+++ b/core/includes/theme.inc
@@ -343,7 +343,8 @@ function theme_get_setting($setting_name, $theme = NULL) {
 
       // Generate the path to the logo image.
       if ($cache[$theme]->get('logo.use_default')) {
-        $cache[$theme]->set('logo.url', file_url_transform_relative(file_create_url($theme_object->getPath() . '/logo.svg')));
+        $logo = \Drupal::service('theme.initialization')->getActiveThemeByName($theme)->getLogo();
+        $cache[$theme]->set('logo.url', file_url_transform_relative(file_create_url($logo)));
       }
       elseif ($logo_path = $cache[$theme]->get('logo.path')) {
         $cache[$theme]->set('logo.url', file_url_transform_relative(file_create_url($logo_path)));
diff --git a/core/lib/Drupal/Core/Theme/ActiveTheme.php b/core/lib/Drupal/Core/Theme/ActiveTheme.php
index bb5c766b04f7..b7076fb6bdef 100644
--- a/core/lib/Drupal/Core/Theme/ActiveTheme.php
+++ b/core/lib/Drupal/Core/Theme/ActiveTheme.php
@@ -19,6 +19,13 @@ class ActiveTheme {
    */
   protected $name;
 
+  /**
+   * The path to the logo.
+   *
+   * @var string
+   */
+  protected $logo;
+
   /**
    * The path to the theme.
    *
@@ -100,6 +107,7 @@ public function __construct(array $values) {
       'path' => '',
       'engine' => 'twig',
       'owner' => 'twig',
+      'logo' => '',
       'stylesheets_remove' => [],
       'libraries' => [],
       'extension' => 'html.twig',
@@ -110,6 +118,7 @@ public function __construct(array $values) {
     ];
 
     $this->name = $values['name'];
+    $this->logo = $values['logo'];
     $this->path = $values['path'];
     $this->engine = $values['engine'];
     $this->owner = $values['owner'];
@@ -203,6 +212,16 @@ public function getBaseThemes() {
     return $this->baseThemes;
   }
 
+  /**
+   * Returns the logo provided by the theme.
+   *
+   * @return string
+   *   The logo path.
+   */
+  public function getLogo() {
+    return $this->logo;
+  }
+
   /**
    * The regions used by the theme.
    *
diff --git a/core/lib/Drupal/Core/Theme/ThemeInitialization.php b/core/lib/Drupal/Core/Theme/ThemeInitialization.php
index 0067ee8710ae..5d429dd799a2 100644
--- a/core/lib/Drupal/Core/Theme/ThemeInitialization.php
+++ b/core/lib/Drupal/Core/Theme/ThemeInitialization.php
@@ -173,6 +173,15 @@ public function getActiveTheme(Extension $theme, array $base_themes = []) {
     $values['path'] = $theme_path;
     $values['name'] = $theme->getName();
 
+    // Use the logo declared in this themes info file, otherwise use logo.svg
+    // from the themes root.
+    if (!empty($theme->info['logo'])) {
+      $values['logo'] = $theme->getPath() . '/' . $theme->info['logo'];
+    }
+    else {
+      $values['logo'] = $theme->getPath() . '/logo.svg';
+    }
+
     // @todo Remove in Drupal 9.0.x.
     $values['stylesheets_remove'] = $this->prepareStylesheetsRemove($theme, $base_themes);
 
diff --git a/core/modules/system/tests/themes/test_theme/test_theme.info.yml b/core/modules/system/tests/themes/test_theme/test_theme.info.yml
index 1c9745f1c364..b73a45d7c622 100644
--- a/core/modules/system/tests/themes/test_theme/test_theme.info.yml
+++ b/core/modules/system/tests/themes/test_theme/test_theme.info.yml
@@ -14,6 +14,7 @@ description: 'Theme for testing the theme system'
 version: VERSION
 base theme: classy
 core: 8.x
+logo: images/logo2.svg
 stylesheets-remove:
   - '@system/css/js.module.css'
 libraries:
diff --git a/core/tests/Drupal/KernelTests/Core/Theme/ThemeSettingsTest.php b/core/tests/Drupal/KernelTests/Core/Theme/ThemeSettingsTest.php
index a077c25a853c..130df2baee58 100644
--- a/core/tests/Drupal/KernelTests/Core/Theme/ThemeSettingsTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Theme/ThemeSettingsTest.php
@@ -60,4 +60,53 @@ public function testNoDefaultConfig() {
     $this->assertNotNull(theme_get_setting('features.favicon', $name));
   }
 
+  /**
+   * Tests that the default logo config can be overridden.
+   */
+  public function testLogoConfig() {
+    /** @var \Drupal\Core\Extension\ThemeHandler $theme_handler */
+    $theme_handler = $this->container->get('theme_handler');
+    $theme_handler->install(['stark']);
+    $theme = $theme_handler->getTheme('stark');
+
+    // Tests default behaviour.
+    $expected = '/' . $theme->getPath() . '/logo.svg';
+    $this->assertEquals($expected, theme_get_setting('logo.url', 'stark'));
+
+    $config = $this->config('stark.settings');
+    drupal_static_reset('theme_get_setting');
+
+    $values = [
+      'default_logo' => FALSE,
+      'logo_path' => 'public://logo_with_scheme.png',
+    ];
+    theme_settings_convert_to_config($values, $config)->save();
+
+    // Tests logo path with scheme.
+    $expected = file_url_transform_relative(file_create_url('public://logo_with_scheme.png'));
+    $this->assertEquals($expected, theme_get_setting('logo.url', 'stark'));
+
+    $values = [
+      'default_logo' => FALSE,
+      'logo_path' => $theme->getPath() . '/logo_relative_path.gif',
+    ];
+    theme_settings_convert_to_config($values, $config)->save();
+
+    drupal_static_reset('theme_get_setting');
+
+    // Tests relative path.
+    $expected = '/' . $theme->getPath() . '/logo_relative_path.gif';
+    $this->assertEquals($expected, theme_get_setting('logo.url', 'stark'));
+
+    $theme_handler->install(['test_theme']);
+    $theme_handler->setDefault('test_theme');
+    $theme = $theme_handler->getTheme('test_theme');
+
+    drupal_static_reset('theme_get_setting');
+
+    // Tests logo set in test_theme.info.yml.
+    $expected = '/' . $theme->getPath() . '/images/logo2.svg';
+    $this->assertEquals($expected, theme_get_setting('logo.url', 'test_theme'));
+  }
+
 }
-- 
GitLab