metatag_hreflang.tokens.inc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * @file
  4. * Custom tokens for Metatag:hreflang.
  5. */
  6. /**
  7. * Implements hook_token_info().
  8. */
  9. function metatag_hreflang_token_info() {
  10. // This only makes sense if either the Translation or Entity Translation
  11. // modules are enabled.
  12. if (!(module_exists('translation') || module_exists('entity_translation'))) {
  13. return;
  14. }
  15. // Don't do anything if the patch was applied to Entity Translation to add
  16. // these.
  17. // @see https://www.drupal.org/node/2603056
  18. if (module_exists('entity_translation') && module_load_include('tokens.inc', 'entity_translation')) {
  19. return;
  20. }
  21. $info = array();
  22. $languages = language_list('enabled');
  23. // Verify there are multiple languages enabled.
  24. if (!empty($languages[1]) && is_array($languages[1]) && count($languages[1]) > 1) {
  25. if (module_exists('node')) {
  26. foreach ($languages[1] as $langcode => $language) {
  27. $info['tokens']['node']['url-' . $langcode] = array(
  28. 'name' => t('URL (@lang translation)', array('@lang' => $language->name)),
  29. 'description' => t('The URL for the %lang translation of the node, if available.', array('%lang' => $language->name)),
  30. );
  31. }
  32. }
  33. }
  34. $info['tokens']['node']['url-original'] = array(
  35. 'name' => t('URL (original language)'),
  36. 'description' => t("The URL for the node that is the original source for the current node's translation."),
  37. );
  38. return $info;
  39. }
  40. /**
  41. * Implements hook_tokens().
  42. */
  43. function metatag_hreflang_tokens($type, $tokens, array $data = array(), array $options = array()) {
  44. $replacements = array();
  45. $sanitize = !empty($options['sanitize']);
  46. // Node tokens.
  47. if ($type == 'node' && !empty($data['node'])) {
  48. // Shortcuts.
  49. $node = $data['node'];
  50. $languages = language_list('enabled');
  51. if (!empty($languages[1]) && is_array($languages[1]) && count($languages[1]) > 1) {
  52. foreach ($tokens as $name => $original) {
  53. // The original entity's URL.
  54. if ($name == 'url-original') {
  55. // Basic options regardless of the translation mechanism used.
  56. $url_options = $options;
  57. $url_options['absolute'] = TRUE;
  58. // Core's content translation.
  59. if (!empty($node->tnid)) {
  60. $node_original = node_load($node->tnid);
  61. // Only deal with published nodes.
  62. if ($node_original->status) {
  63. $url_options['language'] = $languages[1][$node_original->language];
  64. $replacements[$original] = url('node/' . $node_original->nid, $url_options);
  65. }
  66. }
  67. // Entity Translation stores the translation data differently to core.
  68. elseif (isset($node->translations, $node->translations->original, $languages[1][$node->translations->original])) {
  69. $url_options['language'] = $languages[1][$node->translations->original];
  70. $replacements[$original] = url('node/' . $node->nid, $url_options);
  71. }
  72. }
  73. // Separate URLs for each translation.
  74. // Core's content translation.
  75. if (!empty($node->tnid)) {
  76. $translations = translation_node_get_translations($node->tnid);
  77. foreach ($translations as $langcode => $translation) {
  78. // Only deal with published nodes.
  79. if ($translation->status && $name == 'url-' . $langcode) {
  80. $url_options = $options;
  81. $url_options['absolute'] = TRUE;
  82. $url_options['language'] = $languages[1][$langcode];
  83. $replacements[$original] = url('node/' . $translation->nid, $url_options);
  84. }
  85. }
  86. }
  87. // Entity Translation.
  88. elseif (isset($node->translations, $node->translations->data)) {
  89. foreach ($node->translations->data as $langcode => $translation) {
  90. // Only deal with published nodes.
  91. if ($translation['status'] && $name == 'url-' . $langcode) {
  92. $url_options = $options;
  93. $url_options['absolute'] = TRUE;
  94. $url_options['language'] = $languages[1][$langcode];
  95. $replacements[$original] = url('node/' . $node->nid, $url_options);
  96. }
  97. }
  98. }
  99. }
  100. }
  101. }
  102. return $replacements;
  103. }