metatag_hreflang.module 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * @file
  4. * Primary hook implementations for Metatag:hreflang.
  5. */
  6. // @todo Clear caches for all versions of an entity when a translation is edited
  7. // so that the hreflang meta tags update appropriately.
  8. /**
  9. * Implements hook_ctools_plugin_api().
  10. */
  11. function metatag_hreflang_ctools_plugin_api($owner, $api) {
  12. if ($owner == 'metatag' && $api == 'metatag') {
  13. return array('version' => 1);
  14. }
  15. }
  16. /**
  17. * Implements hook_theme().
  18. */
  19. function metatag_hreflang_theme() {
  20. $info['metatag_link_hreflang'] = array(
  21. 'render element' => 'element',
  22. );
  23. return $info;
  24. }
  25. /**
  26. * Theme callback for a rel link tag with the hreflang.
  27. *
  28. * The format is:
  29. * <link rel="[name]" hreflang="[langcode]" href="[value]" />
  30. */
  31. function theme_metatag_link_hreflang($variables) {
  32. $element = &$variables['element'];
  33. $element['#name'] = 'alternate';
  34. $args = array(
  35. '#name' => 'rel',
  36. '#hreflang' => 'hreflang',
  37. '#value' => 'href',
  38. );
  39. element_set_attributes($element, $args);
  40. unset($element['#value']);
  41. return theme('html_tag', $variables);
  42. }
  43. /**
  44. * Implements hook_form_FORM_ID_alter for metatag_admin_settings_form().
  45. */
  46. function metatag_hreflang_form_metatag_admin_settings_form_alter(&$form, &$form_state, $form_id) {
  47. $form['advanced']['metatag_hreflang_allow_dupe'] = array(
  48. '#type' => 'checkbox',
  49. '#title' => t('Allow hreflang tag that matches the x-default tag'),
  50. '#description' => t('It is recommended to not have hreflang="x-default" and hreflang="SOMELANGCODE" meta tags pointing at the same URL. By default if there is a hreflang="SOMELANGCODE" meta tag with the same URL as the hreflang="x-default" meta tag then the hreflang="SOMELANGCODE" tag will be removed. Checking '),
  51. '#default_value' => variable_get('metatag_hreflang_allow_dupe', FALSE),
  52. );
  53. }
  54. /**
  55. * Implements hook_metatag_metatags_view_alter().
  56. *
  57. * Remove any hreflang="LANGCODE" values that match hreflang="x-default". Using
  58. * this hook instead of hook_html_head_alter() as it gets closer to Metatag's
  59. * data structures, and the results are cached so this won't be executed on
  60. * every page request.
  61. */
  62. function metatag_hreflang_metatag_metatags_view_alter(&$output, $instance, $options) {
  63. // This behaviour may be disabled from the Metatag settings page.
  64. if (!variable_get('metatag_hreflang_allow_dupe', FALSE)) {
  65. if (!empty($output['hreflang_xdefault'])) {
  66. $default = $output['hreflang_xdefault']['#attached']['drupal_add_html_head'][0][0]['#value'];
  67. foreach ($output as $tag_name => &$tag) {
  68. // Skip the x-default tag.
  69. if ($tag_name == 'hreflang_xdefault') {
  70. continue;
  71. }
  72. if (strpos($tag_name, 'hreflang_') === 0) {
  73. if ($tag['#attached']['drupal_add_html_head'][0][0]['#value'] == $default) {
  74. $tag['#attached']['drupal_add_html_head'][0][0]['#access'] = FALSE;
  75. }
  76. }
  77. }
  78. }
  79. }
  80. }