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
|
|
82
|
// If the view result is cached, then the result may include fids that no
|
83
|
// longer exist.
|
84
|
if (!isset($files[$row->fid])) {
|
85
|
unset($vars['rows'][$index]);
|
86
|
continue;
|
87
|
}
|
88
|
|
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
|
drupal_add_js(array(
|
101
|
'media' => array(
|
102
|
'browser' => array(
|
103
|
'params' => media_get_browser_params(),
|
104
|
'views' => array(
|
105
|
$vars['view']->name => array(
|
106
|
$vars['view']->current_display,
|
107
|
),
|
108
|
),
|
109
|
),
|
110
|
),
|
111
|
), 'setting');
|
112
|
|
113
|
// Add classes and wrappers from the style plugin.
|
114
|
$handler = $vars['view']->style_plugin;
|
115
|
|
116
|
$class = explode(' ', $handler->options['class']);
|
117
|
$class = array_map('drupal_clean_css_identifier', $class);
|
118
|
|
119
|
$wrapper_class = explode(' ', $handler->options['wrapper_class']);
|
120
|
$wrapper_class = array_map('drupal_clean_css_identifier', $wrapper_class);
|
121
|
|
122
|
$vars['class'] = implode(' ', $class);
|
123
|
$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
|
$vars['list_type_suffix'] = '</' . $handler->options['type'] . '>';
|
128
|
|
129
|
// Run theming variables through a standard Views preprocess function.
|
130
|
template_preprocess_views_view_unformatted($vars);
|
131
|
|
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
|
* Implements hook_views_invalidate_cache().
|
138
|
*/
|
139
|
function media_views_invalidate_cache() {
|
140
|
drupal_static_reset('media_get_browser_plugin_info');
|
141
|
}
|