Skip to content

Instantly share code, notes, and snippets.

@davereid
Created November 1, 2011 19:07
Show Gist options
  • Save davereid/1331561 to your computer and use it in GitHub Desktop.
Save davereid/1331561 to your computer and use it in GitHub Desktop.
Temporary media views integration for 1.x
name = Media browser views
description = Provides views integration for the media browser.
package = Media
core = 7.x
dependencies[] = media
dependencies[] = views
files[] = views_handler_field_file_link_media_browser.inc
<?php
/**
* @file
* Provides Views integration for Media browsers.
*
* @todos:
* http://drupal.org/node/962110 - Display field for media file entities
* http://drupal.org/node/1192326 - Views integration for file entity type
* http://drupal.org/node/1139514 - Overhaulf media browser code
* Ensure this works with multiple select.
*/
/**
* Implements hook_view_api().
*/
function media_views_views_api() {
return array(
'api' => 3,
);
}
/**
* Implements hook_views_data().
*/
function media_views_views_data() {
$data['file_managed']['media_browser_link'] = array(
'field' => array(
'title' => t('Media browser link'),
'help' => t('Provide a simple link to select the file in the media browser.'),
'handler' => 'views_handler_field_file_link_media_browser',
),
);
return $data;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function media_views_form_media_admin_config_browser_alter(&$form, &$form_state) {
// Build the list of file views.
$options = array();
foreach (views_get_all_views() as $view) {
if ($view->base_table != 'file_managed') {
continue;
}
foreach ($view->display as $display) {
$view_name = $view->get_human_name();
$options[$view->name . ':' . $display->id] = $view_name . ': ' . _media_views_get_view_display_title($view, $display->id);
}
}
$form['media_admin_views_enabled'] = array(
'#title' => t('Enable views in browser'),
'#type' => 'checkboxes',
'#multiple' => TRUE,
'#options' => $options,
'#default_value' => variable_get('media_admin_views_enabled', array()),
'#description' => t('A view must have a base table of <em>file_managed</em> in order to be used as a media browser.'),
'#access' => !empty($options),
);
}
/**
* Implements hook_media_browser_plugin_info().
*/
function media_views_media_browser_plugin_info() {
$plugins = array();
$views = array_filter(variable_get('media_admin_views_enabled', array()));
if (!empty($views)) {
// Get all of the views that are type file
foreach ($views as $view_name) {
list($view_name, $display) = explode(':', $view_name . ':default');
if ($view = views_get_view($view_name)) {
$plugins[$view_name . ':' . $display] = array(
'#title' => $view->get_human_name() . ': ' . _media_views_get_view_display_title($view, $display),
'#weight' => 0,
);
}
}
}
return $plugins;
}
/**
* Implements hook_media_browser_plugin_view().
*/
function media_views_media_browser_plugin_view($plugin_name, $params) {
$path = drupal_get_path('module', 'media');
module_load_include('inc', 'media', 'includes/media.admin');
module_load_include('inc', 'media', 'includes/media.pages');
$types = isset($params['types']) ? $params['types'] : array();
// The multiselect parameter is a string. So we check to see if it is set and
// adjust the local variable accordingly.
$multiselect = FALSE;
if (isset($params['multiselect']) && $params['multiselect'] != 'false') {
$multiselect = TRUE;
}
//$redirect = array('media/browser', array('query' => array('render' => 'media-popup')));
// Get the enabled media browser views.
if ($views = variable_get('media_admin_views_enabled', array())) {
// Was this view requested?
if (in_array($plugin_name, $views)) {
list($view_name, $display_id) = explode(':', $plugin_name . ':default');
if (($view = views_get_view($view_name)) && $view->access($display_id)) {
$build = array(
// Output the view and add a an empty form-actions div at the bottom
// for Media to attach its fake cancel button to.
'#markup' => $view->preview($display_id) . '<div class="form-actions form-wrapper"></div>',
'#title' => check_plain($view->get_title()),
);
return $build;
}
}
}
return array();
}
function _media_views_get_file_views() {
$options = array();
foreach (views_get_all_views() as $view) {
if ($view->base_table != 'file_managed') {
continue;
}
foreach ($view->display as $display) {
$options[$view->name . ':' . $display->id] = $view->get_human_name() . ': ' . _media_views_get_view_display_title($view, $display->id);
}
}
return $options;
}
function _media_views_get_view_display_title($view, $display) {
$title = $view->display[$display]->display_title;
if (!empty($view->display[$display]->display_options['title'])) {
$title = $view->display[$display]->display_options['title'];
}
return $title;
}
<?php
/**
* @file
* Provides default views for media
*/
/**
* Implements hook_views_default_views().
*/
function media_views_views_default_views() {
$view = new view;
$view->name = 'media_browser_files';
$view->description = 'files';
$view->tag = '';
$view->base_table = 'file_managed';
$view->human_name = 'Library files';
$view->core = 7;
$view->api_version = '3.0-alpha1';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
/* Display: Defaults */
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->display->display_options['use_ajax'] = TRUE;
$handler->display->display_options['access']['type'] = 'none';
$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['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'] = 'table';
$handler->display->display_options['style_options']['columns'] = array(
'fid' => 'fid',
'filename' => 'filename',
'filesize' => 'filesize',
);
$handler->display->display_options['style_options']['default'] = 'filename';
$handler->display->display_options['style_options']['info'] = array(
'fid' => array(
'sortable' => 0,
'default_sort_order' => 'asc',
'align' => '',
'separator' => '',
),
'filename' => array(
'sortable' => 1,
'default_sort_order' => 'asc',
'align' => '',
'separator' => '',
),
'filesize' => array(
'sortable' => 1,
'default_sort_order' => 'asc',
'align' => '',
'separator' => '',
),
);
$handler->display->display_options['style_options']['override'] = 1;
$handler->display->display_options['style_options']['sticky'] = 0;
$handler->display->display_options['style_options']['empty_table'] = 0;
/* Field: File: File ID */
$handler->display->display_options['fields']['fid']['id'] = 'fid';
$handler->display->display_options['fields']['fid']['table'] = 'file_managed';
$handler->display->display_options['fields']['fid']['field'] = 'fid';
$handler->display->display_options['fields']['fid']['exclude'] = TRUE;
$handler->display->display_options['fields']['fid']['alter']['alter_text'] = 0;
$handler->display->display_options['fields']['fid']['alter']['make_link'] = 0;
$handler->display->display_options['fields']['fid']['alter']['absolute'] = 0;
$handler->display->display_options['fields']['fid']['alter']['external'] = 0;
$handler->display->display_options['fields']['fid']['alter']['replace_spaces'] = 0;
$handler->display->display_options['fields']['fid']['alter']['trim_whitespace'] = 0;
$handler->display->display_options['fields']['fid']['alter']['nl2br'] = 0;
$handler->display->display_options['fields']['fid']['alter']['word_boundary'] = 1;
$handler->display->display_options['fields']['fid']['alter']['ellipsis'] = 1;
$handler->display->display_options['fields']['fid']['alter']['strip_tags'] = 0;
$handler->display->display_options['fields']['fid']['alter']['trim'] = 0;
$handler->display->display_options['fields']['fid']['alter']['html'] = 0;
$handler->display->display_options['fields']['fid']['element_label_colon'] = 1;
$handler->display->display_options['fields']['fid']['element_default_classes'] = 1;
$handler->display->display_options['fields']['fid']['hide_empty'] = 0;
$handler->display->display_options['fields']['fid']['empty_zero'] = 0;
$handler->display->display_options['fields']['fid']['hide_alter_empty'] = 0;
$handler->display->display_options['fields']['fid']['link_to_file'] = 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']['alter']['alter_text'] = 0;
$handler->display->display_options['fields']['filename']['alter']['make_link'] = 1;
$handler->display->display_options['fields']['filename']['alter']['path'] = 'media/browser?render=media-popup&fid=[fid]';
$handler->display->display_options['fields']['filename']['alter']['absolute'] = 0;
$handler->display->display_options['fields']['filename']['alter']['external'] = 0;
$handler->display->display_options['fields']['filename']['alter']['replace_spaces'] = 0;
$handler->display->display_options['fields']['filename']['alter']['trim_whitespace'] = 0;
$handler->display->display_options['fields']['filename']['alter']['nl2br'] = 0;
$handler->display->display_options['fields']['filename']['alter']['word_boundary'] = 1;
$handler->display->display_options['fields']['filename']['alter']['ellipsis'] = 1;
$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']['element_label_colon'] = 1;
$handler->display->display_options['fields']['filename']['element_default_classes'] = 1;
$handler->display->display_options['fields']['filename']['hide_empty'] = 0;
$handler->display->display_options['fields']['filename']['empty_zero'] = 0;
$handler->display->display_options['fields']['filename']['hide_alter_empty'] = 0;
$handler->display->display_options['fields']['filename']['link_to_file'] = 0;
/* Field: File: Size */
$handler->display->display_options['fields']['filesize']['id'] = 'filesize';
$handler->display->display_options['fields']['filesize']['table'] = 'file_managed';
$handler->display->display_options['fields']['filesize']['field'] = 'filesize';
$handler->display->display_options['fields']['filesize']['alter']['alter_text'] = 0;
$handler->display->display_options['fields']['filesize']['alter']['make_link'] = 0;
$handler->display->display_options['fields']['filesize']['alter']['absolute'] = 0;
$handler->display->display_options['fields']['filesize']['alter']['word_boundary'] = 1;
$handler->display->display_options['fields']['filesize']['alter']['ellipsis'] = 1;
$handler->display->display_options['fields']['filesize']['alter']['strip_tags'] = 0;
$handler->display->display_options['fields']['filesize']['alter']['trim'] = 0;
$handler->display->display_options['fields']['filesize']['alter']['html'] = 0;
$handler->display->display_options['fields']['filesize']['element_label_colon'] = 1;
$handler->display->display_options['fields']['filesize']['element_default_classes'] = 1;
$handler->display->display_options['fields']['filesize']['hide_empty'] = 0;
$handler->display->display_options['fields']['filesize']['empty_zero'] = 0;
/* 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']['use_operator'] = TRUE;
$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']['multiple'] = FALSE;
/* Filter criterion: File: Size */
/*$handler->display->display_options['filters']['filesize']['id'] = 'filesize';
$handler->display->display_options['filters']['filesize']['table'] = 'file_managed';
$handler->display->display_options['filters']['filesize']['field'] = 'filesize';
$handler->display->display_options['filters']['filesize']['operator'] = '>=';
$handler->display->display_options['filters']['filesize']['value']['value'] = '0';
$handler->display->display_options['filters']['filesize']['exposed'] = TRUE;
$handler->display->display_options['filters']['filesize']['expose']['operator_id'] = 'filesize_op';
$handler->display->display_options['filters']['filesize']['expose']['label'] = 'File: Size';
$handler->display->display_options['filters']['filesize']['expose']['use_operator'] = TRUE;
$handler->display->display_options['filters']['filesize']['expose']['operator'] = 'filesize_op';
$handler->display->display_options['filters']['filesize']['expose']['identifier'] = 'filesize';
$handler->display->display_options['filters']['filesize']['expose']['multiple'] = FALSE;*/
/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page_1');
$handler->display->display_options['path'] = 'media/browser/files';
$views[$view->name] = $view;
return $views;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment