123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- <?php
- /**
- * @file
- * Primary hook implementations for Metatag:favicons.
- */
- /**
- * Implements hook_ctools_plugin_api().
- */
- function metatag_favicons_ctools_plugin_api($owner, $api) {
- if ($owner == 'metatag' && $api == 'metatag') {
- return array('version' => 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:
- * <link rel="[rel]" href="[value]" sizes="[sizes]" />
- */
- 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:
- * <link rel="mask-icon" href="[value]" sizes="[sizes]" />
- */
- 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:
- * <link rel="[rel]" href="[value]" type="[type]" />
- */
- 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 '';
- }
- }
|