' .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 .= '
'; return $output; } } /** * Implement of hook_menu(). */ function media_menu() { // For managing different types of media and the fields associated with them. $items['admin/config/media/types'] = 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/config/media/types/manage/%media_type'] = array( 'title' => 'Manage media', 'title callback' => 'media_type_page_title', 'title arguments' => array(5), 'description' => 'Manage files used on your site.', 'page callback' => 'drupal_get_form', 'page arguments' => array('media_admin_type_manage_form', 5), 'access arguments' => array('administer media'), 'file' => 'media.admin.inc', ); $items['admin/config/media/types/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('import 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, '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'), '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', // Use the same setting as node module 'theme callback' => '_node_custom_theme', ); $items['media/%media_multi/multiedit'] = array( 'title' => 'Multi Edit', 'page callback' => 'media_page_multiedit', 'page arguments' => array(1), 'access callback' => 'media_access', 'access arguments' => array('edit'), 'weight' => 0, 'file' => 'media.pages.inc', // Use the same setting as node module 'theme callback' => '_node_custom_theme', ); $items['media/%media/delete'] = array( 'title' => 'Delete', 'page callback' => 'media_page_delete', 'page arguments' => array(1), 'access callback' => 'media_access', 'access arguments' => array('edit'), 'weight' => 1, 'type' => MENU_LOCAL_TASK, 'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE, 'file' => 'media.pages.inc', // Use the same setting as node module 'theme callback' => '_node_custom_theme', ); $items['admin/content/media/browser'] = $items['media/browser']; $items['admin/content/media/browser']['type'] = MENU_LOCAL_ACTION; /** * End-user CRUD screens * @var unknown_type */ $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; } /** * Implements hook_admin_paths(). */ function media_admin_paths() { $paths = array( 'media/*/edit' => TRUE, 'media/*/multiedit' => TRUE, 'media/*/delete' => TRUE, ); return $paths; } /** * 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.'), ), 'import media' => array( 'title' => t('Import media files from the local filesystem'), 'description' => t('Simple file importer'), ), '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', ), // Administrative thumbnail previews. 'media_admin_thumbnail_operations' => array( 'variables' => array('file' => NULL), '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' => 'theme/media-dialog-page', 'file' => 'media.theme.inc', ), // 'media_element' => array( 'render element' => 'element', 'file' => 'media.theme.inc', ), 'media_formatter_large_icon' => array( 'variables' => array('file' => NULL), '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); } /** * Implements hook_file_delete(). */ function media_file_delete($file) { // @todo: call field_attach_delete() from here, or add media_delete(). // Remove this file from the {media_filter_usage} table. db_delete('media_filter_usage')->condition('fid', $file->fid)->execute(); } /** * Implements hook_image_style_flush(). * * This hook is invoked by Drupal core when cached image derivatives are no * longer valid. * * @see media_styles_style_flush() * @see media_file_style_flush() */ function media_image_style_flush($style) { // When a image style is flushed, clear the filter and field caches. media_filter_invalidate_caches(); } /** * Implements hook_file_style_flush(). * * This hook is invoked by the File Styles module in Styles 1.x. * * @see media_styles_style_flush() */ function media_file_style_flush($style) { // When a file style is flushed, clear the filter and field caches. media_filter_invalidate_caches(); } /** * Implements hook_styles_style_flush(). * * This hook is invoked by the Styles module in Styles 2.x. * * @see media_file_style_flush() */ function media_styles_style_flush($style) { // When a style is flushed, clear the filter and field caches. media_filter_invalidate_caches(); } /** * 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'; // temporary fix while awaiting fix for 914786 if (module_exists('admin_menu')) { admin_menu_suppress(); } 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(). */ function media_media_operations() { $operations = array( 'delete' => array( 'label' => t('Delete'), 'callback' => NULL, ), 'edit' => array( 'label' => t('Edit'), 'callback' => NULL, 'redirect' => 'media/%fids/multiedit' ), ); if (!module_exists('multiform')) { // If the multiform module is not installed, do not show this option. unset($operations['edit']); } return $operations; } /** * Implementation of hook_forms() */ function media_forms($form_id, $args) { $forms = array(); // To support the multiedit form, each form has to have a unique ID. // So we name all the forms media_edit_N where the first requested form is // media_edit_0, 2nd is media_edit_1, etc. if ($form_id != 'media_edit' && (strpos($form_id, 'media_edit') === 0)){ $forms[$form_id] = array( 'callback' => 'media_edit', ); } return $forms; } /* ***************************************** */ /* API FUNCTIONS */ /* ***************************************** */ /** * Loads a Media Asset * * @param string $fid * @return Media */ function media_load($fid) { if ($files = entity_load('media', array($fid))) { return array_shift($files); } return FALSE; } /** * Loads multiple media Assets * * @param string $fids * Separated by plus(+) signs. e.g. 3+6+12+99 * * @return array * An array of corresponding media entities. */ function media_multi_load($fids) { $fids = explode(" ", $fids); if ($files = media_load_multiple($fids)) { return $files; } } /** * Load media entities from the database. * * @see entity_load() * * @param $fids * An array of media file IDs. * @param $conditions * An array of conditions on the {file_managed} table in the form * 'field' => $value. * @param $reset * Whether to reset the internal media_load cache. * * @return * An array of media objects indexed by fid. */ function media_load_multiple($fids = array(), $conditions = array(), $reset = FALSE) { return entity_load('media', $fids, $conditions, $reset); } /** * 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); media_filter_invalidate_caches($media->fid); } // Since the Media module can't make files into fieldable entities directly // without hacking Drupal core, it does so indirectly via a "media" entity // type. The indirection requires media entities to have one of its fields be // a file field containing the file that defines the media entity itself. // The File module treats this media entity / file field relationship like // any other entity / file field relationship, and increments the file usage // count whenever the media entity is saved, as part of the // field_attach_(insert|update)() execution above. However, we don't want the // media entity's reference to itself to count as usage, since that would // result in all media entities being "in use" and undeletable, so we remove // the self-referential usage record. if ($media->fid) { // file_usage_delete() requires the first argument to be an instance of // stdClass, but $media is an instance of some other class, so we cast it. file_usage_delete((object)(array) $media, 'file', 'media', $media->fid); } } /** * Access callback for media assets. */ function media_access($op) { return (user_access('administer media') || user_access($op . ' media')); } /** * Implements hook_file_download_access(). */ function media_file_download_access($field, $entity_type, $entity) { if ($entity_type == 'media') { return media_access('view'); } } /** * Returns the type of the media file to be used as a page title. */ function media_type_page_title($type) { return t('@type media type settings', array('@type' => $type->label)); } /** * 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. * @param $use_existing * (Optional) If TRUE and there's an existing file in the {file_managed} * table with the passed in URI, then that file object is returned. * Otherwise, a new file object is returned. * @return * A file object, or FALSE on error. */ function file_uri_to_object($uri, $use_existing = FALSE) { if ($use_existing) { $query = db_select('file_managed', 'f') ->fields('f', array('fid')) ->condition('uri', $uri) ->execute() ->fetchCol(); if (!empty($query)) { $file = file_load(array_shift($query)); } } if (!isset($file)) { 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); // This is gagged because some uris will not support it. $file->filesize = @filesize($uri); $file->timestamp = REQUEST_TIME; $file->status = FILE_STATUS_PERMANENT; $file->is_new = TRUE; } 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('group' => JS_LIBRARY, 'weight' => - 5), $path . '/javascript/util/json2.js' => array('group' => JS_LIBRARY), $path . '/javascript/util/ba-debug.min.js' => array('group' => 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('group' => 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('group' => JS_DEFAULT), ), 'css' => array( $path . '/css/media.browser.css' => array('group' => 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); // @todo Why aren't we calling media_save() instead of the above code. // Since we aren't, we have to update usage here too, as we do in // media_save(). file_usage_delete((object)(array) $media, 'file', 'media', $media->fid); // Also clear cached text formats from here. media_filter_invalidate_caches($media->fid); } } 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_wrappers' => array('container'), '#progress_indicator' => 'throbber', '#extended' => FALSE, '#media_options' => array( 'global' => array( 'types' => array(), // Example: array('image', 'audio'); 'schemes' => array(), // Example: array('http', 'ftp', 'flickr'); ), ), '#attributes' => array( 'class' => array('media-widget'), ), '#attached' => array( 'library' => array( array('media', 'media_browser'), ), ), ); return $types; } function media_element_process(&$element, &$form_state, $form) { $fid = isset($element['#value']['fid']) ? $element['#value']['fid'] : 0; //$fid = isset($element['#default_value']['fid']) ? $element['#default_value']['fid'] : 0; $media = media_load($fid); $path = drupal_get_path('module', 'media'); $element['title'] = array( '#type' => 'item', '#title' => $element['#title'], '#markup' => '', '#description' => $element['#description'], ); //@TODO: This should be loaded from the JS in case of a failed form submission. $markup = ''; if (!empty($media)) { $preview = media_get_thumbnail_preview($media); $markup = drupal_render($preview); } $element['preview'] = array( '#type' => 'item', '#markup' => $markup, '#prefix' => '
', '#suffix' => '
' . t(media_variable_get('field_select_media_text')) . '' . t(media_variable_get('field_remove_media_text')) . '', ); /** * This section handles fields on media when media is added as a field. * It is pretty unpolished, so hiding it for now. */ // $element['more_fields_wrapper'] = array( // '#type' => 'fieldset', // '#collapsible' => TRUE, // '#collapsed' => TRUE, // '#title' => t('Meta-data'), // ); // // $element['more_fields_wrapper']['edit'] = array( // '#type' => 'markup', // '#markup' => l(t('Edit'), 'media/' . $fid . '/edit', array('query' => array('render' => 'media-popup'), 'attributes' => array('class'=> array('media-edit-link')))), // ); // // // Oh god, there must be a better way to add a wrapper. // $parents = $element['#parents']; // array_push($parents, 'more_fields'); // // if ($file) { // $media_wo_file = $file; // $media_wo_file->file = array(); // heh, let's see how this works. // field_attach_prepare_view('media', array($file->fid => $media_wo_file)); // entity_prepare_view('media', array($file->fid => $media_wo_file)); // $element['more_fields_wrapper']['more_fields'] = field_attach_view('media', $media_wo_file, 'media_preview'); // } //@HACK: @todo: this is so I can find it in media.js without putting every field in a settings variable. // If I make it hidden (which it should be) it will go to the top of the form... I know this sucks. // This is hidden in media.css $element['fid'] = array( '#type' => 'textfield', '#default_value' => $fid, '#attributes' => array('class' => array('fid')), ); // Media browser attach code. $element['#attached']['js'][] = drupal_get_path('module', 'media') . '/javascript/media.js'; $settings = array(); $setting['media']['elements'][$element['#id']] = $element['#media_options']; $element['#attached']['js'][] = array( 'type' => 'setting', 'data' => $setting, ); // hmm... Might need to think about this. // All settings would likely apply to all media in a multi-value, but what about passing the existing fid? module_load_include('inc', 'media', 'media.browser'); media_include_browser_js(); return $element; // @todo: make this work for file and image fields } /** * Implements hook_filter_info(). * * For performance, the media filter is allowed to be cached by default. See * media_filter_invalidate_caches() for details. Some sites may use advanced * media styles with rendering implentations that differs per theme or based on * other runtime information. For these sites, it may be necessary to implement * a module with a hook_filter_info_alter() implementation that sets * $info['media_filter']['cache'] to FALSE. * * @see media_filter_invalidate_caches() */ 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 ); 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); } } /** * Returns a renderable array with the necessary classes to support a media * thumbnail. Also provides default fallback images if no image is available. * * @param $media * * @return array * Renderable array. */ function media_get_thumbnail_preview($media, $link = NULL) { $preview = field_view_field('media', $media, 'file', 'media_preview'); $preview['#show_names'] = TRUE; $preview['#add_link'] = $link; $preview['#theme_wrappers'][] = 'media_thumbnail'; return $preview; } /** * Implements hook_entity_info(). */ function media_entity_info() { $view_modes = media_field_view_modes('media'); $return = array( 'media' => array( 'label' => t('Media'), 'base table' => 'file_managed', 'fieldable' => TRUE, 'view modes' => $view_modes, 'entity keys' => array( 'id' => 'fid', 'bundle' => 'type', // @TODO: Why 'type' here... ), 'bundle keys' => array( 'bundle' => 'name', // @TODO: But 'name' here? ), 'bundles' => array(), ), ); $media_types = media_type_get_types(); foreach ($media_types as $type => $bundle_info) { $return['media']['bundles'][$type] = (array)$bundle_info; $return['media']['bundles'][$type]['admin'] = array( 'label' => $bundle_info->label, 'path' => 'admin/config/media/types/manage/%media_type', 'real path' => 'admin/config/media/types/manage/' . $type, 'bundle argument' => 5, // @TODO: Add an 'administer media types' perm? 'access arguments' => array('administer site configuration'), ); } // @TODO: Do we need to define search-related view modes for attached fields? return $return; } /** * Check that the media is one of the selected types. * * @param $file * A Drupal file object. * @param $types * An array of media type names * @return * An array. If the file type is not allowed, it will contain an error * message. * * @see hook_file_validate() */ function media_file_validate_types(stdClass $file, $types) { $errors = array(); if (!in_array(media_get_type($file), $types)) { $errors[] = t('Only the following types of files are allowed to be uploaded: %types-allowed', array('%types-allowed' => implode(', ', $types))); } return $errors; } /** * Implements hook_flush_caches(). */ function media_flush_caches() { // Garbage collection for the {media_filter_usage} table. If an fid was last // recorded four months ago (minimum three months due to logic in // media_filter_track_usage()), remove it from this table while the filter // and field caches are being cleared. If the fid is still in use, it will // be added back to the table the next time check_markup() runs on that // content. This prevents fids from staying in this table indefinitely, // even if the post that references them is edited or deleted. db_delete('media_filter_usage')->condition('timestamp', REQUEST_TIME - 86400 * 120, '<')->execute(); }