2

Some of search engines (yandex in my case) want me to output favicon with type image/x-icon.

But drupal 7 outputs it without this type. How could i override this through template.php or any other suggestion? Thanks!

1

The following code in your template.php should do the job.

function MYTHEME_preprocess_html(&$variables) {
  $favicon = theme_get_setting('favicon');
  $type = 'image/x-icon';
  drupal_add_html_head_link(array('rel' => 'shortcut icon', 'href' => drupal_strip_dangerous_protocols($favicon), 'type' => $type));
}

Change image/type to image/x-icon

  • Unfortunately it did nothing to the output of the favicon (i did cleared the cash). Thanks – Rootical V. May 16 '12 at 9:57
  • I've just tested it and it works fine for me. You have change MYTHEME to the name of your theme, yes? – Chapabu May 16 '12 at 10:09
  • Yes of course i did.. – Rootical V. May 16 '12 at 10:11
  • Hmm...you might want to try clearing your cache a couple more times, and the browser cache - the code definitely works. What sub-theme are you using? – Chapabu May 16 '12 at 10:15
  • I am using selfbuilt html5 theme. Ok thank you for the answer i`ll be hoping it will change someday ) – Rootical V. May 16 '12 at 10:26
4

To alter or remove the favicon URL Drupal 7 points to by default (not just add another favicon link) try this:

function YOURTHEME_html_head_alter(&$head_elements)
{
  // Search the head elements for the Favicon
  foreach ($head_elements as $key => $element) {
    if (!empty($element['#attributes'])) {
      if (array_key_exists('href', $element['#attributes'])) {
        if (strpos($element['#attributes']['href'], 'misc/favicon.ico') > 0) 
        {
          // Change the URL
          $head_elements[$key]['#attributes']['href'] = '/'.drupal_get_path('theme', 'YOURTHEME').'/path/to/favicon.ico';

          // Or, delete the favicon link entirely
          unset($head_elements[$key]);
        }
      }
    }
  }
}
2

Another Simple Solution to this problem would be using a contributed module https://drupal.org/project/favicon.

Using this module, you can avoid getting 404 on those browsers which make a call to <>/favicon.ico to retrieve sites fav ICON. This behavior will cause a 404 Entry to show up in drupal logs.

1

In Drupal 8 you could use the following to remove the Icon Link (Don't remove the favicon.ico itself) as a Link is depreciated.

in the file: themes/YOUR_THEME/YOUR_THEME.theme

/**
 * Implements template_preprocess_html().
 *
 * Search the head elements for the Favicon and remove it.
 * See: http://realfavicongenerator.net/faq#why_ico_not_declared.
 */
function YOUR_THEME_page_attachments_alter(&$page) {
  foreach ($page['#attached'] as $key => $element) {
    if ($key === 'html_head_link' && !empty($element)) {
      foreach ($element as $k => $item) {
        if (isset($item[0]['rel']) && $item[0]['rel'] === 'shortcut icon') {
          unset($page['#attached']['html_head_link'][$k]);
        }
      }
    }
  }
}
  • What would you recommend to add all the different favicon-files to Drupal? Just putting them in the html.html.twig or is there a better way? – Ideogram Jun 30 '17 at 7:50
  • 1
    You may have a look on Drupal Metatag module. Personally I mostly ended up adding icons to html.html.twig, as the are static part of the theme. – digitaldonkey Jun 30 '17 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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