1

I have a multisite website with multiple themes,each contains favicon. I have to show the favicon from the active theme in website.

Admin page shown the favicon from admin theme or if favicon is not set its shows the default icon (misc/favicon.co) whereas the website foranonymous users doesnot show the favicon.

With reference to the existing issues in Drupal exchange it can be managed by the following code in a custom module.

/*
 * Implements hook_html_head_alter
 *//*
function mymod_html_head_alter(&$head_elements) {
   global $base_url;
   $default_favicon_element = 'drupal_add_html_head_link:shortcut icon:' . $base_url . '/misc/favicon.ico';
   unset($head_elements[$default_favicon_element]);
}

function mymod_init() {
  global $base_url;
  $favicon_uri = $base_url .'/'. drupal_get_path('theme',$GLOBALS['theme']).'/favicon.ico';
  if (!file_exists($uri)) {
    $favicon_uri = $base_url .'/'.drupal_get_path('theme', 'mytheme100').'/favicon.ico';
  }
  // Specify new favicon location
  $element = array(
    'rel' => 'shortcut icon',
    'href' => $favicon_uri,
  );
  drupal_add_html_head_link($element);
}

But since I have used hook_init its not a better method.I have tried out the same in theme's template.php file with hook_html_head_alter()

    // Search the head elements for the Favicon
global $base_url;
   $favicon_uri = $base_url .'/'. drupal_get_path('theme',$GLOBALS['theme']).'/favicon.ico';
  if (file_exists($favicon_uri)) {print"<br>iff";
    $favicon_uri = $base_url .'/'.drupal_get_path('theme', 'mytheme100').'/favicon.ico';
  }

  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'] = $favicon_uri;

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

This code didn't show the favicon. Any help will be appreciated.

Your Answer

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

Browse other questions tagged or ask your own question.