media.views.inc 4.46 KB
Newer Older
1 2
<?php

3 4 5 6 7
/**
 * @file
 * Provide Views data and handlers for media.module
 */

8 9
/**
 * Implements hook_views_plugins().
10 11
 *
 * Generate a list of which base-tables to enabled the plugins for.
12 13 14 15
 */
function media_views_plugins() {
  $plugins = array();

16 17 18 19 20
  // Always allow the actual file-table
  $base = array('file_managed');

  if (module_exists('search_api')) {
    // If the Search API module exists, also allow indices of the file-entity
21
    // that has the fid field indexed.
22
    $indices = search_api_index_load_multiple(FALSE);
23
    foreach ($indices as $machine_name => $index) {
24 25 26 27 28 29
      if ($index->item_type == 'file' && isset($index->options['fields']['fid'])) {
        $base[] = 'search_api_index_' . $machine_name;
      }
    }
  }

30 31 32 33 34 35
  // Display plugin.
  $plugins['display']['media_browser'] = array(
    'title' => t('Media browser tab'),
    'help' => t('Display as a tab in the media browser.'),
    'handler' => 'media_views_plugin_display_media_browser',
    'theme' => 'views_view',
36
    'base' => $base,
37 38 39 40 41 42 43 44 45 46 47
    'use ajax' => TRUE,
    'use pager' => TRUE,
    'accept attachments' => TRUE,
  );

  // Style plugin.
  $plugins['style']['media_browser'] = array(
    'title' => t('Media browser'),
    'help' => t('Displays rows as an HTML list.'),
    'handler' => 'media_views_plugin_style_media_browser',
    'theme' => 'media_views_view_media_browser',
48
    'base' => $base,
49 50 51 52 53 54 55 56 57 58 59 60 61 62
    'uses row plugin' => FALSE,
    'uses row class' => FALSE,
    'uses options' => FALSE,
    'uses fields' => FALSE,
    'type' => 'normal',
    'help topic' => 'style-media-browser',
  );
  return $plugins;
}

/**
 * Display the view as a media browser.
 */
function template_preprocess_media_views_view_media_browser(&$vars) {
63
  module_load_include('inc', 'media', 'includes/media.browser');
64 65 66
  // Load file objects for each View result.
  $fids = array();
  foreach ($vars['rows'] as $index => $row) {
67 68
    // The Search API module returns the row in a slightly different format,
    // so convert it to the format that the normal file_managed table returns.
69 70 71
    if (!empty($row->entity->fid)) {
      $vars['rows'][$index]->fid = $row->entity->fid;
    }
72 73 74 75 76
    $fids[$index] = $row->fid;
  }
  $files = file_load_multiple($fids);

  // Render the preview for each file.
77 78 79
  $params = media_get_browser_params();
  $view_mode = isset($params['view_mode']) ? $params['view_mode'] : 'preview';

80
  foreach ($vars['rows'] as $index => $row) {
81 82 83 84 85 86 87 88

    // If the view result is cached, then the result may include fids that no
    // longer exist.
    if (!isset($files[$row->fid])) {
      unset($vars['rows'][$index]);
      continue;
    }

89
    $file = $files[$row->fid];
90
    // Add url/preview to the file object.
91
    media_browser_build_media_item($file, $view_mode);
92
    $vars['rows'][$index] = $file;
93
    $vars['rows'][$index]->preview = $file->preview;
94 95
  }

96
  // Add the files to JS so that they are accessible inside the browser.
97
  drupal_add_js(array('media' => array('files' => array_values($files))), 'setting');
98

99
  // Add the browser parameters to the settings and that this display exists.
100 101 102 103 104 105
  drupal_add_js(array(
    'media' => array(
      'browser' => array(
        'params' => media_get_browser_params(),
        'views' => array(
          $vars['view']->name => array(
106
            $vars['view']->current_display,
107 108 109 110 111 112
          ),
        ),
      ),
    ),
  ), 'setting');

113
  // Add classes and wrappers from the style plugin.
114
  $handler = $vars['view']->style_plugin;
115 116 117 118 119 120 121 122 123

  $class = explode(' ', $handler->options['class']);
  $class = array_map('drupal_clean_css_identifier', $class);

  $wrapper_class = explode(' ', $handler->options['wrapper_class']);
  $wrapper_class = array_map('drupal_clean_css_identifier', $wrapper_class);

  $vars['class'] = implode(' ', $class);
  $vars['wrapper_class'] = implode(' ', $wrapper_class);
124
  $vars['wrapper_prefix'] = '<div class="' . implode(' ', $wrapper_class) . '">';
125
  $vars['wrapper_suffix'] = '</div>';
126
  $vars['list_type_prefix'] = '<' . $handler->options['type'] . ' id="media-browser-library-list" class="' . implode(' ', $class) . '">';
127 128
  $vars['list_type_suffix'] = '</' . $handler->options['type'] . '>';

129
  // Run theming variables through a standard Views preprocess function.
130 131
  template_preprocess_views_view_unformatted($vars);

132
  // Add media browser JavaScript and CSS.
133
  drupal_add_js(drupal_get_path('module', 'media') . '/js/plugins/media.views.js');
134
}
135 136 137 138 139 140 141

/**
 * Implements hook_views_invalidate_cache().
 */
function media_views_invalidate_cache() {
  drupal_static_reset('media_get_browser_plugin_info');
}