Respuestas:
Puede colocarlo favicon.icoen su carpeta de temas (al mismo nivel que your_theme.info) y se usará automáticamente.
Funciona para Drupal 6, 7 y 8.
Nota: el favicon está fuertemente almacenado en caché por algunos navegadores, es posible que deba hacer un esfuerzo adicional para ver el nuevo.
En Drupal 8, puede usar el settings.ymlarchivo, ubicado enthemes/YOURTHEME/config/install/YOURTHEME.settings.yml
Aquí hay un ejemplo para la personalización del logo / favicon del tema:
logo:
use_default: false
path: 'themes/YOURTHEME/logo.png'
favicon:
use_default: false
path: 'themes/YOURTHEME/favicon.png'
Sin embargo, si cambia estas configuraciones mientras su tema ya está instalado en la administración de Drupal, deberá desinstalarlo y luego reinstalarlo. De lo contrario, incluso si borra todos los cachés, Drupal no tendrá en cuenta sus cambios.
<?php
function hook_page_alter(&$pages) {
$favicon = "http://example.com/sites/default/files/favicon.ico";
$type = theme_get_setting('favicon_mimetype');
drupal_add_html_head_link(array('rel' => 'shortcut icon', 'href' => drupal_strip_dangerous_protocols($favicon), 'type' => $type));
}
?>
/**
* Implements hook_html_head_alter().
*/
function MYTHEME_html_head_alter(&$head_elements) {
// Remove existing favicon location
global $base_url;
$default_favicon_element = 'drupal_add_html_head_link:shortcut icon:' . $base_url . '/misc/favicon.ico';
unset($head_elements[$default_favicon_element]);
// Specify new favicon location
$element = array(
'rel' => 'shortcut icon',
'href' => '/path-to-favicon/favicon.ico',
);
drupal_add_html_head_link($element);
}
/**
* Implements hook_html_head_alter().
*/
// Remove existing favicon location
function MODULENAME_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]);
// Specify new favicon location
$element = array(
'rel' => 'shortcut icon',
'href' => '/path-to-favicon/favicon.ico',
);
drupal_add_html_head_link($element);
}
Ver hook_html_head_alter para más información.
Nota: No es necesario enumerar la nueva ubicación de favicon en hook_html_head_alter(). Normalmente lo especifico en THEMENAME_preprocess_html()o MODULENAME_init().
El siguiente código (en un módulo personalizado) reemplaza el favicon, en lugar de agregar uno adicional.
/**
* Implements hook_html_head_alter().
*
* Replaces the favicon.
*
* @param array $head_elements
*/
function MYMODULE_html_head_alter(&$head_elements) {
foreach ($head_elements as $key => $element) {
if (1
// The array key can vary, depending on the original favicon setting.
&& 0 === strpos($key, 'drupal_add_html_head_link:shortcut icon:')
&& !empty($element['#attributes']['href'])
&& 'shortcut icon' === $element['#attributes']['rel']
) {
// Make sure to use a file that actually exists!
$favicon_path = drupal_get_path('module', 'MYMODULE') . '/img/favicon_32.png';
$favicon_url = file_create_url($favicon_path);
// If the favicon path came from a user-provided setting, we would also need drupal_strip_dangerous_protocols().
$element['#attributes']['href'] = $favicon_url;
$element['#attributes']['type'] = 'image/png';
$head_elements[$key] = $element;
}
}
}
Para la ubicación del archivo favicon, sugeriría la carpeta del módulo de MYMODULE o sites / default / favicon.ico. El objetivo es tener el archivo en control de versiones y NO en la carpeta de archivos públicos. No queremos que se pueda escribir en la web.
Supongo que la mayoría de la gente usará * .ico en lugar de * .png, en este caso el 'tipo' puede mantener su valor original.