Commit 54822b19 authored by Dave Reid's avatar Dave Reid

Issue #1289872 by effulgentsia, becw | JacobSingh: Use ctools plugins to...

Issue #1289872 by effulgentsia, becw | JacobSingh: Use ctools plugins to provide add-ons to the media browser.
parent 65ba3c69
<?php
class MediaBrowserLibrary extends MediaBrowserPlugin {
function view() {
$path = drupal_get_path('module', 'media');
return array(
'#attached' => array(
'js' => array(
$path . '/js/plugins/media.library.js',
),
'css' => array(
//@todo: should move this.
$path . '/js/plugins/media.library.css',
),
),
'#settings' => array(
'viewMode' => 'thumbnails',
'getMediaUrl' => url('media/browser/list'),
) + $this->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>',
);
}
}
<?php
class MediaBrowserUpload extends MediaBrowserPlugin {
function view() {
$path = drupal_get_path('module', 'media');
module_load_include('inc', 'media', 'includes/media.admin');
module_load_include('inc', 'media', 'includes/media.pages');
$attached = array();
if ($this->params['multiselect'] && module_exists('plupload')) {
$upload_form_id = 'media_add_upload_multiple';
$attached['js'] = array($path . '/js/plugins/media.upload_multiple.js');
}
else {
$upload_form_id = 'media_add_upload';
}
$upload_form = drupal_get_form($upload_form_id, $this->params);
return array(
'form' => array($upload_form),
'#attached' => $attached,
);
}
}
<?php
class MediaBrowserView extends MediaBrowserPlugin {
function view() {
$view = views_get_view($this->info['view_name']);
return array(
'#markup' => $view->preview($this->info['view_display_id']) . '<div class="form-actions form-wrapper"></div>',
);
}
}
This diff is collapsed.
......@@ -122,9 +122,8 @@ function media_field_widget_settings_form($field, $instance) {
'#weight' => 2,
);
module_load_include('inc', 'media', 'includes/media.browser');
$plugins = module_invoke_all('media_browser_plugin_info');
drupal_alter('media_browser_plugins', $plugins);
ctools_include('plugins');
$plugins = ctools_get_plugins('media', 'browser');
$form['browser_plugins'] = array(
'#type' => 'checkboxes',
'#title' => t('Enabled browser plugins'),
......@@ -133,7 +132,7 @@ function media_field_widget_settings_form($field, $instance) {
'#description' => t('If no plugins are selected, they will all be available.'),
);
foreach ($plugins as $key => $plugin) {
$form['browser_plugins']['#options'][$key] = !empty($plugin['#title']) ? $plugin['#title'] : $key;
$form['browser_plugins']['#options'][$key] = !empty($plugin['title']) ? $plugin['title'] : $key;
}
return $form;
......
......@@ -2,72 +2,34 @@
/**
* @file
* Hook provided by the media module.
* Hooks provided by the Media module.
*/
/**
* Return an array of plugins for the media browser.
*
* Implementors are expected to return a renderable element.
*
* Each element will be a jQuery tab on the media browser.
*
* Some elements are special:
* - #title: The title that goes on the tab
* - #settings: Drupal.settings.media.browser.$key (where key is the array key).
* - #callback: If provided, will make the tab an "ajax" tab.
*
* Example:
* $plugins['library'] = array(
* '#title' => t('Library'),
* '#attached' => array(
* 'js' => array(
* $path . '/js/plugins/media.library.js',
* ),
* ),
* '#settings' => array(
* 'viewMode' => 'thumbnails',
* 'getMediaUrl' => url('media/browser/list'),
* ),
* '#markup' => '<div> Library goes here</div>',
* );
*
* @param $plugin_name
* The name of the plugin to view
*
* @param $params
* An array of parameters which came in is $_GET['params'].
* The expected parameters is still being defined.
* - types: Array of media types to support
* - multiselect: Boolean enabling or disabling multiselect
*
* @return
* Renderable array.
*/
function hook_media_browser_plugin_view($plugin_name, $params) {
}
/**
* Returns a list of plugins for the media browser.
*
* Plugins are defined in a multi-dimensional associative
* array format with the following keys:
*
* - #weight (optional): Weight of the plugin in relation to other plugins
* when being displayed, e.g. tabs in the browser.
*
* @example
* <code>
* array(
* 'unique_plugin_name' => array(
* '#weight' => 42,
* ),
* );
* </code>
* Media provides a CTools plugin API; this is one of those hooks. It should
* return a nested array of plugin information, keyed by plugin name. Each
* plugin info array may have the following keys:
* - title (required): A name for the tab in the media browser.
* - handler (required): The class name of the handler. This class must
* implement a view() method, and may (should) extend the
* @link MediaBrowserPlugin MediaBrowserPlugin @endlink class.
* - weight (optional): Integer to determine the tab order. Defaults to 0.
* - access callback (optional): A callback for user access checks.
* - access arguments (optional): An array of arguments for the user access
* check.
* Additional custom keys may be provided for use by the handler.
*/
function hook_media_browser_plugin_info() {
$plugins['media_upload'] = array(
'title' => t('Upload'),
'handler' => 'MediaBrowserUpload',
'weight' => -10,
'access callback' => 'user_access',
'access arguments' => array('create files'),
);
return $plugins;
}
/**
......
......@@ -2,14 +2,23 @@ name = Media
description = Provides the core Media API
package = Media
core = 7.x
dependencies[] = file_entity
dependencies[] = image
dependencies[] = views
files[] = includes/MediaReadOnlyStreamWrapper.inc
files[] = includes/media.browser.inc
files[] = includes/MediaBrowserLibrary.inc
files[] = includes/MediaBrowserUpload.inc
files[] = includes/MediaBrowserView.inc
files[] = includes/media_views_plugin_display_media_browser.inc
files[] = includes/media_views_plugin_style_media_browser.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
......@@ -973,19 +973,41 @@ function media_flush_caches() {
db_delete('media_filter_usage')->condition('timestamp', REQUEST_TIME - 86400 * 120, '<')->execute();
}
/**
* Implements hook_ctools_plugin_type().
*/
function media_ctools_plugin_type() {
return array(
'browser' => array(
'version' => 1,
// Cache plugin information?
'cache' => TRUE,
// Which plugin info array keys contain classes?
'classes' => array('handler'),
// CTools will call hook_mymodule_myplugintype() to discover plugins.
'use hooks' => TRUE,
'hook' => 'media_browser_plugin_info',
),
);
}
/**
* Implements hook_ctools_plugin_api().
*
* Lets CTools know which plugin APIs are implemented by Media module.
*/
function media_ctools_plugin_api($owner, $api) {
static $api_versions = array(
'file_entity' => array(
'file_default_displays' => 1,
),
);
if (isset($api_versions[$owner][$api])) {
return array('version' => $api_versions[$owner][$api]);
function media_ctools_plugin_api($module, $api) {
if ($module == 'file_entity' && $api == 'file_default_displays') {
return array(
'version' => 1,
);
}
if ($module == 'media' && $api == 'browser') {
return array(
'version' => 1,
'path' => drupal_get_path('module', 'media') . '/includes',
);
}
}
......
<?php
/**
* Implements hook_ctools_plugin_api().
*/
function media_internet_ctools_plugin_api($module, $api) {
if ($module == 'media' && $api == 'browser') {
return array(
'version' => 1,
);
}
}
/**
* Implements hook_media_browser_plugin_info().
*/
function media_internet_media_browser_plugin_info() {
$plugins = array();
/*
@todo: Coming soon.
'search' => array(
),
*/
if (user_access('administer media') || user_access('add media from remote sources')) {
$plugins['media_internet'] = array(
'#title' => t('Web'),
'#weight' => -10,
$plugins['media_internet'] = array(
'title' => t('Web'),
'handler' => 'MediaBrowserInternet',
'access callback' => 'media_internet_access',
);
return $plugins;
}
/**
* Access callback for the media_internet media browser plugin.
*/
function media_internet_access() {
return user_access('administer media') || user_access('add media from remote sources');
}
class MediaBrowserInternet extends MediaBrowserPlugin {
function view() {
// @todo: implement the multiselect argument here.
$from_web_form = drupal_get_form('media_internet_add', $this->params['types'], $this->params['multiselect']);
return array(
'form' => array($from_web_form),
'#attached' => array(
//'js' => array($path . '/js/plugins/media.fromurl.js'),
),
);
}
return $plugins;
}
/**
......@@ -31,33 +58,6 @@ function media_internet_permission() {
);
}
/**
* Implements hook_media_browser_plugin_view().
*/
function media_internet_media_browser_plugin_view($plugin_name, $params) {
$path = drupal_get_path('module', 'media');
$types = isset($params['types']) ? $params['types'] : array();
$multiselect = isset($params['multiselect']) ? $params['multiselect'] : FALSE;
$redirect = array('media/browser', array('query' => array('render' => 'media-popup')));
switch ($plugin_name) {
case 'media_internet':
// @todo: implement the multiselect argument here.
$from_web_form = drupal_get_form('media_internet_add', $types, $multiselect);
return array(
'#title' => t('Web'),
'form' => array($from_web_form),
'#attached' => array(
//'js' => array($path . '/js/plugins/media.fromurl.js'),
),
);
break;
}
return array();
}
/**
* Provides a form for adding media items from 3rd party sources.
*/
......
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