media.views.inc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * @file
  4. * Provide Views data and handlers for media.module
  5. */
  6. /**
  7. * Implements hook_views_plugins().
  8. *
  9. * Generate a list of which base-tables to enabled the plugins for.
  10. */
  11. function media_views_plugins() {
  12. $plugins = array();
  13. // Always allow the actual file-table
  14. $base = array('file_managed');
  15. if (module_exists('search_api')) {
  16. // If the Search API module exists, also allow indices of the file-entity
  17. // that has the fid field indexed.
  18. $indices = search_api_index_load_multiple(FALSE);
  19. foreach ($indices as $machine_name => $index) {
  20. if ($index->item_type == 'file' && isset($index->options['fields']['fid'])) {
  21. $base[] = 'search_api_index_' . $machine_name;
  22. }
  23. }
  24. }
  25. // Display plugin.
  26. $plugins['display']['media_browser'] = array(
  27. 'title' => t('Media browser tab'),
  28. 'help' => t('Display as a tab in the media browser.'),
  29. 'handler' => 'media_views_plugin_display_media_browser',
  30. 'theme' => 'views_view',
  31. 'theme path' => drupal_get_path('module', 'views') . '/theme',
  32. 'base' => $base,
  33. 'use ajax' => TRUE,
  34. 'use pager' => TRUE,
  35. 'accept attachments' => TRUE,
  36. );
  37. // Style plugin.
  38. $plugins['style']['media_browser'] = array(
  39. 'title' => t('Media browser'),
  40. 'help' => t('Displays rows as an HTML list.'),
  41. 'handler' => 'media_views_plugin_style_media_browser',
  42. 'theme' => 'media_views_view_media_browser',
  43. 'base' => $base,
  44. 'uses row plugin' => FALSE,
  45. 'uses row class' => FALSE,
  46. 'uses options' => FALSE,
  47. 'uses fields' => FALSE,
  48. 'type' => 'normal',
  49. 'help topic' => 'style-media-browser',
  50. );
  51. return $plugins;
  52. }
  53. /**
  54. * Display the view as a media browser.
  55. */
  56. function template_preprocess_media_views_view_media_browser(&$vars) {
  57. module_load_include('inc', 'media', 'includes/media.browser');
  58. // Load file objects for each View result.
  59. $fids = array();
  60. foreach ($vars['rows'] as $index => $row) {
  61. // The Search API module returns the row in a slightly different format,
  62. // so convert it to the format that the normal file_managed table returns.
  63. if (!empty($row->entity->fid)) {
  64. $vars['rows'][$index]->fid = $row->entity->fid;
  65. }
  66. $fids[$index] = $row->fid;
  67. }
  68. $files = file_load_multiple($fids);
  69. // Render the preview for each file.
  70. $params = media_get_browser_params();
  71. $view_mode = isset($params['view_mode']) ? $params['view_mode'] : 'preview';
  72. foreach ($vars['rows'] as $index => $row) {
  73. // If the view result is cached, then the result may include fids that no
  74. // longer exist.
  75. if (!isset($files[$row->fid])) {
  76. unset($vars['rows'][$index]);
  77. continue;
  78. }
  79. $file = $files[$row->fid];
  80. // Add url/preview to the file object.
  81. media_browser_build_media_item($file, $view_mode);
  82. $vars['rows'][$index] = $file;
  83. $vars['rows'][$index]->preview = $file->preview;
  84. }
  85. // Add the files to JS so that they are accessible inside the browser.
  86. drupal_add_js(array('media' => array('files' => array_values($files))), 'setting');
  87. // Add the browser parameters to the settings and that this display exists.
  88. $params = media_get_browser_params();
  89. drupal_add_js(array(
  90. 'media' => array(
  91. 'browser' => array(
  92. 'params' => media_get_browser_params(),
  93. 'views' => array(
  94. $vars['view']->name => array(
  95. $vars['view']->current_display,
  96. ),
  97. ),
  98. ),
  99. ),
  100. ), 'setting');
  101. // Add classes and wrappers from the style plugin.
  102. $handler = $vars['view']->style_plugin;
  103. $class = explode(' ', $handler->options['class']);
  104. $class = array_map('drupal_clean_css_identifier', $class);
  105. $wrapper_class = explode(' ', $handler->options['wrapper_class']);
  106. $wrapper_class = array_map('drupal_clean_css_identifier', $wrapper_class);
  107. $vars['class'] = implode(' ', $class);
  108. $vars['wrapper_class'] = implode(' ', $wrapper_class);
  109. $vars['wrapper_prefix'] = '<div class="' . implode(' ', $wrapper_class) . '">';
  110. $vars['wrapper_suffix'] = '</div>';
  111. $vars['list_type_prefix'] = '<' . $handler->options['type'] . ' id="media-browser-library-list" class="' . implode(' ', $class) . '">';
  112. $vars['list_type_suffix'] = '</' . $handler->options['type'] . '>';
  113. $vars['aria_role'] = (isset($params['multiselect']) && $params['multiselect']) ? 'checkbox' : 'radio';
  114. // Run theming variables through a standard Views preprocess function.
  115. template_preprocess_views_view_unformatted($vars);
  116. // Add media browser JavaScript and CSS.
  117. drupal_add_js(drupal_get_path('module', 'media') . '/js/plugins/media.views.js');
  118. }
  119. /**
  120. * Implements hook_views_invalidate_cache().
  121. */
  122. function media_views_invalidate_cache() {
  123. drupal_static_reset('media_get_browser_plugin_info');
  124. }