How to use a relative path for a theme's favicon I'm trying to ship a favicon with my custom Drupal 8 theme, but I can't figure out how to specify its location relative to my theme's installation location.

Following this answer I've created a config/install/MY_THEME.settings.yml file containing

favicon:
  use_default: false
  path: '/themes/MY_THEME/static/favicon.png'


(I'm keeping all my theme's static files in a static subdirectory of my theme directory)

This works, but contains a hard-coded reference to the installation location of my theme. Hence, if a user decides to install the theme in themes/contrib or themes/custom instead of in themes the favicon won't be found.

Therefore I would like to specify a path relative to my theme's root directory or relative to the MY_THEME.settings.yml file. However, it seems that Drupal uses the server root (/) as a base when I specify a relative path, so static/favicon.ico ends up as /static/favicon.ico which obviously doesn't work.
another idea for a workaround would be to overwrite the href property on runtime with hook_page_attachments_alter, somethng like this:

function MYTHEME_page_attachments_alter(array &amp;$page) {
     foreach($page['#attached']['html_head_link'] as $k =&gt; $v) {
          if (array_key_exists('rel', $v[0]) &amp;&amp; $v[0]['rel'] == 'shortcut icon') {
                  $page['#attached']['html_head_link'][$k][0]['href'] = drupal_get_path('theme', 'MYTHEME') . 'static/favicon.png';
          }
     }
}


or use the default favicon path (MY_THEME/favicon.ico), it will be taken, see this source:

// Generate the path to the favicon.
      if ($cache[$theme]-&gt;get('features.favicon')) {
        $favicon_path = $cache[$theme]-&gt;get('favicon.path');
        if ($cache[$theme]-&gt;get('favicon.use_default')) {
          if (file_exists($favicon = $theme_object-&gt;getPath() . '/favicon.ico')) {
            $cache[$theme]-&gt;set('favicon.url', file_url_transform_relative(file_create_url($favicon)));
          }
          else {
            $cache[$theme]-&gt;set('favicon.url', file_url_transform_relative(file_create_url('core/misc/favicon.ico')));
          }
        }
        elseif ($favicon_path) {
          $cache[$theme]-&gt;set('favicon.url', file_url_transform_relative(file_create_url($favicon_path)));
        }
        else {
          $cache[$theme]-&gt;set('features.favicon', FALSE);
        }
      }