metatag.tokens.inc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * @file
  4. * Custom tokens for Metatag.
  5. */
  6. /**
  7. * Implements hook_token_info().
  8. */
  9. function metatag_token_info() {
  10. $info = array();
  11. $info['types']['metatag'] = array(
  12. 'name' => t('Meta tags'),
  13. 'description' => t('Generated by the Metatag module, may not be used to fill in other meta tags.'),
  14. );
  15. $metatag_info = metatag_get_info();
  16. foreach($metatag_info['tags'] as $value) {
  17. $info['tokens']['metatag'][$value['name']] = array(
  18. 'name' => $value['label'],
  19. 'description' => $value['description']
  20. );
  21. }
  22. if (module_exists('taxonomy')) {
  23. $info['tokens']['term']['metatag'] = array(
  24. 'name' => t('Metatag.'),
  25. 'description' => t('Metatag.'),
  26. 'type' => 'metatag'
  27. );
  28. }
  29. if (module_exists('node')) {
  30. $info['tokens']['node']['metatag'] = array(
  31. 'name' => t('Metatag.'),
  32. 'description' => t('Metatag.'),
  33. 'type' => 'metatag'
  34. );
  35. }
  36. return $info;
  37. }
  38. /**
  39. * Implements hook_tokens().
  40. */
  41. function metatag_tokens($type, $tokens, array $data = array(), array $options = array()) {
  42. $replacements = array();
  43. $sanitize = !empty($options['sanitize']);
  44. // Metatag tokens.
  45. if ($type == 'metatag' && !empty($data['metatag'])) {
  46. $metatag = $data['metatag'];
  47. foreach ($tokens as $name => $original) {
  48. if(isset($metatag[$name])){
  49. $replacements[$original] = $sanitize ? filter_xss($metatag[$name]) : $metatag[$name];
  50. }
  51. }
  52. }
  53. // Token tokens.
  54. if ($type == 'term' && !empty($data['term'])) {
  55. $term = $data['term'];
  56. if ($metatag_tokens = token_find_with_prefix($tokens, 'metatag')) {
  57. $result = metatag_token_generate_array($term, 'taxonomy_term', $term->vocabulary_machine_name);
  58. $replacements += token_generate('metatag', $metatag_tokens, array('metatag' => $result), $options);
  59. }
  60. }
  61. // Node tokens.
  62. if ($type == 'node' && !empty($data['node'])) {
  63. $node = $data['node'];
  64. if ($metatag_tokens = token_find_with_prefix($tokens, 'metatag')) {
  65. $result = metatag_token_generate_array($node, 'node', $node->type);
  66. $replacements += token_generate('metatag', $metatag_tokens, array('metatag' => $result), $options);
  67. }
  68. }
  69. return $replacements;
  70. }
  71. /**
  72. * Implements hook_tokens_alter().
  73. *
  74. * Fix [node:summary] until http://drupal.org/node/1295524 is committed, this
  75. * code is retrofitted from the patches in that issue.
  76. */
  77. function metatag_tokens_alter(array &$replacements, array $context) {
  78. // Only proceed if this is working on a node.
  79. if ($context['type'] == 'node' && !empty($context['data']['node'])) {
  80. // Loop through each of the tokens.
  81. foreach ($context['tokens'] as $name => $original) {
  82. // Only deal with the 'node:summary' token, that's the one being fixed.
  83. if ($name == 'summary') {
  84. // A shortcut to the node being processed.
  85. $node = $context['data']['node'];
  86. // Work out the langcode being used.
  87. if (isset($context['options']['language'])) {
  88. $langcode = $context['options']['language']->language;
  89. }
  90. else {
  91. $langcode = NULL;
  92. }
  93. // Decide whether the string needs to be sanitized.
  94. $sanitize = !empty($context['options']['sanitize']);
  95. if ($items = field_get_items('node', $node, 'body', $langcode)) {
  96. $instance = field_info_instance('node', 'body', $node->type);
  97. $field_langcode = field_language('node', $node, 'body', $langcode);
  98. // If the summary is not empty, use it.
  99. if (!empty($items[0]['summary'])) {
  100. $output = $sanitize ? _text_sanitize($instance, $field_langcode, $items[0], 'summary') : $items[0]['summary'];
  101. }
  102. // Attempt to provide a suitable version of the 'body' field.
  103. else {
  104. $output = $sanitize ? _text_sanitize($instance, $field_langcode, $items[0], 'value') : $items[0]['value'];
  105. // A summary was requested.
  106. if ($name == 'summary') {
  107. if (isset($instance['display']['teaser']['settings']['trim_length'])) {
  108. $trim_length = $instance['display']['teaser']['settings']['trim_length'];
  109. }
  110. else {
  111. // Use default value.
  112. $trim_length = NULL;
  113. }
  114. // Generate an optionally trimmed summary of the body field.
  115. $output = text_summary($output, $instance['settings']['text_processing'] ? $items[0]['format'] : NULL, $trim_length);
  116. }
  117. }
  118. // Override the original value.
  119. $replacements[$original] = $output;
  120. }
  121. }
  122. }
  123. }
  124. }
  125. /**
  126. * Generate an array of meta tags for a given entity.
  127. */
  128. function metatag_token_generate_array($entity, $entity_type, $bundle) {
  129. if (metatag_entity_supports_metatags($entity_type, $bundle)) {
  130. $token_type = token_get_entity_mapping('entity', $entity_type);
  131. $instance = "{$entity_type}:{$bundle}";
  132. $options = array();
  133. $options['token data'][$token_type] = $entity;
  134. $options['entity'] = $entity;
  135. $metatags = isset($entity->metatags) ? $entity->metatags : array();
  136. $metatags += metatag_config_load_with_defaults($instance);
  137. $result = array();
  138. foreach ($metatags as $metatag => $data) {
  139. if ($metatag_instance = metatag_get_instance($metatag, $data)) {
  140. $result[$metatag] = $metatag_instance->getValue($options);
  141. }
  142. }
  143. return $result;
  144. }
  145. return NULL;
  146. }