1);
}
}
/**
* Implements hook_theme().
*/
function metatag_favicons_theme() {
$info['metatag_favicon'] = array(
'render element' => 'element',
);
$info['metatag_mask_icon'] = array(
'render element' => 'element',
);
$info['metatag_shortcut_icon'] = array(
'render element' => 'element',
);
return $info;
}
/**
* Implements hoko_html_head_alter().
*
* Remove the default shortcut icon if one was set by Metatag.
*/
function metatag_favicons_html_head_alter(&$elements) {
if (isset($elements['metatag_shortcut icon'])) {
foreach ($elements as $key => $element) {
if (isset($element['#tag']) && $element['#tag'] == 'link') {
if (isset($element['#attributes']) && is_array($element['#attributes'])) {
if (isset($element['#attributes']['rel'])) {
if ($element['#attributes']['rel'] == 'shortcut icon') {
unset($elements[$key]);
}
}
}
}
}
}
}
/**
* Theme callback for a favicon meta tag.
*
* The format is:
*
*/
function theme_metatag_favicon($variables) {
$element = &$variables['element'];
$args = array(
'#rel' => 'rel',
'#value' => 'href',
'#sizes' => 'sizes',
'#mask' => 'mask',
);
element_set_attributes($element, $args);
unset($element['#value']);
return theme('html_tag', $variables);
}
/**
* Theme callback for the mask-icon meta tag.
*
* The format is:
*
*/
function theme_metatag_mask_icon($variables) {
$element = &$variables['element'];
$args = array(
'#rel' => 'rel',
'#value' => 'href',
'#color' => 'color',
);
element_set_attributes($element, $args);
unset($element['#value']);
return theme('html_tag', $variables);
}
/**
* Theme callback for a shortcut icon meta tag.
*
* The format is:
*
*/
function theme_metatag_shortcut_icon($variables) {
$element = &$variables['element'];
// Extract the MIME type.
$element['#type'] = metatag_favicons_get_mime_type($element['#value']);
$args = array(
'#rel' => 'rel',
'#value' => 'href',
'#type' => 'type',
);
element_set_attributes($element, $args);
unset($element['#value']);
return theme('html_tag', $variables);
}
/**
* Helper function to get the theme's favicon URL.
*
* @return string
* The absolute URL to the favicon, empty string if not found.
*/
function metatag_favicons_get_theme_favicon() {
$favicon_url = '';
// Is the favicon enabled?
if (theme_get_setting('toggle_favicon')) {
$favicon_url = theme_get_setting('favicon');
}
return $favicon_url;
}
/**
* Returns the correct MIME type for favicons.
*
* @param string $uri
* The URI, or URL, of the favicon to be checked.
*
* @return string
* The MIME type on success, an empty string on failure.
*/
function metatag_favicons_get_mime_type($uri) {
// URLs must have a file extension in order for this to work.
$extension_dot = strrpos($uri, '.');
if (!$extension_dot) {
return '';
}
// Return the proper MIME type based on the extension.
$extension = strtolower(substr($uri, $extension_dot + 1));
switch ($extension) {
case 'ico':
return 'image/vnd.microsoft.icon';
case 'jpg':
case 'jpeg':
return 'image/jpeg';
case 'svg':
return 'image/svg+xml';
case 'gif':
case 'png':
return 'image/' . $extension;
default:
return '';
}
}