metatag_hreflang.metatag.inc 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * @file
  4. * Metatag integration for the Metatag:hreflang module.
  5. */
  6. /**
  7. * Implements hook_metatag_bundled_config_alter().
  8. *
  9. * This provides recommended defaults that should be sufficient for most sites.
  10. */
  11. function metatag_hreflang_metatag_bundled_config_alter(array &$configs) {
  12. // This only makes sense if either the Translation or Entity Translation
  13. // modules are enabled.
  14. if (!(module_exists('translation') || module_exists('entity_translation'))) {
  15. return;
  16. }
  17. foreach ($configs as &$config) {
  18. switch ($config->instance) {
  19. case 'node':
  20. // The x-default should default to the source language.
  21. $config->config += array(
  22. 'hreflang_xdefault' => array('value' => '[node:url-original]'),
  23. );
  24. // Add all of the other hreflang values.
  25. $languages = language_list('enabled');
  26. if (!empty($languages[1])) {
  27. foreach (array_keys($languages[1]) as $langcode) {
  28. $config->config += array(
  29. 'hreflang_' . $langcode => array('value' => '[node:url-' . $langcode . ']'),
  30. );
  31. }
  32. }
  33. break;
  34. }
  35. }
  36. }
  37. /**
  38. * Implements hook_metatag_info().
  39. */
  40. function metatag_hreflang_metatag_info() {
  41. $info['groups']['hreflang'] = array(
  42. 'label' => t('Alternative language links (hreflang)'),
  43. 'description' => t('These meta tags are designed to point visitors to versions of the current page in other languages. It is recommended to use the default "[node:url-LANGCODE]" tokens for the hreflang values, they will only be output if a translation exists for that locale. Also, it is suggested to use the "[node:source:url]" token for the default locale.'),
  44. 'form' => array(
  45. '#weight' => 60,
  46. ),
  47. );
  48. $weight = 100;
  49. // Default values for each meta tag.
  50. $tag_info_defaults = array(
  51. 'description' => '',
  52. 'class' => 'DrupalLinkMetaTag',
  53. 'group' => 'hreflang',
  54. );
  55. $info['tags']['hreflang_xdefault'] = array(
  56. 'label' => t('Default locale (x-default)'),
  57. 'description' => t('This should point to the version of the page that is for the main or primary locale, e.g. the original version of an article that is translated into other languages.'),
  58. 'weight' => $weight,
  59. 'element' => array(
  60. '#theme' => 'metatag_link_hreflang',
  61. '#hreflang' => 'x-default',
  62. ),
  63. ) + $tag_info_defaults;
  64. if (variable_get('metatag_hreflang_allow_dupe', FALSE)) {
  65. $info['tags']['hreflang_xdefault']['description'] .= ' ' . t('If this string matches one of the values below the other value will not be removed.');
  66. }
  67. else {
  68. $info['tags']['hreflang_xdefault']['description'] .= ' ' . t('If this string matches one of the values below that other tag will be removed, thus ensuring that there is only one hreflang meta tag for each possible variation of this page.');
  69. }
  70. // Add a meta tag for each locale enabled.
  71. $languages = language_list('enabled');
  72. if (!empty($languages[1])) {
  73. foreach ($languages[1] as $langcode => $locale) {
  74. // Getting granular with these so they're all grouped together.
  75. $weight += 0.01;
  76. $info['tags']['hreflang_' . $langcode] = array(
  77. 'label' => t('hreflang value for :language (:native)', array(':language' => t($locale->name), ':native' => $locale->native)),
  78. 'weight' => $weight,
  79. 'element' => array(
  80. '#theme' => 'metatag_link_hreflang',
  81. '#hreflang' => $langcode,
  82. ),
  83. ) + $tag_info_defaults;
  84. }
  85. }
  86. return $info;
  87. }