' .t('About') .'';
$output .= '
' .t('The Media module is a File Browser to the Internet, media provides a framework for managing files and multimedia assets, regardless of whether they are hosted on your own site or a 3rd party site. It replaces the Drupal core upload field with a unified User Interface where editors and administrators can upload, manage, and reuse files and multimedia assets. Media module also provides rich integration with WYSIWYG module to let content creators access media assets in rich text editor. For more information check Media Module page',array('@media_faq' => 'http://drupal.org/project/media')) .'
';
$output .= '' .t('Uses') .'
';
$output .= '';
$output .= '- ' .t('Media Repository') .'
';
$output .= '- ' .t('Media module allows you to maintain a media asset repository where in you can add, remove, reuse your media assets. You can add the media file using upload form or from a url and also do bulk operations on the media assets.', array('@mediarepo' => url('admin/content/media'))) .'
';
$output .= '- ' .t('Attaching media assets to content types') .'
';
$output .= '- ' .t('Media assets can be attached to content types as fields. To add a media field to a content type, go to the content type\'s manage fields page, and add a new field of type Multimedia Asset.', array('@content-type' => url('admin/structure/types'))) .'
';
$output .= '- ' .t('Using media assets in WYSIWYG') .'
';
$output .= '- ' .t('Media module provides rich integration with WYSIWYG editors, using Media Browser plugin you can select media asset from library to add to the rich text editor moreover you can add media asset from the media browser itself using either upload method or add from url method. To configure media with WYSIWYG you need two steps of configuration:');
$output .= '
- ' .t('Enable WYSIWYG plugin on your desired WYSIWYG profile. Please note that you will need to have WYSIWYG module enabled.', array('@wysiwyg-profile' => url('admin/config/content/wysiwyg'),'@wysiwyg' => 'http://drupal.org/project/wysiwyg')) .'
';
$output .= '- ' .t('Add Media Filter to the WYSIWYG enabled Input format', array('@input-format' => url('admin/config/content/formats'))) .'
';
return $output;
}
}
/**
* Implement of hook_menu().
*/
function media_menu() {
// For managing different types of media and the fields associated with them.
$items['admin/structure/media'] = array(
'title' => 'Media Types',
'description' => 'Manage files used on your site.',
'page callback' => 'media_admin_type_list',
'access arguments' => array('administer media'),
'file' => 'media.admin.inc',
);
$items['admin/structure/media/manage/%media_type'] = array(
'title' => 'Manage media',
'description' => 'Manage files used on your site.',
'page callback' => 'drupal_get_form',
'page arguments' => array('media_admin_type_manage_form', 4),
'access arguments' => array('administer media'),
'type' => MENU_CALLBACK,
'file' => 'media.admin.inc',
);
$items['admin/structure/media/manage/%media_type/settings'] = array(
'title' => 'Settings',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -1,
);
/**
* Administrative screens for managing media
* @var unknown_type
*/
$items['admin/content/media'] = array(
'title' => 'Media',
'description' => 'Manage files used on your site.',
'page callback' => 'drupal_get_form',
'page arguments' => array('media_admin'),
'access arguments' => array('administer media'),
'type' => MENU_LOCAL_TASK,
'file' => 'media.admin.inc',
);
$items['admin/content/media/import'] = array(
'title' => 'Import media',
'description' => 'Import files into your media library.',
'page callback' => 'drupal_get_form',
'page arguments' => array('media_import'),
'file' => 'media.admin.inc',
'access arguments' => array('administer media'),
'type' => MENU_LOCAL_ACTION,
);
$items['media/browser'] = array(
'title' => 'Media browser',
'description' => 'Media Browser for picking media and uploading new media',
'page callback' => 'media_browser',
'access callback' => 'media_access',
'access arguments' => array('view'),
'type' => MENU_CALLBACK,
'file' => 'media.browser.inc',
'theme callback' => 'media_dialog_get_theme_name',
);
$items['media/browser/testbed'] = array(
'title' => 'Media Browser test',
'description' => 'Make it easier to test media browser',
'page callback' => 'drupal_get_form',
'page arguments' => array('media_browser_testbed'),
'file' => 'media.browser.inc',
'access arguments' => array('administer media'),
'type' => MENU_CALLBACK,
);
/**
* Browser callbacks
* @var unknown_type
*/
$items['media/browser/list'] = array(
'title' => 'Media browser list',
'description' => 'Ajax Callback for getting media',
'page callback' => 'media_browser_list',
'access callback' => 'media_access',
'access arguments' => array('view'),
'type' => MENU_CALLBACK,
'file' => 'media.browser.inc',
);
$items['media/browser/library'] = array(
'title' => 'Media browser library',
'description' => 'Media Browser for picking media and uploading new media',
'page callback' => 'media_browser_library',
'access callback' => 'media_access',
'access arguments' => array('view'),
'type' => MENU_CALLBACK,
'file' => 'media.browser.inc',
);
$items['media/%media/format-form'] = array(
'title' => 'Style selector',
'description' => 'Choose a format for a piece of media',
'page callback' => 'drupal_get_form',
'page arguments' => array('media_format_form', 1),
'access arguments' => array('administer media'),
'weight' => 0,
'type' => MENU_CALLBACK,
'file' => 'media.filter.inc',
'theme callback' => 'media_dialog_get_theme_name',
);
$items['media/%media'] = array(
'page callback' => 'media_file_view',
'page arguments' => array(1),
'access callback' => 'media_access',
'access arguments' => array('view'),
'type' => MENU_CALLBACK,
'file' => 'media.pages.inc',
);
$items['media/%media/view'] = array(
'title' => 'View',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['media/%media/edit'] = array(
'title' => 'Edit',
'page callback' => 'media_page_edit',
'page arguments' => array(1),
'access callback' => 'media_access',
'access arguments' => array('edit'),
'weight' => 0,
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
'file' => 'media.pages.inc',
);
$items['media/add/upload'] = array(
'title' => 'Upload new file',
'page callback' => 'drupal_get_form',
'page arguments' => array('media_add_upload'),
'access arguments' => array('administer media'),
'type' => MENU_LOCAL_ACTION,
'file' => 'media.pages.inc',
'weight' => -1,
);
$items['admin/content/media/add/upload'] = $items['media/add/upload'];
/**
* End-user CRUD screens
* @var unknown_type
*/
$items['media/add/from_url'] = array(
'title' => 'Add media from URL',
'page callback' => 'drupal_get_form',
'page arguments' => array('media_add_from_url'),
'access arguments' => array('administer media'),
'type' => MENU_LOCAL_ACTION,
'file' => 'media.pages.inc',
'weight' => -1,
);
$items['admin/content/media/add/from_url'] = $items['media/add/from_url'];
$items['media/js'] = array(
'page callback' => 'media_preview_ajax',
'access callback' => 'media_access',
'access arguments' => array('view'),
'type' => MENU_CALLBACK,
'file' => 'media.pages.inc',
);
return $items;
}
/**
* Implement hook_permission().
*/
function media_permission() {
return array(
'administer media' => array(
'title' => t('Administer media'),
'description' => t('Add, edit or delete media files and administer settings.'),
),
'view media' => array(
'title' => t('View media'),
'description' => t('View all media files.'), // @TODO better description
),
'edit media' => array(
'title' => t('Edit media'),
'description' => t('Edit all media files.'), // @TODO better description
),
);
}
/**
* Implement hook_theme().
* @TODO: Needs a major cleanup.
*/
function media_theme() {
return array(
// The default media file list form element.
'media_file_list' => array(
'variables' => array('element' => NULL),
),
// A link for a file w/ an icon to the media/$fid page.
'media_link' => array(
'variables' => array('file' => NULL),
'file' => 'media.theme.inc',
),
// A preview of the uploaded file.
'media_thumbnail' => array(
'render element' => 'element',
'file' => 'media.theme.inc',
),
// Administrative thumbnail previews.
'media_admin_thumbnail' => array(
'variables' => array('file' => array(), 'style_name' => 'thumbnail'),
'file' => 'media.theme.inc',
),
// Tabs in the media browser.
'media_browser_tabs' => array(
'file' => 'media.theme.inc',
),
// Dialog page.
'media_dialog_page' => array(
'render element' => 'page',
'template' => 'media-dialog-page',
'file' => 'media.theme.inc',
),
//
'media_element' => array(
'render element' => 'element',
'file' => 'media.theme.inc',
),
);
}
/**
* Implement hook_image_default_styles().
*
*/
function media_image_default_styles() {
$styles = array();
$styles['square_thumbnail'] = array(
'effects' => array(
array(
'name' => 'image_scale_and_crop',
'data' => array('width' => 180, 'height' => 180),
'weight' => 0,
),
)
);
return $styles;
}
/**
* Implement hook_file_insert().
*/
function media_file_insert($file) {
$file->type = media_get_type($file);
drupal_write_record('file_managed', $file, array('fid'));
media_save_attached_file($file);
}
/**
* Implement hook_file_update().
*/
function media_file_update($file) {
$file->type = media_get_type($file);
media_save_attached_file($file);
}
/**
* Implement hook_page_alter().
*
* This is used to use our alternate template when ?render=media-popup is passed
* in the URL.
*/
function media_page_alter(&$page) {
if (isset($_GET['render']) && $_GET['render'] == 'media-popup') {
$page['#theme'] = 'media_dialog_page';
foreach (element_children($page) as $key) {
if ($key != 'content') {
unset($page[$key]);
}
}
}
}
/**
* Implements hook_element_info_alter().
*/
function media_element_info_alter(&$types) {
$types['text_format']['#pre_render'][] = 'media_pre_render_text_format';
}
/**
* Implement hook_media_display_types().
*
* This is used to display media in different ways on the admin section.
* Perhaps should be merged in with the browser display.
*/
function media_media_display_types() {
$path = drupal_get_path('module', 'media');
$display_types = array();
$display_types['list'] = array(
'title' => t('List'),
'description' => t('Display as a list.'),
'icon' => $path . '/images/display-list.png',
'icon_active' => $path . '/images/display-list-active.png',
'callback' => 'media_admin_list',
'file' => drupal_get_path('module', 'media') . '/media.admin.inc',
);
$display_types['thumbnails'] = array(
'title' => t('Thumbnails'),
'description' => t('Display as thumbnails.'),
'icon' => $path . '/images/display-thumbnails.png',
'icon_active' => $path . '/images/display-thumbnails-active.png',
'callback' => 'media_admin_thumbnails',
'file' => drupal_get_path('module', 'media') . '/media.admin.inc',
);
return $display_types;
}
/**
* Implement hook_media_operations().
* @TODO: Shouldn't this be using actions or something?
*/
function media_media_operations() {
$operations = array(
'delete' => array(
'label' => t('Delete'),
'callback' => NULL,
),
);
return $operations;
}
/* ***************************************** */
/* API FUNCTIONS */
/* ***************************************** */
/**
* Loads a Media Asset
*
* @param $fid
* @return StdClass
*/
function media_load($fid) {
if ($files = entity_load('media', array($fid))) {
return array_shift($files);
}
}
/**
* Save a Media Asset
*
* @param $media
* @return StdClass
*/
function media_save($media) {
field_attach_presave('media', $media);
if ($media->fid) {
$op = 'update';
}
else {
$op = 'insert';
}
// Do something here for insert update
if ($op == 'insert') {
// should never happen I think, except perhaps on import.
// @todo: validate bundle, etc
field_attach_insert('media', $media);
}
else {
field_attach_update('media', $media);
}
}
/**
* Access callback for media assets.
*/
function media_access($op) {
return (user_access('administer media') || user_access($op . ' media'));
}
/**
* Get all display types.
*/
function media_display_types() {
$types = &drupal_static(__FUNCTION__);
if (!$types) {
$types = module_invoke_all('media_display_types');
drupal_alter('media_display_types', $types);
}
return $types;
}
/* ***************************************** */
/* Callbacks */
/* ***************************************** */
/**
* Process callback for the media_browser element.
*
* @param $element
* @param $edit
* @param $form_state
* @param $form
* @return array
*/
function media_file_list_element_process($element, $edit, $form_state, $form) {
$element['list'] = array(
'#type' => 'select',
'#options' => $element['#options'],
'#size' => media_variable_get('file_list_size'),
);
return $element;
}
if (!function_exists('file_uri_to_object')) {
// @todo: get this committed http://drupal.org/node/685818
/**
* Returns a file object which can be passed to file_save().
*
* @param $uri
* A string containing the URI, path, or filename.
* @return
* A file object, or FALSE on error.
*/
function file_uri_to_object($uri) {
global $user;
$uri = file_stream_wrapper_uri_normalize($uri);
$wrapper = file_stream_wrapper_get_instance_by_uri($uri);
$file = new StdClass;
$file->uid = $user->uid;
$file->filename = basename($uri);
$file->uri = $uri;
$file->filemime = file_get_mimetype($uri);
$file->filesize = filesize($uri);
$file->timestamp = REQUEST_TIME;
$file->status = FILE_STATUS_PERMANENT;
return $file;
}
}
/**
* Implement hook_library().
*/
function media_library() {
$path = drupal_get_path('module','media');
$common = array(
'website' => 'http://drupal.org/project/media',
'version' => '7.x',
);
/**
* Contains libraries common to other media modules.
*/
$libraries['media_base'] = array(
'title' => 'Media base',
'js' => array(
$path . '/javascript/media.core.js' => array('weight' => JS_LIBRARY - 5),
$path . '/javascript/util/json2.js' => array('weight' => JS_LIBRARY),
$path . '/javascript/util/ba-debug.min.js' => array('weight' => JS_LIBRARY),
),
'css' => array(
$path . '/css/media.css',
),
);
/**
* Includes resources needed to launch the media browser. Should be included
* on pages where the media browser needs to be launched from.
*/
$libraries['media_browser'] = array(
'title' => 'Media Browser popup libraries',
'js' => array(
$path . '/javascript/media.popups.js' => array('weight' => JS_DEFAULT),
),
'dependencies' => array(
array('media', 'media_base'),
array('system', 'ui.resizable'),
array('system', 'ui.draggable'),
array('system', 'ui.dialog'),
),
);
/**
* Resources needed in the media browser itself.
*/
$libraries['media_browser_page'] = array(
'title' => 'Media browser',
'js' => array(
$path . '/javascript/media-browser.js' => array('weight' => JS_DEFAULT),
),
'css' => array(
$path . '/css/media.browser.css' => array('weight' => CSS_DEFAULT),
),
'dependencies' => array(
array('media', 'media_base'),
array('system', 'ui.tabs'),
array('system', 'ui.draggable'),
array('system', 'ui.dialog'),
),
);
foreach ($libraries as &$library) {
$library += $common;
}
return $libraries;
}
/**
* Theme callback used to identify when we are in a popup dialog.
*
* We do this because most times the default theme will look terrible in the
* browser. So this will default to the administration theme, unless set otherwise.
*
* @return string
*/
function media_dialog_get_theme_name() {
// Set to empty to get the default theme.
return media_variable_get('dialog_get_theme_name');
}
/**
* A wrapper around simplexml to retrieve a given XML file.
*
* @param $url
* The URL to the XML to retrieve.
* @param $display_errors
* Optional; if TRUE, then we'll display errors to the end user. They'll be
* logged to the watchdog in any case.
* @param $refresh
* Optional; if TRUE, then we'll force a new load of the XML. Otherwise,
* a cached version will be retrieved if possible.
* @return
* A fully populated object, or FALSE on an error.
*/
function media_retrieve_xml($url, $display_errors = FALSE, $refresh = FALSE) {
module_load_include('inc', 'media', 'media.xml');
return _media_retrieve_xml($url, $display_errors, $refresh);
}
/**
* This will parse a url or embedded code into a unique URI.
*
* The function will call all modules implementing hook_media_parse($url),
* which should return either a string containing a parsed URI or NULL.
*
* @NOTE The implementing modules may throw an error, which will not be caught
* here; it's up to the calling function to catch any thrown errors.
*
* @NOTE In emfield, we originally also accepted an array of regex patterns
* to match against. However, that module used a registration for providers,
* and simply stored the match in the database keyed to the provider object.
* However, other than the stream wrappers, there is currently no formal
* registration for media handling. Additionally, few, if any, stream wrappers
* will choose to store a straight match from the parsed URL directly into
* the URI. Thus, we leave both the matching and the final URI result to the
* implementing module in this implementation.
*
* An alternative might be to do the regex pattern matching here, and pass a
* successful match back to the implementing module. However, that would
* require either an overloaded function or a new hook, which seems like more
* overhead than it's worth at this point.
*
* @TODO Once hook_module_implements_alter() is in core (see the issue at
* http://drupal.org/node/692950) we may want to implement media_media_parse()
* to ensure we were passed a valid URL, rather than an unsupported or
* malformed embed code that wasn't caught earlier. It will needed to be
* weighted so it's called after all other streams have a go, as the fallback,
* and will need to throw an error.
*
* @param string $url
* The original URL or embed code to parse.
* @param optional string $form_field
* The field from FAPI when being validated, suitable for
* form_set_error(). If this is set, then a particular implementation
* may throw an error if it believes the URL to be malformed.
* @return
* The unique URI for the file, based on its stream wrapper, or NULL.
*
* @see media_parse_to_file().
* @see media_add_from_url_validate().
*/
function media_parse_to_uri($url) {
// Trim any whitespace before parsing.
$url = trim($url);
foreach (module_implements('media_parse') as $module) {
$success = module_invoke($module, 'media_parse', $url);
if (isset($success)) {
return $success;
}
}
}
/**
* Parse a URL or embed code and return a file object.
*
* If a remote stream doesn't claim the parsed URL in media_parse_to_uri(),
* then we'll copy the file locally.
*
* @NOTE The implementing modules may throw an error, which will not be caught
* here; it's up to the calling function to catch any thrown errors.
*
* @see media_parse_to_uri().
* @see media_add_from_url_submit().
*/
function media_parse_to_file($url) {
try {
$uri = media_parse_to_uri($url);
}
catch (Exception $e) {
// Pass the error along.
throw $e;
return;
}
if (isset($uri)) {
// Attempt to load an existing file from the unique URI.
$select = db_select('file_managed', 'f')
->extend('PagerDefault')
->fields('f', array('fid'))
->condition('uri', $uri);
$fid = $select->execute()->fetchCol();
if (!empty($fid)) {
$file = file_load(array_pop($fid));
return $file;
}
}
if (isset($uri)) {
// The URL was successfully parsed to a URI, but does not yet have an
// associated file: save it!
$file = file_uri_to_object($uri);
file_save($file);
}
else {
// The URL wasn't parsed. We'll try to save a remote file.
// Copy to temporary first.
$source_uri = file_stream_wrapper_uri_normalize('temporary://' . basename($url));
if (!@copy(@$url, $source_uri)) {
throw new Exception('Unable to add file ' . $url);
return;
}
$source_file = file_uri_to_object($source_uri);
$scheme = variable_get('file_default_scheme', 'public') . '://';
$uri = file_stream_wrapper_uri_normalize($scheme . $source_file->filename);
// Now to its new home.
$file = file_move($source_file, $uri, FILE_EXISTS_RENAME);
}
return $file;
}
/**
* When we save a file, we need to ensure any required media entity info is
* saved as well.
*/
function media_save_attached_file($file) {
if ($file) {
// This is terrible. Just awful.
if ($file->status != FILE_STATUS_PERMANENT) {
// We only want to manage files which are FILE_STATUS_PERMANENT
return;
}
$defaults = array (
'display' => TRUE,
);
// Make a copy of the file object to use as the media object
// (file is a field of media and the base table). This is weird.
// The problem is, files get managed elsewhere, and when they move from
// temporary to PERMANENT files, we want to create media entities for them.
// Unfortunately, there is no hook for that, so on every insert / update
// we have to look to see if the media entity has been setup
// and if not, we have to create it.
$media = media_load($file->fid);
if (!$media->file) { //Then we haven't setup the media entity.
$media->file = array();
$media->file[LANGUAGE_NONE] = array();
$media->file[LANGUAGE_NONE][0] = (array)$file + $defaults;
$media->is_new = TRUE;
field_attach_presave('media', $media);
field_attach_update('media', $media);
//field_attach_insert('media', $media);
}
}
return $media;
}
/**
* Implements hook_element_info().
*
* Media field... Not sure if this is going to work :)
*/
function media_element_info() {
$types = array();
$types['media'] = array(
'#input' => TRUE,
'#process' => array('media_element_process'),
'#value_callback' => 'media_element_value',
//'#element_validate' => array('file_managed_file_validate'),
'#theme' => 'media_element',
'#theme_wrappers' => array('form_element'),
'#progress_indicator' => 'throbber',
'#progress_message' => NULL,
'#upload_validators' => array(),
'#upload_location' => NULL,
'#extended' => FALSE,
'#attached' => array(
'library' => array(
array('media', 'media_browser'),
),
),
);
return $types;
}
function media_element_process(&$element) {
// @todo: make this work for file and image fields
}
/**
* Implement hook_filter_info
*/
function media_filter_info() {
$filters['media_filter'] = array(
'title' => t('Converts Media tags to Markup'),
'description' => t('This filter will convert [[{type:media... ]] tags into markup'),
'process callback' => 'media_filter',
'tips callback' => 'media_filter_tips', // @TODO not implemented
'cache' => FALSE,
);
return $filters;
}
/**
* Sets the status to FILE_STATUS_PERMANENT.
*
* @param $file
* A file object.
*/
function _media_save_file_permenently(&$file) {
if ($file->status < FILE_STATUS_PERMANENT) {
$file->status = FILE_STATUS_PERMANENT;
file_save($file);
}
}