Projet

Général

Profil

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

root / htmltest / sites / all / modules / media / media.views.inc @ a5572547

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
  foreach ($vars['rows'] as $index => $row) {
78
    $file = $files[$row->fid];
79
    // Add url/preview to the file object.
80
    media_browser_build_media_item($file);
81
    $vars['rows'][$index] = $file;
82
    $vars['rows'][$index]->preview = $file->preview;
83
  }
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

    
88
  // Add the browser parameters to the settings and that this display exists.
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

    
102
  // Add classes and wrappers from the style plugin.
103
  $handler = $vars['view']->style_plugin;
104

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

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

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

    
118
  // Run theming variables through a standard Views preprocess function.
119
  template_preprocess_views_view_unformatted($vars);
120

    
121
  // Add media browser javascript and CSS.
122
  drupal_add_js(drupal_get_path('module', 'media') . '/js/plugins/media.views.js');
123
}
124

    
125
/**
126
 * Implements hook_views_invalidate_cache().
127
 */
128
function media_views_invalidate_cache() {
129
  cache_clear_all('media:browser:plugin', 'cache', TRUE);
130
  drupal_static_reset('media_get_browser_plugin_info');
131
}