metatag_hreflang.tokens.inc 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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_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. // Only generate tokens if there are multiple translations.
  51. if (isset($node->translations) && !empty($node->translations->data)) {
  52. $languages = language_list('enabled');
  53. if (!empty($languages[1]) && is_array($languages[1]) && count($languages[1]) > 1) {
  54. foreach ($tokens as $name => $original) {
  55. // The original entity's URL.
  56. if ($name == 'url-original') {
  57. if (isset($node->translations->original, $languages[1][$node->translations->original])) {
  58. $url_options = $options;
  59. $url_options['language'] = $languages[1][$node->translations->original];
  60. $url_options['absolute'] = TRUE;
  61. $replacements[$original] = url('node/' . $node->nid, $url_options);
  62. }
  63. }
  64. // Separate URLs for each translation.
  65. foreach ($node->translations->data as $langcode => $translation) {
  66. if ($name == 'url-' . $langcode) {
  67. $url_options = $options;
  68. $url_options['language'] = $languages[1][$langcode];
  69. $url_options['absolute'] = TRUE;
  70. $replacements[$original] = url('node/' . $node->nid, $url_options);
  71. }
  72. }
  73. }
  74. }
  75. }
  76. }
  77. return $replacements;
  78. }