Commit 5663f4d6 authored by Dave Reid's avatar Dave Reid

Issue #1224766 by robeano, arthurf, Dave Reid, becw: Added support for views...

Issue #1224766 by robeano, arthurf, Dave Reid, becw: Added support for views to be used as media browser tabs. This adds a Views dependency for Media 2.x as we intend to completely replace the current browser with our default view enabled by default.
parent ff088e6d
......@@ -49,7 +49,7 @@ function media_browser($selected = NULL) {
}
foreach ($plugins as $key => &$plugin) {
$plugin += module_invoke($plugin['#module'], 'media_browser_plugin_view', $key, $params);
$plugin += module_invoke($plugin['#module'], 'media_browser_plugin_view', $key, $plugin);
}
// Allow modules to change the tab names or whatever else they want to change
......@@ -221,13 +221,33 @@ function media_media_browser_plugin_info() {
'#weight' => -10,
);
}
// Get a list of views using the 'media_browser' display type and provide each
// as a media browser plugin.
foreach (views_get_all_views(TRUE) as $view) {
foreach ($view->display as $display) {
if ($display->display_plugin == 'media_browser') {
$title = $display->display_title;
if (!empty($display->display_options['title'])) {
$title = $display->display_options['title'];
}
$plugins["{$view->name}--{$display->id}"] = array(
'#title' => $title,
'#weight' => 11, // @TODO make this configurable.
'#views_view_name' => $view->name,
'#views_view_display_id' => $display->id,
);
}
}
}
return $plugins;
}
/**
* Implementaion of hook_media_browser_plugin_view().
*/
function media_media_browser_plugin_view($plugin_name, $params) {
function media_media_browser_plugin_view($plugin_name, $params = array()) {
$path = drupal_get_path('module', 'media');
module_load_include('inc', 'media', 'includes/media.admin');
......@@ -284,6 +304,17 @@ function media_media_browser_plugin_view($plugin_name, $params) {
'#markup' => '<div id="container"><div id="scrollbox"><ul id="media-browser-library-list" class="media-list-thumbnails"></ul><div id="status"></div></div></div>',
);
break;
default:
// Is this coming from our implementation of views?
if (!empty($params['#views_view_name'])) {
if (($view = views_get_view($params['#views_view_name']))) {
return array(
'#markup' => $view->preview($params['#views_view_display_id']) . '<div class="form-actions form-wrapper"></div>',
'#title' => t('Views'),
);
}
}
break;
}
return array();
......
<?php
/**
* @file
* Provide Views data and handlers for media.module
*/
/**
* Implements hook_field_views_data().
*/
function media_field_views_data($field) {
$data = field_views_field_default_views_data($field);
foreach ($data as $table_name => $table_data) {
// Add the relationship only on the fid field.
$data[$table_name][$field['field_name'] . '_fid']['relationship'] = array(
'handler' => 'views_handler_relationship',
'base' => 'file_managed',
'base field' => 'fid',
'field' => $field['field_name'] . '_fid',
'label' => t('file from !field_name', array('!field_name' => $field['field_name'])),
);
}
return $data;
}
<?php
/**
* @file
* Contains the media browser tab display plugin.
*/
/**
* Display plugin to provide a view as a tab in the media browser.
*
* This is currently just a stub--there's nothing special we need to set up for
* rendering a view as a tab in the media browser. It would be possible to
* provide a special method that returns the plugin info for
* media_media_browser_plugin_info() and the render array for
* media_media_browser_plugin_view().
*
* @ingroup views_display_plugins
*/
class media_views_plugin_display_media_browser extends views_plugin_display {}
<?php
/**
* @file
* The media browser style plugin.
*/
/**
// * Style plugin to render media items as an interactive grid for the media
// * browser.
*
* @ingroup views_style_plugins
*/
class media_views_plugin_style_media_browser extends views_plugin_style_list {
// Stores the files loaded with pre_render.
var $files = array();
/**
* Set default options.
*/
function option_definition() {
$options = parent::option_definition();
$options['type'] = array('default' => 'ul');
$options['class'] = array('default' => 'media-list-thumbnails');
$options['wrapper_class'] = array('default' => '');
return $options;
}
// Prevents a problem with views when get_row_class() is not set
function get_row_class($row_index) {
}
/**
* Add the base field (fid) to the query.
*/
function query() {
$this->view->query->add_field($this->view->base_table, $this->view->base_field);
parent::query();
}
}
......@@ -55,12 +55,15 @@ Drupal.media.browser.validateButtons = function() {
// "OK" button action to finalize the selection and remove the IFRAME.
//
// @todo An alternate, less hacky solution would be most welcome.
if (!($('.form-submit', this).length > 0)) {
$('<a class="button fake-ok">' + Drupal.t('Submit') + '</a>').appendTo(this).bind('click', Drupal.media.browser.submit);
if (!($('.fake-cancel', this).length > 0)) {
if (!$('.form-submit', this).length) {
if (!$('.fake-ok', this).length) {
$('<a class="button fake-ok">' + Drupal.t('Submit') + '</a>').appendTo(this).bind('click', Drupal.media.browser.submit);
}
if (!$('.fake-cancel', this).length) {
$('<a class="button fake-cancel">' + Drupal.t('Cancel') + '</a>').appendTo(this).bind('click', Drupal.media.browser.submit);
}
} else if (!($('.fake-cancel', this).length > 0)) {
}
else if (!($('.fake-cancel', this).length > 0)) {
var parent = $('.form-actions', this);
if (!parent.length) {
parent = $('form > div', this);
......
<?php
/**
* @file media-views-view-media-browser.tpl.php
* View template to display a grid of media previews in the media browser.
*
* @see views-view-list.tpl.php
* @see template_preprocess_media_views_view_media_browser()
* @ingroup views_templates
*/
?>
<?php print $wrapper_prefix; ?>
<div id="scrollbox">
<?php print $list_type_prefix; ?>
<?php foreach ($rows as $id => $row): ?>
<li id="media-item-<?php print $row->fid; ?>" class="<?php print $classes_array[$id]; ?>">
<?php print $row->preview; ?>
</li>
<?php endforeach; ?>
<?php print $list_type_suffix; ?>
<div id="status"></div>
</div>
<?php print $wrapper_suffix; ?>
......@@ -4,8 +4,12 @@ package = Media
core = 7.x
dependencies[] = file_entity
dependencies[] = image
dependencies[] = views
files[] = includes/MediaReadOnlyStreamWrapper.inc
files[] = tests/media.test
files[] = tests/media.types.test
files[] = tests/media.entity.test
files[] = includes/media_views_plugin_display_media_browser.inc
files[] = includes/media_views_plugin_style_media_browser.inc
testing_api = 2.x
......@@ -1053,6 +1053,47 @@ function media_form_file_entity_admin_files_alter(&$form, $form_state) {
function media_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'media') . '/includes',
'path' => drupal_get_path('module', 'media'),
);
}
/**
* Implementation of hook_views_default_views().
*/
function media_views_default_views() {
return media_load_all_exports('media', 'views', 'view.inc', 'view');
}
/**
* Fetches an array of exportables from files.
*
* @param $module
* The module invoking this request. (Can be called by other modules.)
* @param $directory
* The subdirectory in the custom module.
* @param $extension
* The file extension.
* @param $name
* The name of the variable found in each file. Defaults to the same as
* $extension.
*
* @return
* Array of $name objects.
*/
function media_load_all_exports($module, $directory, $extension, $name = NULL) {
if (!$name) {
$name = $extension;
}
$return = array();
// Find all the files in the directory with the correct extension.
$files = file_scan_directory(drupal_get_path('module', $module) . "/$directory", "/.$extension/");
foreach ($files as $path => $file) {
require $path;
if (isset($$name)) {
$return[$$name->name] = $$name;
}
}
return $return;
}
<?php
/**
* Implements hook_views_plugins().
*/
function media_views_plugins() {
$plugins = array();
// 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',
'base' => array('file_managed'),
'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',
'base' => array('file_managed'),
'uses row plugin' => FALSE,
'uses row class' => FALSE,
'uses options' => FALSE,
'uses fields' => FALSE,
'type' => 'normal',
'help topic' => 'style-media-browser',
);
return $plugins;
}
/**
* @file
* Provide Views data and handlers for media.module
*/
/**
* Implements hook_field_views_data().
*/
function media_field_views_data($field) {
$data = field_views_field_default_views_data($field);
foreach ($data as $table_name => $table_data) {
// Add the relationship only on the fid field.
$data[$table_name][$field['field_name'] . '_fid']['relationship'] = array(
'handler' => 'views_handler_relationship',
'base' => 'file_managed',
'base field' => 'fid',
'field' => $field['field_name'] . '_fid',
'label' => t('file from !field_name', array('!field_name' => $field['field_name'])),
);
}
return $data;
}
/**
* Display the view as a media browser.
*/
function template_preprocess_media_views_view_media_browser(&$vars) {
// Load file objects for each View result.
$fids = array();
foreach ($vars['rows'] as $index => $row) {
$fids[$index] = $row->fid;
}
$files = file_load_multiple($fids);
// Render the preview for each file.
foreach ($vars['rows'] as $index => $row) {
$file = $files[$row->fid];
$preview = media_get_thumbnail_preview($file);
$preview = drupal_render($preview);
$vars['rows'][$index]->preview = $preview;
$vars['rows'][$index]->file = $file;
// @todo Remove this hacky way of 'linking' each item to select it in the browser.
$vars['rows'][$index]->preview = l($preview, 'media/browser', array('html' => TRUE, 'query' => array('render' => 'media-popup', 'fid' => $row->fid)));
}
// Add classes and wrappers from the style plugin.
$handler = $vars['view']->style_plugin;
$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);
$vars['wrapper_prefix'] = '<div id="container">';
$vars['wrapper_suffix'] = '</div>';
$vars['list_type_prefix'] = '<' . $handler->options['type'] . ' id="media-browser-library-list">';
$vars['list_type_suffix'] = '</' . $handler->options['type'] . '>';
if ($vars['wrapper_class']) {
$vars['wrapper_prefix'] = '<div id="container" class="' . $vars['wrapper_class'] . '">';
}
if ($vars['class']) {
$vars['list_type_prefix'] = '<' . $handler->options['type'] . ' id="media-browser-library-list" class="' . $vars['class'] . '">';
}
// Run the prepared theming variables through a standard Views preprocess
// function.
template_preprocess_views_view_unformatted($vars);
// Add media browser javascript and CSS.
drupal_add_js(drupal_get_path('module', 'media') . '/js/plugins/media.library.js');
drupal_add_css(drupal_get_path('module', 'media') . '/js/plugins/media.library.css');
}
<?php
$view = new view;
$view->name = 'media_default';
$view->description = 'Default view for the media browser library';
$view->tag = 'default, media';
$view->base_table = 'file_managed';
$view->human_name = 'Media browser';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['use_ajax'] = TRUE;
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['query']['options']['query_comment'] = FALSE;
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['exposed_form']['options']['autosubmit'] = 1;
$handler->display->display_options['exposed_form']['options']['autosubmit_hide'] = 1;
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '10';
$handler->display->display_options['pager']['options']['offset'] = '0';
$handler->display->display_options['pager']['options']['id'] = '0';
$handler->display->display_options['pager']['options']['expose']['items_per_page_options_all'] = 0;
$handler->display->display_options['style_plugin'] = 'media_browser';
/* No results behavior: Global: Text area */
$handler->display->display_options['empty']['area']['id'] = 'area';
$handler->display->display_options['empty']['area']['table'] = 'views';
$handler->display->display_options['empty']['area']['field'] = 'area';
$handler->display->display_options['empty']['area']['empty'] = FALSE;
$handler->display->display_options['empty']['area']['content'] = 'No files available.';
$handler->display->display_options['empty']['area']['format'] = 'filtered_html';
$handler->display->display_options['empty']['area']['tokenize'] = 0;
/* Field: File: Name */
$handler->display->display_options['fields']['filename']['id'] = 'filename';
$handler->display->display_options['fields']['filename']['table'] = 'file_managed';
$handler->display->display_options['fields']['filename']['field'] = 'filename';
$handler->display->display_options['fields']['filename']['label'] = '';
$handler->display->display_options['fields']['filename']['alter']['alter_text'] = 0;
$handler->display->display_options['fields']['filename']['alter']['make_link'] = 0;
$handler->display->display_options['fields']['filename']['alter']['absolute'] = 0;
$handler->display->display_options['fields']['filename']['alter']['word_boundary'] = 0;
$handler->display->display_options['fields']['filename']['alter']['ellipsis'] = 0;
$handler->display->display_options['fields']['filename']['alter']['strip_tags'] = 0;
$handler->display->display_options['fields']['filename']['alter']['trim'] = 0;
$handler->display->display_options['fields']['filename']['alter']['html'] = 0;
$handler->display->display_options['fields']['filename']['hide_empty'] = 0;
$handler->display->display_options['fields']['filename']['empty_zero'] = 0;
$handler->display->display_options['fields']['filename']['link_to_file'] = 1;
/* Filter criterion: File: Name */
$handler->display->display_options['filters']['filename']['id'] = 'filename';
$handler->display->display_options['filters']['filename']['table'] = 'file_managed';
$handler->display->display_options['filters']['filename']['field'] = 'filename';
$handler->display->display_options['filters']['filename']['operator'] = 'contains';
$handler->display->display_options['filters']['filename']['exposed'] = TRUE;
$handler->display->display_options['filters']['filename']['expose']['operator_id'] = 'filename_op';
$handler->display->display_options['filters']['filename']['expose']['label'] = 'File name';
$handler->display->display_options['filters']['filename']['expose']['operator'] = 'filename_op';
$handler->display->display_options['filters']['filename']['expose']['identifier'] = 'filename';
$handler->display->display_options['filters']['filename']['expose']['required'] = 0;
$handler->display->display_options['filters']['filename']['expose']['multiple'] = FALSE;
/* Filter criterion: File: Type */
$handler->display->display_options['filters']['type']['id'] = 'type';
$handler->display->display_options['filters']['type']['table'] = 'file_managed';
$handler->display->display_options['filters']['type']['field'] = 'type';
$handler->display->display_options['filters']['type']['exposed'] = TRUE;
$handler->display->display_options['filters']['type']['expose']['operator_id'] = 'type_op';
$handler->display->display_options['filters']['type']['expose']['label'] = 'Type';
$handler->display->display_options['filters']['type']['expose']['operator'] = 'type_op';
$handler->display->display_options['filters']['type']['expose']['identifier'] = 'type';
$handler->display->display_options['filters']['type']['expose']['reduce'] = 0;
/* Display: Media browser */
$handler = $view->new_display('media_browser', 'Media browser', 'media_browser_1');
$handler->display->display_options['defaults']['title'] = FALSE;
$handler->display->display_options['title'] = 'View Library';
$translatables['media_default'] = array(
t('Master'),
t('more'),
t('Apply'),
t('Reset'),
t('Sort by'),
t('Asc'),
t('Desc'),
t('Items per page'),
t('- All -'),
t('Offset'),
t('No files available.'),
t('File name'),
t('Type'),
t('Media browser'),
t('View Library'),
);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment