1

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.

1 Answer 1

1

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 &$page) {
     foreach($page['#attached']['html_head_link'] as $k => $v) {
          if (array_key_exists('rel', $v[0]) && $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]->get('features.favicon')) {
        $favicon_path = $cache[$theme]->get('favicon.path');
        if ($cache[$theme]->get('favicon.use_default')) {
          if (file_exists($favicon = $theme_object->getPath() . '/favicon.ico')) {
            $cache[$theme]->set('favicon.url', file_url_transform_relative(file_create_url($favicon)));
          }
          else {
            $cache[$theme]->set('favicon.url', file_url_transform_relative(file_create_url('core/misc/favicon.ico')));
          }
        }
        elseif ($favicon_path) {
          $cache[$theme]->set('favicon.url', file_url_transform_relative(file_create_url($favicon_path)));
        }
        else {
          $cache[$theme]->set('features.favicon', FALSE);
        }
      }
3
  • This would need extra checks to see if the user has chosen not to use the theme's favicon. Mar 8, 2016 at 12:13
  • 1
    i guess you can use the default location. theme_folder/favicon.ico - check the source here api.drupal.org/api/drupal/core%21includes%21theme.inc/8
    – rémy
    Mar 8, 2016 at 20:32
  • Using the default location seems like the easiest way to accomplish this. Thanks for your input! Mar 9, 2016 at 6:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.