metatag_views.module 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * @file
  4. * Provides native meta tag integration with Views.
  5. */
  6. /**
  7. * Implements hook_views_api().
  8. */
  9. function metatag_views_views_api() {
  10. return array('api' => 3.0);
  11. }
  12. /**
  13. * Implements hook_ctools_plugin_api().
  14. */
  15. function metatag_views_ctools_plugin_api($owner, $api) {
  16. if ($owner == 'metatag' && $api == 'metatag') {
  17. return array('version' => 1);
  18. }
  19. }
  20. /**
  21. * Implements hook_view_preview_info_alter().
  22. */
  23. function metatag_views_views_preview_info_alter(&$rows, $view) {
  24. $metatags = $view->display_handler->get_option('metatags');
  25. if (!is_array($metatags) || empty($metatags)) {
  26. return;
  27. }
  28. // If meta tags were found but they're not nested for the language, fix it.
  29. // This leaves some possibility for future versions to support translation.
  30. if (!empty($metatags) && !isset($metatags[LANGUAGE_NONE])) {
  31. $metatags = array(LANGUAGE_NONE => $metatags);
  32. }
  33. // Set the page title to be the previewed views title before fetching meta
  34. // tag values.
  35. $title = drupal_set_title();
  36. if ($view_title = $view->get_title()) {
  37. drupal_set_title($view_title);
  38. }
  39. $instance = 'view:' . $view->name;
  40. $options['token data']['view'] = $view;
  41. $values = metatag_metatags_values($instance, $metatags, $options);
  42. foreach ($values as $metatag => $value) {
  43. $metatag_info = metatag_get_info('tags', $metatag);
  44. $values[$metatag] = check_plain($metatag_info['label']) . ': ' . check_plain($value);
  45. }
  46. if (!empty($values)) {
  47. $rows['query'][] = array(
  48. '<strong>' . t('Meta tags') . '</strong>',
  49. implode('<br />', $values),
  50. );
  51. }
  52. // Restore the page title.
  53. drupal_set_title($title);
  54. }
  55. /**
  56. * Implements hook_page_alter().
  57. */
  58. function metatag_views_page_alter(&$page) {
  59. $view = views_get_page_view();
  60. // Check if Views metatags are enabled.
  61. if (!empty($view) && metatag_config_is_enabled('view')) {
  62. // The following is taken from views_get_page_view().
  63. // If a module is still putting in the display like we used to, catch that.
  64. if (is_subclass_of($view, 'views_plugin_display')) {
  65. $view = $view->view;
  66. }
  67. // Prevent Views settings from overwriting global:frontpage.
  68. if (drupal_is_front_page() && metatag_config_is_enabled('global:frontpage')) {
  69. return;
  70. }
  71. // Load the meta tags for this view.
  72. $metatags = $view->display_handler->get_option('metatags');
  73. if (!is_array($metatags) || empty($metatags)) {
  74. $metatags = array();
  75. }
  76. // If meta tags were found but they're not nested for the language, fix it.
  77. // This leaves some possibility for future versions to support translation.
  78. if (!empty($metatags) && !isset($metatags[LANGUAGE_NONE])) {
  79. $metatags = array(LANGUAGE_NONE => $metatags);
  80. }
  81. // Build options for meta tag rendering.
  82. $instance = 'view:' . $view->name;
  83. $options = array();
  84. $options['token data']['view'] = $view;
  85. // The page region can be changed.
  86. $region = variable_get('metatag_page_region', 'content');
  87. // Add the metatags.
  88. $page[$region]['metatags'][$instance] = metatag_metatags_view($instance, $metatags, $options);
  89. }
  90. }