add taxonomy terms as tokens to Metatag Quick

Public

Taxonomy terms as tokens in the Metatag Quick module, a patch

Get raw version
php
  1. <?php
  2. // Adds meta tag to internal storage that will be processed during page build.
  3. // Copy of _metatags_quick_add_head($item = FALSE) of Meta tags quick module
  4. // see module http://drupal.org/project/metatags_quick and code
  5. // http://drupalcode.org/project/metatags_quick.git/blob/6b87f44670f79b6db43f03b605641487e65c8e07:/metatags_quick.module
  6. function _YOUR_MODULE_add_head($item = FALSE) {
  7. static $added_meta = array();
  8. static $meta_data = array();
  9. if (!empty($added_meta[$item['name']])) {
  10. return;
  11. }
  12. // Only output meta if content is not empty.
  13. if ($item['content']) {
  14. $content = $item['content'];
  15. if (!empty($item['type']) && !empty($item['entity'])) {
  16. $content = token_replace($content, array(
  17. $item['type'] => $item['entity'],
  18. ));
  19. }
  20. else {
  21. $content = token_replace($content);
  22. }
  23. // (Not nice) hack to separate multiple tags returned by token.
  24. $content = preg_replace('/<\/a><a/', '<\/a>, <a', $content);
  25. $content = trim(strip_tags($content));
  26. $item['content'] = $content;
  27. $meta_data[] = $item;
  28. $element = array(
  29. '#tag' => 'meta',
  30. '#attributes' => array(
  31. 'name' => $item['name'],
  32. 'content' => $item['content'],
  33. ),
  34. );
  35. drupal_add_html_head($element, 'metatags_quick_' . $item['name']);
  36.  
  37. }
  38. $added_meta[$item['name']] = TRUE;
  39. }
  40.  
  41. /**
  42. * Adds 'Link generating meta keywords' and 'Plain text generating meta keywords'
  43. * field formatters for taxonomy reference terms.
  44. * Terms are extracted and they are added as meta keywords to the header.
  45. * Implements hook_field_formatter_info().
  46. */
  47. function YOUR_MODULE_field_formatter_info() {
  48. // taxonomy_field_formatter_info()
  49. return array(
  50. 'YOUR_MODULE_taxonomy_term_reference_link' => array(
  51. 'label' => t('Link generating meta keywords'),
  52. 'description' => t('Adds additionally taxonomy terms as meta keywords to html head.'),
  53. 'field types' => array('taxonomy_term_reference'),
  54. ),
  55. 'YOUR_MODULE_taxonomy_term_reference_plain' => array(
  56. 'label' => t('Plain text generating meta keywords'),
  57. 'description' => t('Adds additionally taxonomy terms as meta keywords to html head.'),
  58. 'field types' => array('taxonomy_term_reference'),
  59. ),
  60. );
  61. }
  62.  
  63. /**
  64. * Simply calls the base function taxonomy_field_formatter_prepare_view().
  65. * Implements hook_field_formatter_prepare_view().
  66. */
  67. function YOUR_MODULE_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) {
  68. return taxonomy_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, $items, $displays);
  69. }
  70.  
  71. /**
  72. * Extracts taxonomy terms and adds them as meta keywords to the header.
  73. * Implements hook_field_formatter_view().
  74. */
  75. function YOUR_MODULE_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  76. switch ($display['type']) {
  77. case 'YOUR_MODULE_taxonomy_term_reference_link':
  78. // Reset to base taxonomy formatter name
  79. $display['type'] = 'taxonomy_term_reference_link';
  80. YOUR_MODULE_taxonomy_terms_add_head($items);
  81. break;
  82. case 'YOUR_MODULE_taxonomy_term_reference_plain':
  83. // Reset to base taxonomy formatter name
  84. $display['type'] = 'taxonomy_term_reference_plain';
  85. YOUR_MODULE_taxonomy_terms_add_head($items);
  86. break;
  87. }
  88.  
  89. // Call base function
  90. return taxonomy_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display);
  91. }
  92.  
  93. /**
  94. * Sets meta keywords to the header using the terms of the $items array.
  95. * Does not set keywords for front page.
  96. * @return
  97. * Returns FALSE in case of front page, otherwise TRUE.
  98. */
  99. function YOUR_MODULE_taxonomy_terms_add_head($items) {
  100. if (!drupal_is_front_page()) {
  101. $terms = array();
  102. foreach ($items as $delta => $item) {
  103. $name = ($item['tid'] != 'autocreate' ? $item['taxonomy_term']->name : $item['name']);
  104. $terms[] = check_plain($name);
  105. }
  106.  
  107. _YOUR_MODULE_add_head(array(
  108. 'name' => 'keywords',
  109. 'content' => implode(', ', $terms),
  110. ));
  111. return TRUE;
  112. }
  113. return FALSE;
  114. }
  115. ?>