metatag_favicons.module 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * @file
  4. * Primary hook implementations for Metatag:favicons.
  5. */
  6. /**
  7. * Implements hook_ctools_plugin_api().
  8. */
  9. function metatag_favicons_ctools_plugin_api($owner, $api) {
  10. if ($owner == 'metatag' && $api == 'metatag') {
  11. return array('version' => 1);
  12. }
  13. }
  14. /**
  15. * Implements hook_theme().
  16. */
  17. function metatag_favicons_theme() {
  18. $info['metatag_favicon'] = array(
  19. 'render element' => 'element',
  20. );
  21. $info['metatag_mask_icon'] = array(
  22. 'render element' => 'element',
  23. );
  24. $info['metatag_shortcut_icon'] = array(
  25. 'render element' => 'element',
  26. );
  27. return $info;
  28. }
  29. /**
  30. * Implements hoko_html_head_alter().
  31. *
  32. * Remove the default shortcut icon if one was set by Metatag.
  33. */
  34. function metatag_favicons_html_head_alter(&$elements) {
  35. if (isset($elements['metatag_shortcut icon'])) {
  36. foreach ($elements as $key => $element) {
  37. if (isset($element['#tag']) && $element['#tag'] == 'link') {
  38. if (isset($element['#attributes']) && is_array($element['#attributes'])) {
  39. if (isset($element['#attributes']['rel'])) {
  40. if ($element['#attributes']['rel'] == 'shortcut icon') {
  41. unset($elements[$key]);
  42. }
  43. }
  44. }
  45. }
  46. }
  47. }
  48. }
  49. /**
  50. * Theme callback for a favicon meta tag.
  51. *
  52. * The format is:
  53. * <link rel="[rel]" href="[value]" sizes="[sizes]" />
  54. */
  55. function theme_metatag_favicon($variables) {
  56. $element = &$variables['element'];
  57. $args = array(
  58. '#rel' => 'rel',
  59. '#value' => 'href',
  60. '#sizes' => 'sizes',
  61. '#mask' => 'mask',
  62. );
  63. element_set_attributes($element, $args);
  64. unset($element['#value']);
  65. return theme('html_tag', $variables);
  66. }
  67. /**
  68. * Theme callback for the mask-icon meta tag.
  69. *
  70. * The format is:
  71. * <link rel="mask-icon" href="[value]" sizes="[sizes]" />
  72. */
  73. function theme_metatag_mask_icon($variables) {
  74. $element = &$variables['element'];
  75. $args = array(
  76. '#rel' => 'rel',
  77. '#value' => 'href',
  78. '#color' => 'color',
  79. );
  80. element_set_attributes($element, $args);
  81. unset($element['#value']);
  82. return theme('html_tag', $variables);
  83. }
  84. /**
  85. * Theme callback for a shortcut icon meta tag.
  86. *
  87. * The format is:
  88. * <link rel="[rel]" href="[value]" type="[type]" />
  89. */
  90. function theme_metatag_shortcut_icon($variables) {
  91. $element = &$variables['element'];
  92. // Extract the MIME type.
  93. $element['#type'] = metatag_favicons_get_mime_type($element['#value']);
  94. $args = array(
  95. '#rel' => 'rel',
  96. '#value' => 'href',
  97. '#type' => 'type',
  98. );
  99. element_set_attributes($element, $args);
  100. unset($element['#value']);
  101. return theme('html_tag', $variables);
  102. }
  103. /**
  104. * Helper function to get the theme's favicon URL.
  105. *
  106. * @return string
  107. * The absolute URL to the favicon, empty string if not found.
  108. */
  109. function metatag_favicons_get_theme_favicon() {
  110. $favicon_url = '';
  111. // Is the favicon enabled?
  112. if (theme_get_setting('toggle_favicon')) {
  113. $favicon_url = theme_get_setting('favicon');
  114. }
  115. return $favicon_url;
  116. }
  117. /**
  118. * Returns the correct MIME type for favicons.
  119. *
  120. * @param string $uri
  121. * The URI, or URL, of the favicon to be checked.
  122. *
  123. * @return string
  124. * The MIME type on success, an empty string on failure.
  125. */
  126. function metatag_favicons_get_mime_type($uri) {
  127. // URLs must have a file extension in order for this to work.
  128. $extension_dot = strrpos($uri, '.');
  129. if (!$extension_dot) {
  130. return '';
  131. }
  132. // Return the proper MIME type based on the extension.
  133. $extension = strtolower(substr($uri, $extension_dot + 1));
  134. switch ($extension) {
  135. case 'ico':
  136. return 'image/vnd.microsoft.icon';
  137. case 'jpg':
  138. case 'jpeg':
  139. return 'image/jpeg';
  140. case 'svg':
  141. return 'image/svg+xml';
  142. case 'gif':
  143. case 'png':
  144. return 'image/' . $extension;
  145. default:
  146. return '';
  147. }
  148. }