Commit 36659576 authored by Dave Reid's avatar Dave Reid

Issue #1400200: Added a proper interface and abstract class for Media browser...

Issue #1400200: Added a proper interface and abstract class for Media browser plugins. Improved documentation.
parent 0ad4d7ae
<?php
class MediaBrowserLibrary extends MediaBrowserPlugin {
/**
* @file
* Definition of MediaBrowserLibrary.
*/
function view() {
/**
* Media browser plugin for displaying the media browser library.
*
* @deprecated
*/
class MediaBrowserLibrary extends MediaBrowserPlugin {
/**
* Implements MediaBrowserPlugin::view().
*/
public function view() {
$path = drupal_get_path('module', 'media');
$build['#attached']['js'][] = $path . '/js/plugins/media.library.js';
//@todo: should move this.
$build['#attached']['css'][] = $path . '/js/plugins/media.library.css';
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>',
);
}
$build['#settings']['viewMode'] = 'thumbnails';
$build['#settings']['getMediaUrl'] = url('media/browser/list');
$build['#settings'] += $this->params;
$build['#markup'] = '<div id="container"><div id="scrollbox"><ul id="media-browser-library-list" class="media-list-thumbnails"></ul><div id="status"></div></div></div>';
return $build;
}
}
<?php
/**
* @file
* Definition of MediaBrowserPlugin.
*/
/**
* Defines a Media browser plugin base class.
*
* MediaBrowserPlugin implementations need to implement at least the
* view() method.
*/
abstract class MediaBrowserPlugin implements MediaBrowserPluginInterface {
/**
* The plugin metadata array from hook_media_browser_plugin_info().
*
* @var array
*/
protected $info;
/**
* The parameters for the current media browser from media_get_browser_params().
*
* @var array
*/
protected $params;
/**
* Implements MediaBrowserPluginInterface::__construct().
*/
public function __construct($info, $params) {
$this->info = $info;
$this->params = $params;
}
/**
* Provide a render array to display the plugin in a media browser.
*
* This render array will be a jQuery tab in the media browser.
*
* Some elements are special:
* - #settings: Drupal.settings.media.browser.$key (where key is the array key).
* - #callback: If provided, will make the tab an "ajax" tab.
* - #title: If provided, will be used as the tab's title. Otherwise the
* 'title' value from the plugin's hook_media_browser_plugin_info() will
* be used.
* - #weight: If provided, will be used to order the tabs between each other.
* A lower weight will be displayed first while a higher weight will be
* displayed later. If not provided, and there is a 'weight' value in the
* plugin's hook_media_browser_plugin_info() then it will be used,
* otherwise a default of 0 will be used.
* - form: If the plugin is to display a native Drupal form, then the output
* of drupal_get_form should be returned into the 'form' render key.
*
* Example:
* @code
* return array(
* '#attached' => array(
* 'js' => array(
* drupal_get_path('module', 'media') . '/js/plugins/media.library.js',
* ),
* ),
* '#settings' => array(
* 'viewMode' => 'thumbnails',
* 'getMediaUrl' => url('media/browser/list'),
* ),
* '#markup' => '<div>Library goes here</div>',
* );
* @endcode
*
* @return
* Renderable array.
*/
abstract public function view();
}
<?php
/**
* @file
* Definition of MediaBrowserPluginInterface.
*/
/**
* Defines a Media browser plugin.
*
* Extends the MediaBrowserPluginInterface with methods expected by all
* Media browser classes.
*/
interface MediaBrowserPluginInterface {
/**
* Set up the plugin class.
*
* @param $info
* An array of plugin info from hook_media_browser_plugin_info()
* implementations.
* @param $params
* An array of parameters which came in is $_GET['params']. The expected
* parameters are still being defined.
* - 'types': array of media types to support
* - 'multiselect': boolean; TRUE enables multiselect
*/
public function __construct($info, $params);
}
<?php
class MediaBrowserUpload extends MediaBrowserPlugin {
function view() {
$path = drupal_get_path('module', 'media');
/**
* @file
* Definition of MediaBrowserUpload.
*/
module_load_include('inc', 'media', 'includes/media.admin');
/**
* Media browser plugin for showing the upload form.
*
* @deprecated
*/
class MediaBrowserUpload extends MediaBrowserPlugin {
/**
* Implements MediaBrowserPlugin::view().
*/
public function view() {
module_load_include('inc', 'media', 'includes/media.pages');
$attached = array();
$build = 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');
$build['form'] = drupal_get_form('media_add_upload_multiple', $this->params);
$build['#attached']['js'][] = drupal_get_path('module', 'media') . '/js/plugins/media.upload_multiple.js';
}
else {
$upload_form_id = 'media_add_upload';
$build['form'] = drupal_get_form('media_add_upload', $this->params);
}
$upload_form = drupal_get_form($upload_form_id, $this->params);
return array(
'form' => $upload_form,
'#attached' => $attached,
);
return $build;
}
}
<?php
class MediaBrowserView extends MediaBrowserPlugin {
/**
* @file
* Definition of MediaBrowserView.
*/
/**
* Media browser plugin for displaying a specific view and display.
*/
class MediaBrowserView extends MediaBrowserPlugin {
/**
* Implements MediaBrowserPlugin::view().
*/
function view() {
$view = views_get_view($this->info['view_name']);
$display_id = $this->info['view_display_id'];
// Check if the user has access to see this view.
if (empty($view) || !$view->access($display_id)) {
return array();
return;
}
return array(
'#markup' => $view->preview($display_id),
);
$build['#markup'] = $view->preview($display_id);
return $build;
}
}
......@@ -80,10 +80,17 @@ function media_browser($selected = NULL) {
}
if ($access) {
$plugin_obj = new $plugin['class']($plugin, $params);
$plugin_output[$key] = $plugin_obj->view() + array(
'#title' => $plugin['title'],
'#weight' => isset($plugin['weight']) ? $plugin['weight'] : 0,
);
$plugin_output[$key] = $plugin_obj->view();
if (!empty($plugin_output[$key]) && is_array($plugin_output[$key])) {
$plugin_output[$key] += array(
'#title' => $plugin['title'],
'#weight' => isset($plugin['weight']) ? $plugin['weight'] : 0,
);
}
else {
unset($plugin_output[$key]);
continue;
}
}
// We need to get a submit and cancel button on each tab. If the plugin
......@@ -409,67 +416,3 @@ function media_browser_build_media_item($file) {
$file->preview = drupal_render($preview);
$file->url = file_create_url($file->uri);
}
/**
* Base class for media browser plugins.
*
* This is basically just API documentation.
*/
class MediaBrowserPlugin {
// Plugin info array.
var $info;
// Browser instance parameters.
var $params;
/**
* Set up the plugin class.
*
* @param $info
* An array of plugin info from hook_media_browser_plugin_info()
* implementations.
* @param $params
* An array of parameters which came in is $_GET['params']. The expected
* parameters are still being defined.
* - types: array of media types to support
* - multiselect: boolean; TRUE enables multiselect
*/
function __construct($info, $params) {
$this->info = $info;
$this->params = $params;
}
/**
* Provide a render array to display the plugin in a media browser.
*
* This render array will be a jQuery tab in the media browser.
*
* Some elements are special:
* - #settings: Drupal.settings.media.browser.$key (where key is the array key).
* - #callback: If provided, will make the tab an "ajax" tab.
*
* Example:
* @code
* return array(
* '#attached' => array(
* 'js' => array(
* drupal_get_path('module', 'media') . '/js/plugins/media.library.js',
* ),
* ),
* '#settings' => array(
* 'viewMode' => 'thumbnails',
* 'getMediaUrl' => url('media/browser/list'),
* ),
* '#markup' => '<div>Library goes here</div>',
* );
* @endcode
*
* @return
* Renderable array.
*/
function view() {
return array('#markup' => '');
}
}
......@@ -9,7 +9,8 @@ dependencies[] = views
files[] = includes/MediaReadOnlyStreamWrapper.inc
files[] = includes/media.browser.inc
files[] = includes/MediaBrowserPluginInterface.inc
files[] = includes/MediaBrowserPlugin.inc
files[] = includes/MediaBrowserLibrary.inc
files[] = includes/MediaBrowserUpload.inc
files[] = includes/MediaBrowserView.inc
......
......@@ -2,20 +2,18 @@
/**
* @file
* Media browser internet plugin.
* Definition of MediaBrowserInternet.
*/
/**
* Media browser plugin for Media Internet 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' => $from_web_form,
'#attached' => array(
//'js' => array($path . '/js/plugins/media.fromurl.js'),
),
);
/**
* Implements MediaBrowserPlugin::view().
*/
public function view() {
$build['form'] = drupal_get_form('media_internet_add', $this->params['types'], $this->params['multiselect']);
return $build;
}
}
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