metatag_views.module 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * @file
  4. * Provides native meta tag integration with Views.
  5. */
  6. // This is used to join the view name and the display name when building the
  7. // context string.
  8. define('METATAG_VIEWS_CONTEXT_SEPARATOR', '__');
  9. /**
  10. * Implements hook_views_api().
  11. */
  12. function metatag_views_views_api() {
  13. return array('api' => 3.0);
  14. }
  15. /**
  16. * Implements hook_ctools_plugin_api().
  17. */
  18. function metatag_views_ctools_plugin_api($owner, $api) {
  19. if ($owner == 'metatag' && $api == 'metatag') {
  20. return array('version' => 1);
  21. }
  22. }
  23. /**
  24. * Implements hook_view_preview_info_alter().
  25. */
  26. function metatag_views_views_preview_info_alter(&$rows, $view) {
  27. $metatags = $view->display_handler->get_option('metatags');
  28. if (!is_array($metatags) || empty($metatags)) {
  29. return;
  30. }
  31. // If meta tags were found but they're not nested for the language, fix it.
  32. // This leaves some possibility for future versions to support translation.
  33. if (!empty($metatags) && !isset($metatags[LANGUAGE_NONE])) {
  34. $metatags = array(LANGUAGE_NONE => $metatags);
  35. }
  36. // Set the page title to be the previewed views title before fetching meta
  37. // tag values.
  38. $title = drupal_set_title();
  39. if ($view_title = $view->get_title()) {
  40. drupal_set_title($view_title);
  41. }
  42. $instance = 'view:' . $view->name;
  43. $options['token data']['view'] = $view;
  44. $values = metatag_metatags_values($instance, $metatags, $options);
  45. foreach ($values as $metatag => $value) {
  46. $metatag_info = metatag_get_info('tags', $metatag);
  47. $values[$metatag] = $metatag_info['label'] . ': ' . check_plain($value);
  48. }
  49. if (!empty($values)) {
  50. $rows['query'][] = array(
  51. '<strong>' . t('Meta tags') . '</strong>',
  52. implode('<br />', $values),
  53. );
  54. }
  55. // Restore the page title.
  56. drupal_set_title($title);
  57. }
  58. /**
  59. * Implements hook_page_alter().
  60. */
  61. function metatag_views_page_alter(&$page) {
  62. // By default do not add meta tags to admin pages. To enable meta tags on
  63. // admin pages set the 'metatag_tag_admin_pages' variable to TRUE.
  64. if (path_is_admin(current_path()) && !variable_get('metatag_tag_admin_pages', FALSE)) {
  65. return;
  66. }
  67. $view = views_get_page_view();
  68. // Check if Views metatags are enabled.
  69. if (!empty($view) && metatag_config_is_enabled('view')) {
  70. global $language;
  71. // The following is taken from views_get_page_view().
  72. // If a module is still putting in the display like we used to, catch that.
  73. if (is_subclass_of($view, 'views_plugin_display')) {
  74. $view = $view->view;
  75. }
  76. // Prevent Views settings from overwriting global:frontpage.
  77. if (drupal_is_front_page() && metatag_config_is_enabled('global:frontpage')) {
  78. return;
  79. }
  80. // Include only view name by default.
  81. $instance = 'view:' . $view->name;
  82. // Include display name if option is overridden.
  83. if (!$view->display_handler->is_defaulted('metatags')) {
  84. $instance = 'view:' . $view->name . ':' . $view->current_display;
  85. }
  86. // Load the meta tags for this view.
  87. $metatags = $view->display_handler->get_option('metatags');
  88. // Only proceed if there's something to work with.
  89. if (!empty($metatags) && is_array($metatags)) {
  90. // If meta tags were found but they're not nested for the language, fix
  91. // it. This leaves some possibility for future versions to support
  92. // translation.
  93. if (!isset($metatags[LANGUAGE_NONE])) {
  94. $metatags = array(LANGUAGE_NONE => $metatags);
  95. }
  96. // Translate all of the meta tags using i18n, but don't update the
  97. // strings.
  98. metatag_translate_metatags($metatags[LANGUAGE_NONE], 'metatag_views:' . $view->name . METATAG_VIEWS_CONTEXT_SEPARATOR . $view->current_display, NULL, FALSE);
  99. // Build options for meta tag rendering.
  100. $options = array();
  101. $options['token data']['view'] = $view;
  102. $options['language'] = $language->language;
  103. // The page region can be changed.
  104. $region = variable_get('metatag_page_region', 'content');
  105. // Add the metatags.
  106. $page[$region]['metatags'][$instance] = metatag_metatags_view($instance, $metatags, $options);
  107. }
  108. }
  109. }