Projet

Général

Profil

Paste
Télécharger (4,63 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / media / media.views.inc @ 01f36513

1
<?php
2

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

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

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

    
19
  if (module_exists('search_api')) {
20
    // 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
      if ($index->item_type == 'file' && isset($index->options['fields']['fid'])) {
25
        $base[] = 'search_api_index_' . $machine_name;
26
      }
27
    }
28
  }
29

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

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

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

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

    
81
  foreach ($vars['rows'] as $index => $row) {
82

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

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

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

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

    
115
  // Add classes and wrappers from the style plugin.
116
  $handler = $vars['view']->style_plugin;
117

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

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

    
124
  $vars['class'] = implode(' ', $class);
125
  $vars['wrapper_class'] = implode(' ', $wrapper_class);
126
  $vars['wrapper_prefix'] = '<div class="' . implode(' ', $wrapper_class) . '">';
127
  $vars['wrapper_suffix'] = '</div>';
128
  $vars['list_type_prefix'] = '<' . $handler->options['type'] . ' id="media-browser-library-list" class="' . implode(' ', $class) . '">';
129
  $vars['list_type_suffix'] = '</' . $handler->options['type'] . '>';
130
  $vars['aria_role'] = $params['multiselect'] ? 'checkbox' : 'radio';
131

    
132
  // Run theming variables through a standard Views preprocess function.
133
  template_preprocess_views_view_unformatted($vars);
134

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

    
139
/**
140
 * Implements hook_views_invalidate_cache().
141
 */
142
function media_views_invalidate_cache() {
143
  drupal_static_reset('media_get_browser_plugin_info');
144
}