Projet

Général

Profil

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

root / drupal7 / sites / all / modules / media / media.views.inc @ 2b3c8cc1

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(NULL);
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
    'base' => $base,
37
    'use ajax' => TRUE,
38
    'use pager' => TRUE,
39
    'accept attachments' => TRUE,
40
  );
41

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

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

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

    
80
  foreach ($vars['rows'] as $index => $row) {
81
    $file = $files[$row->fid];
82
    // Add url/preview to the file object.
83
    media_browser_build_media_item($file, $view_mode);
84
    $vars['rows'][$index] = $file;
85
    $vars['rows'][$index]->preview = $file->preview;
86
  }
87

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

    
91
  // Add the browser parameters to the settings and that this display exists.
92
  drupal_add_js(array(
93
    'media' => array(
94
      'browser' => array(
95
        'params' => media_get_browser_params(),
96
        'views' => array(
97
          $vars['view']->name => array(
98
            $vars['view']->current_display,
99
          ),
100
        ),
101
      ),
102
    ),
103
  ), 'setting');
104

    
105
  // Add classes and wrappers from the style plugin.
106
  $handler = $vars['view']->style_plugin;
107

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

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

    
114
  $vars['class'] = implode(' ', $class);
115
  $vars['wrapper_class'] = implode(' ', $wrapper_class);
116
  $vars['wrapper_prefix'] = '<div class="' . implode(' ', $wrapper_class) . '">';
117
  $vars['wrapper_suffix'] = '</div>';
118
  $vars['list_type_prefix'] = '<' . $handler->options['type'] . ' id="media-browser-library-list" class="' . implode(' ', $class) . '">';
119
  $vars['list_type_suffix'] = '</' . $handler->options['type'] . '>';
120

    
121
  // Run theming variables through a standard Views preprocess function.
122
  template_preprocess_views_view_unformatted($vars);
123

    
124
  // Add media browser javascript and CSS.
125
  drupal_add_js(drupal_get_path('module', 'media') . '/js/plugins/media.views.js');
126
}
127

    
128
/**
129
 * Implements hook_views_invalidate_cache().
130
 */
131
function media_views_invalidate_cache() {
132
  drupal_static_reset('media_get_browser_plugin_info');
133
}