diff --git a/media.pages.inc b/media.pages.inc index a4edf08..7221a61 100644 --- a/media.pages.inc +++ b/media.pages.inc @@ -19,8 +19,8 @@ * * @see file_download() */ -function media_download($media) { - $uri = $media->uri; +function media_download($file) { + $uri = $file->uri; $scheme = file_uri_scheme($uri); if (file_stream_wrapper_valid_scheme($scheme) && file_exists($uri)) { // Let other modules provide headers and controls access to the file. @@ -40,11 +40,11 @@ function media_download($media) { } // Default Content-Type and Content-Length headers, in case no other // hook_file_download() implementation set them. - $name = mime_header_encode($media->filename); - $type = mime_header_encode($media->filemime); + $name = mime_header_encode($file->filename); + $type = mime_header_encode($file->filemime); $headers += array( 'Content-Type' => $type . '; name="' . $name . '"', - 'Content-Length' => $media->filesize, + 'Content-Length' => $file->filesize, ); // Force the Content-Disposition header, regardless of what other // hook_file_download() implementations normally set. diff --git a/media_gallery.fields.inc b/media_gallery.fields.inc index 54448ac..18e01e5 100644 --- a/media_gallery.fields.inc +++ b/media_gallery.fields.inc @@ -11,40 +11,68 @@ */ function media_gallery_field_formatter_info() { return array( - 'media_gallery_thumbnail' => array( - 'label' => t('Gallery thumbnail'), - 'field types' => array('media'), - ), - 'media_gallery_lightbox' => array( - 'label' => t('Gallery lightbox'), - 'field types' => array('media'), - ), - 'media_gallery_detail' => array( - 'label' => t('Gallery detail'), - 'field types' => array('media'), - ), - 'media_gallery_block_thumbnail' => array( - 'label' => t('Block thumbnail'), - 'field types' => array('media'), - ), - 'media_gallery_collection_thumbnail' => array( - 'label' => t('Collection thumbnail'), + 'media_gallery' => array( + 'label' => t('Gallery media'), 'field types' => array('media'), + 'settings' => array('file_view_mode' => 'media_gallery_thumbnail'), ), ); } /** + * Implements hook_field_formatter_settings_form(). + */ +function media_gallery_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) { + $display = $instance['display'][$view_mode]; + $settings = $display['settings']; + + $element = array(); + + if ($display['type'] == 'media_gallery') { + $element['file_view_mode'] = array( + '#title' => t('File view mode'), + '#type' => 'select', + '#default_value' => $settings['file_view_mode'], + '#options' => media_gallery_file_view_modes(), + ); + } + + return $element; +} + +/** + * Implements hook_field_formatter_settings_summary(). + */ +function media_gallery_field_formatter_settings_summary($field, $instance, $view_mode) { + $display = $instance['display'][$view_mode]; + $settings = $display['settings']; + + $summary = ''; + + if ($display['type'] == 'media_gallery') { + $entity_info = entity_get_info('file'); + $file_view_mode_label = isset($entity_info['view modes'][$settings['file_view_mode']]) ? $entity_info['view modes'][$settings['file_view_mode']]['label'] : t('Default'); + $summary = t('File view mode: @view_mode', array('@view_mode' => $file_view_mode_label)); + } + + return $summary; +} + +/** * Implements hook_field_formatter_view(). */ function media_gallery_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) { $element = array(); + if (!$items) { + return $element; + } + $file_view_mode = $display['settings']['file_view_mode']; // The Formatter Reference module allows per-entity, rather than // per-bundle, formatter assignment, but it only works when the field being // formatted and the reference field are part of the same entity. For media // galleries, we want to enable formatter reference fields on the gallery node - // to be used as formatters for fields within the media entities, so we pass + // to be used as formatters for fields within the file entities, so we pass // along those field values to them. Here we just collect all formatter // reference field names for the entity. $formatter_reference_fields = array(); @@ -57,46 +85,37 @@ function media_gallery_field_formatter_view($entity_type, $entity, $field, $inst } } } - if (!$items) { - return $element; - } - // Each item is a fully loaded media entity, thanks to - // media_field_prepare_view(), but $items is keyed by delta rather than by - // entity id, and we need to invoke functions that require entities keyed by - // id. While we're in here, we also add some information to each media entity. - foreach ($items as $delta => $media_entity) { - $media_entities[$media_entity->fid] = $media_entity; + // Prepare the referenced file entities for viewing. + $files = array(); + foreach ($items as $delta => $item) { + $file = $item['file']; - // Pass along formatter reference field values from the entity to the media - // entity. + // Pass along formatter reference field values from the gallery to the file. foreach ($formatter_reference_fields as $field_name) { - if (!isset($media_entity->{$field_name}) && isset($entity->{$field_name})) { - $media_entity->{$field_name} = $entity->{$field_name}; + if (!isset($file->{$field_name}) && isset($entity->{$field_name})) { + $file->{$field_name} = $entity->{$field_name}; } } - // Set a default value for the media entity's media_title field. + // Set a default value for the file's media_title field. // @todo Eventually, fix this to take into account the possibility of a // multilingual media title field. - if (isset($media_entity->media_title) && !isset($media_entity->media_title[LANGUAGE_NONE][0]['value'])) { - $media_entity->media_title[LANGUAGE_NONE][0]['value'] = _media_gallery_get_media_title($media_entity); + if (isset($file->media_title) && !isset($file->media_title[LANGUAGE_NONE][0]['value'])) { + $file->media_title[LANGUAGE_NONE][0]['value'] = _media_gallery_get_media_title($file); } + + $files[$file->fid] = $file; } + field_attach_prepare_view('file', $files, $file_view_mode); + entity_prepare_view('file', $files); - // Get the view of each media entity. The Media module doesn't provide a - // media_view_multiple() function, so we invoke field_attach_prepare_view(), - // entity_prepare_view(), and field_attach_view() ourselves. - field_attach_prepare_view('media', $media_entities, $display['type']); - entity_prepare_view('media', $media_entities); - foreach ($items as $delta => $media_entity) { - $element[$delta] = field_attach_view('media', $media_entity, $display['type'], $langcode); - // Theme the resulting media entity render array, according to the chosen - // formatter. Add information needed by those theme functions. + // View each file. + foreach ($items as $delta => $item) { + $element[$delta] = file_view($item['file'], $file_view_mode, $langcode); $element[$delta]['#media_gallery_entity_type'] = $entity_type; $element[$delta]['#media_gallery_entity'] = $entity; - $element[$delta]['#media_gallery_media_entity'] = $media_entity; - switch ($display['type']) { + switch ($file_view_mode) { case 'media_gallery_thumbnail': $element[$delta]['#theme'] = 'media_gallery_media_item_thumbnail'; break; @@ -130,13 +149,14 @@ function media_gallery_field_formatter_view($entity_type, $entity, $field, $inst * @see media_gallery_detail_page() * @see media_gallery_lightbox_page() */ -function media_gallery_item_view($gallery_node, $media_entity, $display) { +function media_gallery_item_view($gallery_node, $file, $file_view_mode) { + $display = array('type' => 'media_gallery', 'settings' => array('file_view_mode' => $file_view_mode)); $id = $gallery_node->nid; $field_name = 'media_gallery_media'; $field = field_info_field($field_name); $instance = field_info_instance('node', $field_name, 'media_gallery'); $langcode = key($gallery_node->{$field_name}); - $items = array(0 => $media_entity); + $items = array(0 => array('fid' => $file->fid, 'file' => $file)); $items_multi = array($id => &$items); // Taken by reference, so can't inline. $formatter = field_info_formatter_types($display['type']); // "prepare_view" function is optional. @@ -160,9 +180,9 @@ function media_gallery_item_view($gallery_node, $media_entity, $display) { function media_gallery_form_field_ui_display_overview_form_alter(&$form) { // See formatter_reference_form_field_ui_display_overview_form_alter() for // more details about why this form needs to be altered. All we're doing here - // is allowing a gallery node's formatter reference field to format media + // is allowing a gallery node's formatter reference field to format file // entity fields. - if (module_exists('formatter_reference') && $form['#entity_type'] == 'media') { + if (module_exists('formatter_reference') && $form['#entity_type'] == 'file') { $gallery_field_instances = field_info_instances('node', 'media_gallery'); $fields = field_info_fields(); foreach ($form['#fields'] as $field_name) { @@ -190,10 +210,10 @@ function media_gallery_form_field_ui_display_overview_form_alter(&$form) { /** * Gets the title of a media entity either from the media_title field or based on the filename. */ -function _media_gallery_get_media_title($media_entity) { +function _media_gallery_get_media_title($file) { // If the entity has a value for the title field, use it. - if (isset($media_entity->media_title[LANGUAGE_NONE][0]['value'])) { - $title = $media_entity->media_title[LANGUAGE_NONE][0]['value']; + if (isset($file->media_title[LANGUAGE_NONE][0]['value'])) { + $title = $file->media_title[LANGUAGE_NONE][0]['value']; } // Otherwise, base it on the file's filename, but with some adjustments for // human-friendly display. @@ -207,7 +227,7 @@ function _media_gallery_get_media_title($media_entity) { ); // In addition to above replacements, also capitalize the first letter of // each word, and remove leading and trailing spaces. - $title = trim(ucwords(preg_replace(array_keys($replacements), array_values($replacements), $media_entity->filename))); + $title = trim(ucwords(preg_replace(array_keys($replacements), array_values($replacements), $file->filename))); } return $title; } diff --git a/media_gallery.install b/media_gallery.install index b65136c..047bbc3 100644 --- a/media_gallery.install +++ b/media_gallery.install @@ -44,26 +44,15 @@ function media_gallery_install() { // Save the new bundle settings. field_bundle_settings('node', 'media_gallery', $bundle_settings); - // Set the "Gallery Thumbnail", "Gallery Lightbox", and "Gallery Detail" view - // modes to be enabled for image and video bundles and default those bundle's - // file field display appropriately. Do this before creating new field - // instances in media entity bundles. + // Enable custom display settings for gallery context view modes for file + // types that can be in a gallery. // @todo Add a hook_update() function for 'video'. foreach (array('image', 'video') as $bundle) { - $bundle_settings = field_bundle_settings('media', $bundle); - $bundle_settings['view_modes']['media_gallery_thumbnail']['custom_settings'] = TRUE; - $bundle_settings['view_modes']['media_gallery_lightbox']['custom_settings'] = TRUE; - $bundle_settings['view_modes']['media_gallery_detail']['custom_settings'] = TRUE; - $bundle_settings['view_modes']['media_gallery_block_thumbnail']['custom_settings'] = TRUE; - $bundle_settings['view_modes']['media_gallery_collection_thumbnail']['custom_settings'] = TRUE; - field_bundle_settings('media', $bundle, $bundle_settings); - $instance = field_info_instance('media', 'file', $bundle); - $instance['display']['media_gallery_thumbnail'] = array('type' => 'styles_file_media_gallery_thumbnail', 'label' => 'hidden'); - $instance['display']['media_gallery_lightbox'] = array('type' => 'styles_file_media_gallery_large', 'label' => 'hidden'); - $instance['display']['media_gallery_detail'] = array('type' => 'styles_file_media_gallery_large', 'label' => 'hidden'); - $instance['display']['media_gallery_block_thumbnail'] = array('type' => 'styles_file_media_gallery_thumbnail', 'label' => 'hidden'); - $instance['display']['media_gallery_collection_thumbnail'] = array('type' => 'styles_file_media_gallery_thumbnail', 'label' => 'hidden'); - field_update_instance($instance); + $bundle_settings = field_bundle_settings('file', $bundle); + foreach (media_gallery_file_view_modes() as $view_mode => $label) { + $bundle_settings['view_modes'][$view_mode]['custom_settings'] = TRUE; + } + field_bundle_settings('file', $bundle, $bundle_settings); } // Clear caches so that our implementation of hook_image_default_styles() is @@ -72,7 +61,9 @@ function media_gallery_install() { cache_clear_all('*', 'cache', TRUE); drupal_static_reset('image_styles'); drupal_static_reset('image_effects'); - styles_style_flush(); + if (module_exists('styles')) { + styles_style_flush(); + } // Add the taxonomy vocabulary for media gallery collections. $vocabulary = media_gallery_create_taxonomy_vocab(); @@ -379,22 +370,26 @@ function _media_gallery_controlled_instances($group = NULL) { ), 'display' => array( 'default' => array( - 'type' => 'media_gallery_thumbnail', + 'type' => 'media_gallery', + 'settings' => array('file_view_mode' => 'media_gallery_thumbnail'), 'label' => 'hidden', 'weight' => 2, ), 'full' => array( - 'type' => 'media_gallery_thumbnail', + 'type' => 'media_gallery', + 'settings' => array('file_view_mode' => 'media_gallery_thumbnail'), 'label' => 'hidden', 'weight' => 2, ), 'teaser' => array( - 'type' => 'media_gallery_collection_thumbnail', + 'type' => 'media_gallery', + 'settings' => array('file_view_mode' => 'media_gallery_collection_thumbnail'), 'label' => 'hidden', 'weight' => 2, ), 'media_gallery_block' => array( - 'type' => 'media_gallery_block_thumbnail', + 'type' => 'media_gallery', + 'settings' => array('file_view_mode' => 'media_gallery_block_thumbnail'), 'label' => 'hidden', 'weight' => 2, ), @@ -856,10 +851,9 @@ function _media_gallery_ensure_media_instances() { foreach ($instances as $instance) { $instance_copy = $instance; $instance_copy += array( - 'entity_type' => 'media', + 'entity_type' => 'file', 'bundle' => $bundle, ); - // Friendlier label than "media". $label = in_array($bundle, array('image', 'audio', 'video')) ? $bundle : 'file'; if ($instance_copy['field_name'] == 'field_tags' && !isset($instance_copy['description'])) { $instance_copy['description'] = $t("Enter a comma-separated list of words to describe your $label."); @@ -1254,3 +1248,27 @@ function media_gallery_update_7008() { field_update_instance($instance); } } + +/** + * Update old per-view-mode media_gallery_* field formatters to the generic media_gallery formatter with a setting. + */ +function media_gallery_update_7009() { + $instances = array(); + $fields = field_read_fields(array('type' => 'media'), array('include_inactive' => TRUE)); + foreach ($fields as $field) { + $instances = array_merge($instances, field_read_instances(array('field_id' => $field['id']), array('include_inactive' => TRUE))); + } + foreach ($instances as $instance) { + $update_instance = FALSE; + foreach ($instance['display'] as $view_mode => $display) { + if (in_array($display['type'], array('media_gallery_thumbnail', 'media_gallery_lightbox', 'media_gallery_detail', 'media_gallery_block_thumbnail', 'media_gallery_collection_thumbnail'))) { + $update_instance = TRUE; + $instance['display'][$view_mode]['type'] = 'media_gallery'; + $instance['display'][$view_mode]['settings'] = array('file_view_mode' => $display['type']); + } + } + if ($update_instance) { + field_update_instance($instance); + } + } +} diff --git a/media_gallery.module b/media_gallery.module index a30dc77..6d1641e 100644 --- a/media_gallery.module +++ b/media_gallery.module @@ -19,6 +19,19 @@ require_once(dirname(__FILE__) . '/fields_rsi_prevention.inc'); define('MEDIA_GALLERY_PAGER_ELEMENT', 1); /** + * Helper function to return the view modes used by this module for displaying files in gallery context. + */ +function media_gallery_file_view_modes() { + return array( + 'media_gallery_thumbnail' => t('Gallery thumbnail'), + 'media_gallery_lightbox' => t('Gallery lightbox'), + 'media_gallery_detail' => t('Gallery detail'), + 'media_gallery_block_thumbnail' => t('Gallery block thumbnail'), + 'media_gallery_collection_thumbnail' => t('Gallery collection thumbnail'), + ); +} + +/** * Implements hook_menu(). */ function media_gallery_menu() { @@ -45,20 +58,20 @@ function media_gallery_menu() { 'page arguments' => array('gallery', 3), 'file' => 'media_gallery.pages.inc', ); - $items['media-gallery/detail/%node/%media'] = array( + $items['media-gallery/detail/%node/%file'] = array( 'page callback' => 'media_gallery_detail_page', 'page arguments' => array(2, 3), 'access callback' => 'node_access', 'access arguments' => array('view', 2), 'file' => 'media_gallery.pages.inc', ); - $items['media-gallery/detail/%node/%media/view'] = array( + $items['media-gallery/detail/%node/%file/view'] = array( 'title' => 'View', 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10, ); - // An in-gallery-context version of media/%media/edit. - $items['media-gallery/detail/%node/%media/edit'] = array( + // An in-gallery-context version of media/%file/edit. + $items['media-gallery/detail/%node/%file/edit'] = array( 'title' => 'Edit', 'page callback' => 'media_gallery_media_page_edit', 'page arguments' => array(2, 3), @@ -71,7 +84,7 @@ function media_gallery_menu() { // Use the same setting as node module 'theme callback' => '_node_custom_theme', ); - $items['media-gallery/detail/%node/%media/remove'] = array( + $items['media-gallery/detail/%node/%file/remove'] = array( 'title' => 'Remove', 'page callback' => 'drupal_get_form', 'page arguments' => array('media_gallery_remove_item_form', 2, 3), @@ -83,7 +96,7 @@ function media_gallery_menu() { // Use the same setting as node module. 'theme callback' => '_node_custom_theme', ); - $items['media-gallery/lightbox/%node/%media'] = array( + $items['media-gallery/lightbox/%node/%file'] = array( 'page callback' => 'media_gallery_lightbox_page', 'page arguments' => array(2, 3), 'access callback' => 'node_access', @@ -111,7 +124,7 @@ function media_gallery_menu() { ); // @todo Move to Media module once it is ready. - $items['media/%media/download'] = array( + $items['media/%file/download'] = array( 'title' => 'Download', 'page callback' => 'media_download', 'page arguments' => array(1), @@ -139,7 +152,7 @@ function media_gallery_menu_alter(&$items) { // you won't be allowed to delete it anyway. Therefore, do not show // contextual links there. // @todo Perhaps this should be changed in the media module itself? - $items['media/%media/delete']['context'] = MENU_CONTEXT_PAGE; + $items['media/%file/delete']['context'] = MENU_CONTEXT_PAGE; } /** @@ -774,16 +787,16 @@ function _media_gallery_get_media_fid($file) { * * @param $node * The gallery node object. - * @param $media - * The media item object to remove from the gallery. + * @param $file + * The file to remove from the gallery. * * @return * The updated gallery node object. */ -function media_gallery_remove_item_from_gallery($node, $media) { +function media_gallery_remove_item_from_gallery($node, $file) { $items = &$node->media_gallery_media[LANGUAGE_NONE]; foreach ($items as $key => $item) { - if ($media->fid == _media_gallery_get_media_fid($item)) { + if ($file->fid == _media_gallery_get_media_fid($item)) { unset($items[$key]); } } @@ -794,19 +807,16 @@ function media_gallery_remove_item_from_gallery($node, $media) { /** * Implements hook_entity_info_alter(). */ -function media_gallery_entity_info_alter(&$info) { - // For each media field formatter we add, we also need to add the - // corresponding media entity view mode. - foreach (media_gallery_field_formatter_info() as $formatter_name => $formatter_info) { - if (in_array('media', $formatter_info['field types'])) { - $info['media']['view modes'][$formatter_name] = array('label' => $formatter_info['label'], 'custom settings' => FALSE); - } +function media_gallery_entity_info_alter(&$entity_info) { + // Add view modes for displaying files in gallery contexts. + foreach (media_gallery_file_view_modes() as $view_mode => $label) { + $entity_info['file']['view modes'][$view_mode] = array('label' => $label, 'custom settings' => FALSE); } - // Add a view mode for displaying a node in a media gallery block. - $info['node']['view modes']['media_gallery_block'] = array( - 'label' => t('Media gallery block'), - 'custom settings' => FALSE, - ); + + // Add a view mode for media_gallery_block_view() to use for displaying a + // media gallery node in a block. Drupal does not support restricting view + // modes to specific bundles. + $entity_info['node']['view modes']['media_gallery_block'] = array('label' => t('Media gallery block'), 'custom settings' => FALSE); } /** @@ -827,9 +837,9 @@ function media_gallery_form_alter(&$form, &$form_state, $form_id) { // Prepopulate the media_edit form with our best guess at the image title. if (!empty($form['media_title']) && empty($form['media_title'][LANGUAGE_NONE][0]['value']['#default_value'])) { $fid = $form['fid']['#value']; - $entity = media_load($fid); - if ($entity->type === 'image') { - $form['media_title'][LANGUAGE_NONE][0]['value']['#default_value'] = _media_gallery_get_media_title($entity); + $file = file_load($fid); + if ($file->type === 'image') { + $form['media_title'][LANGUAGE_NONE][0]['value']['#default_value'] = _media_gallery_get_media_title($file); } } // Prepopulate the license field with the correct default. @@ -1010,8 +1020,8 @@ function media_gallery_multiedit_remove_item($form, &$form_state) { $gallery = $form['#gallery']; if (!empty($form_state['values']['remove'])) { // Remove the item from the gallery. - $media = media_load($form_state['values']['fid']); - media_gallery_remove_item_from_gallery($gallery, $media); + $file = file_load($form_state['values']['fid']); + media_gallery_remove_item_from_gallery($gallery, $file); } // Redirect to the gallery page. $uri = entity_uri('node', $gallery); @@ -1034,7 +1044,7 @@ function media_gallery_media_edit_submit($form, &$form_state) { function media_gallery_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) { // Remove the ability for a user to select a license for externally hosted // media. - if ($entity_type == 'media') { + if ($entity_type == 'file') { $scheme = file_uri_scheme($entity->uri); // @todo Implement a more generic determination for when it makes sense for // a user to select a license and when it doesn't. @@ -1096,7 +1106,7 @@ function media_gallery_node_view_alter(&$build) { foreach (element_children($build['media_gallery_media']) as $delta) { // For each media item, add contextual links to the in-gallery-context // tasks that can be performed on a media item. - $fid = $build['media_gallery_media'][$delta]['#media_gallery_media_entity']->fid; + $fid = $build['media_gallery_media'][$delta]['#file']->fid; $build['media_gallery_media'][$delta]['#contextual_links']['media_gallery'] = array('media-gallery/detail', array($build['#node']->nid, $fid)); } } @@ -1383,16 +1393,16 @@ function media_gallery_multiedit_access($node) { * * @param $node * The gallery node object. - * @param $media - * The media item object to remove from the gallery. + * @param $file + * The file to remove from the gallery. * * @return * TRUE if access is granted; FALSE otherwise. */ -function media_gallery_remove_item_access($node, $media) { +function media_gallery_remove_item_access($node, $file) { // Only grant access if the user can edit the gallery and the provided media // item is attached to the gallery. - return media_gallery_edit_access($node) && in_array($media->fid, media_gallery_get_file_ids($node)); + return media_gallery_edit_access($node) && in_array($file->fid, media_gallery_get_file_ids($node)); } /** @@ -1499,15 +1509,11 @@ function media_gallery_styles_presets() { /** * Implements hook_media_wysiwyg_allowed_view_modes_alter(). * - * We don't want media's WYSIWYG form to show the view modes for galleries. - * So we remove any of the view modes we create. + * Removes view modes intended for gallery context only from the formatting + * options of media added to wysiwyg. */ -function media_gallery_media_wysiwyg_allowed_view_modes_alter($bundle, &$view_modes) { - require_once dirname(__FILE__) . '/media_gallery.fields.inc'; - // While these technically aren't view modes, we know they are all media - // view_modes as well as media formatters. - $media_gallery_view_modes = media_gallery_field_formatter_info(); - $view_modes = array_diff_key($view_modes, $media_gallery_view_modes); +function media_gallery_media_wysiwyg_allowed_view_modes_alter(&$view_modes) { + $view_modes = array_diff_key($view_modes, media_gallery_file_view_modes()); } /** @@ -1606,3 +1612,85 @@ function _media_gallery_allow_all_pathauto_aliases() { $GLOBALS['conf']['pathauto_taxonomy_pattern'] = $GLOBALS['conf']['media_gallery_original_pathauto_taxonomy_pattern']; } } + +/** + * Implements hook_ctools_plugin_api(). + * + * Lets CTools know which plugin APIs are implemented by this module. + */ +function media_gallery_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]); + } +} + +/** + * Implements hook_file_default_displays(). + * + * Provides default display configurations for files displayed in gallery view + * modes. + * + * @see file_entity_schema() + */ +function media_gallery_file_default_displays() { + $default_displays = array(); + + $default_image_styles = array( + 'media_gallery_thumbnail' => 'media_gallery_thumbnail', + 'media_gallery_lightbox' => 'media_gallery_large', + 'media_gallery_detail' => 'media_gallery_large', + 'media_gallery_block_thumbnail' => 'media_gallery_thumbnail', + 'media_gallery_collection_thumbnail' => 'media_gallery_thumbnail', + ); + + // People updating from older versions of Media module will have Styles module + // formatters enabled at weight 0. By default, we want the following taking + // precedence, but we do not want to disable the Styles module ones since + // those might be capable of rendering files not covered by these. Therefore, + // set these at a lower weight. + $default_weight = -1; + + foreach ($default_image_styles as $view_mode => $image_style) { + // Images. + $display_name = 'image__' . $view_mode . '__file_image'; + $default_displays[$display_name] = (object) array( + 'api_version' => 1, + 'name' => $display_name, + 'status' => 1, + 'settings' => array('image_style' => $image_style), + 'weight' => $default_weight, + ); + + // YouTube. + if (module_exists('media_youtube')) { + if (in_array($view_mode, array('media_gallery_lightbox', 'media_gallery_detail'))) { + // Video. Omit settings to use media_youtube_video defaults. + $display_name = 'video__' . $view_mode . '__media_youtube_video'; + $default_displays[$display_name] = (object) array( + 'api_version' => 1, + 'name' => $display_name, + 'status' => 1, + 'weight' => $default_weight, + ); + } + else { + // Thumbnail. + $display_name = 'video__' . $view_mode . '__media_youtube_image'; + $default_displays[$display_name] = (object) array( + 'api_version' => 1, + 'name' => $display_name, + 'status' => 1, + 'settings' => array('image_style' => $image_style), + 'weight' => $default_weight, + ); + } + } + } + + return $default_displays; +} diff --git a/media_gallery.pages.inc b/media_gallery.pages.inc index efc719a..0841b09 100644 --- a/media_gallery.pages.inc +++ b/media_gallery.pages.inc @@ -75,7 +75,7 @@ function media_gallery_list_galleries($term) { /** * Menu callback; view a single gallery media entity as its own page. */ -function media_gallery_detail_page($gallery_node, $media_entity) { +function media_gallery_detail_page($gallery_node, $file) { // Set the breadcrumb. $node_url_arguments = entity_uri('node', $gallery_node); drupal_set_breadcrumb(array( @@ -84,48 +84,20 @@ function media_gallery_detail_page($gallery_node, $media_entity) { )); // Set the title for the page - $title = _media_gallery_get_media_title($media_entity); + $title = _media_gallery_get_media_title($file); drupal_set_title($title); drupal_add_js(drupal_get_path('module', 'media_gallery') . '/media_gallery.js'); - $return = media_gallery_item_view($gallery_node, $media_entity, array('type' => 'media_gallery_detail')); + $return = media_gallery_item_view($gallery_node, $file, 'media_gallery_detail'); return $return; } /** - * Menu callback; view a single gallery media entity as the content of a lightbox. + * Menu callback; view a single gallery media item as the content of a lightbox. */ -function media_gallery_lightbox_page($gallery_node, $media_entity) { - // In order for the media to be displayed properly inside the lightbox and - // interact correctly with the JavaScript that resizes the lightbox, it needs - // to have 'width' and 'height' tags. To get these, however, requires an - // actual media file, so for media where the derivative hasn't been - // generated yet, we have to create it beforehand so we can determine its - // width and height. - $instance = field_info_instance('media', 'file', 'image'); - $display = field_get_display($instance, 'media_gallery_lightbox', $media_entity); - if (preg_match('@^styles_(.*?)_(.*?)$@i', $display['type'], $matches)) { - $style_name = $matches[2]; - // Check if the media file already exists at its final path; if not, create - // the derivative. - $style_path = image_style_path($style_name, $media_entity->uri); - if (!file_exists($style_path)) { - $style = image_style_load($style_name); - image_style_create_derivative($style, $media_entity->uri, $style_path); - } - // Now that we expect the file to actually exist, we can find its width and - // height and pass this information on as an override so that it appears as - // attributes on the final rendered media. - $info = image_get_info($style_path); - $media_entity->override = array( - 'width' => $info['width'], - 'height' => $info['height'], - ); - } - - // Display the gallery media entity in lightbox format. - return media_gallery_item_view($gallery_node, $media_entity, array('type' => 'media_gallery_lightbox')); +function media_gallery_lightbox_page($gallery_node, $file) { + return media_gallery_item_view($gallery_node, $file, 'media_gallery_lightbox'); } /** @@ -319,10 +291,10 @@ function media_gallery_add_images($node) { * * @param $node * The gallery node object. - * @param $media - * The media item object to remove from the gallery. + * @param $file + * The file to remove from the gallery. */ -function media_gallery_remove_item_form($form, &$form_state, $node, $media) { +function media_gallery_remove_item_form($form, &$form_state, $node, $file) { // Set the breadcrumb. $node_url_arguments = entity_uri('node', $node); drupal_set_breadcrumb(array( @@ -336,11 +308,11 @@ function media_gallery_remove_item_form($form, &$form_state, $node, $media) { ); $form['fid'] = array( '#type' => 'value', - '#value' => $media->fid, + '#value' => $file->fid, ); return confirm_form($form, - t('Are you sure you want to remove %file from the gallery %gallery?', array('%file' => $media->filename, '%gallery' => $node->title)), - "media-gallery/detail/{$node->nid}/{$media->fid}", + t('Are you sure you want to remove %file from the gallery %gallery?', array('%file' => $file->filename, '%gallery' => $node->title)), + "media-gallery/detail/{$node->nid}/{$file->fid}", t('The file will still be available in the media library to use elsewhere on this site.'), t('Remove file') ); @@ -351,19 +323,19 @@ function media_gallery_remove_item_form($form, &$form_state, $node, $media) { */ function media_gallery_remove_item_form_submit($form, &$form_state) { $node = node_load($form_state['values']['nid']); - $media = media_load($form_state['values']['fid']); - media_gallery_remove_item_from_gallery($node, $media); - drupal_set_message(t('The file %file was removed from the gallery.', array('%file' => $media->filename))); + $file = file_load($form_state['values']['fid']); + media_gallery_remove_item_from_gallery($node, $file); + drupal_set_message(t('The file %file was removed from the gallery.', array('%file' => $file->filename))); $form_state['redirect'] = "node/{$node->nid}"; } /** * Menu callback; presents the Media editing form in the context of a gallery. */ -function media_gallery_media_page_edit($gallery, $media) { +function media_gallery_media_page_edit($gallery, $file) { // The Media Gallery module defines titles differently than just using the // filename. - drupal_set_title(t('Edit @type @title', array('@type' => $media->type, '@title' => _media_gallery_get_media_title($media))), PASS_THROUGH); + drupal_set_title(t('Edit @type @title', array('@type' => $file->type, '@title' => _media_gallery_get_media_title($file))), PASS_THROUGH); // Set the breadcrumb. $node_url_arguments = entity_uri('node', $gallery); @@ -384,7 +356,7 @@ function media_gallery_media_page_edit($gallery, $media) { // Since we have a $form_state, we can't call drupal_get_form(), so pass the // expected arguments to drupal_build_form(). - $form_state['build_info']['args'] = array($media); + $form_state['build_info']['args'] = array($file); return drupal_build_form('media_edit', $form_state); } @@ -402,7 +374,7 @@ function media_gallery_media_page_multiedit($node) { foreach ($items as $item) { $fids[] = $item['fid']; } - $media_entities = media_load_multiple($fids); + $files = file_load_multiple($fids); // Load the Media module include file containing the form callback. // @todo To support AJAX enabled widgets within the form, we need to also add @@ -411,7 +383,7 @@ function media_gallery_media_page_multiedit($node) { module_load_include('inc', 'media', 'includes/media.pages'); // Build and process the form. - $form = media_page_multiedit($media_entities); + $form = media_page_multiedit($files); // Override the page title set by media_page_multiedit() and return the form. drupal_set_title(t('Edit media for @title', array('@type' => $node->type, '@title' => $node->title)), PASS_THROUGH); diff --git a/media_gallery.theme.inc b/media_gallery.theme.inc index 9fd1631..98fa5f9 100644 --- a/media_gallery.theme.inc +++ b/media_gallery.theme.inc @@ -117,16 +117,16 @@ function template_preprocess_media_gallery_media_item_thumbnail(&$variables) { } // Get the rendered file without annoying DIV wrappers. - $element['file']['#theme'] = 'media_gallery_file_field_inline'; + $element['file'] = array('#theme' => 'media_gallery_file_field_inline', '0' => $element['file']); $image = drupal_render($element['file']); $gallery_id = $element['#media_gallery_entity']->nid; - $media_id = $element['#media_gallery_media_entity']->fid; + $media_id = $element['#file']->fid; // Add a class that is a more targeted version of what template_preprocess() // automatically adds for this theme hook, to enable per-type (e.g., video vs. // image) styling. - $variables['classes_array'][] = drupal_html_class('media_gallery_media_item_thumbnail_' . $element['#media_gallery_media_entity']->type); + $variables['classes_array'][] = drupal_html_class('media_gallery_media_item_thumbnail_' . $element['#file']->type); // Add a class for the wrapper. $variables['classes_array'][] = 'media-gallery-item-wrapper'; @@ -170,9 +170,24 @@ function template_preprocess_media_gallery_media_item_thumbnail(&$variables) { function theme_media_gallery_media_item_lightbox($variables) { $element = $variables['element']; $gallery_node = new FieldsRSIPreventor($element['#media_gallery_entity']); - $media_entity = $element['#media_gallery_media_entity']; + $file = $element['#file']; + + // The lightbox JavaScript requires width and height attributes to be set on + // the displayed image, but if we're displaying an image derivative, we need + // to create it in order to know its width and height. + // @todo Improve the JavaScript to not require this. + if ($element['file']['#theme'] == 'image_style') { + $style_name = $element['file']['#style_name']; + $style_path = image_style_path($style_name, $file->uri); + if (!file_exists($style_path)) { + $style = image_style_load($style_name); + image_style_create_derivative($style, $file->uri, $style_path); + } + $info = image_get_info($style_path); + $element['file'] += array('#attributes' => array()); + $element['file']['#attributes'] += array('width' => $info['width'], 'height' => $info['height']); + } - unset($element['file']['#theme']); $image = drupal_render($element['file']); $matches = NULL; @@ -184,7 +199,7 @@ function theme_media_gallery_media_item_lightbox($variables) { } $gallery_id = $element['#media_gallery_entity']->nid; - $media_id = $element['#media_gallery_media_entity']->fid; + $media_id = $element['#file']->fid; // Create an array of variables to be added to the main image link. $link_vars = array(); @@ -193,7 +208,7 @@ function theme_media_gallery_media_item_lightbox($variables) { $link_vars['no_link'] = $element['#bundle'] == 'video' ? TRUE : FALSE; if ($gallery_node->getValue('media_gallery_allow_download') == TRUE) { - $download_link = $element['#bundle'] == 'video' ? l(t('View detail page'), $link_vars['link_path']) : theme('media_gallery_download_link', array('file' => $media_entity)); + $download_link = $element['#bundle'] == 'video' ? l(t('View detail page'), $link_vars['link_path']) : theme('media_gallery_download_link', array('file' => $file)); } else { @@ -209,7 +224,7 @@ function theme_media_gallery_media_item_lightbox($variables) { theme('media_gallery_item', $link_vars) . ''; // The license info has been themed already, keep it from being rendered as a child $element['field_license']['#access'] = FALSE; @@ -241,7 +256,7 @@ function theme_media_gallery_media_item_lightbox($variables) { function theme_media_gallery_media_item_detail($variables) { $element = $variables['element']; $gallery_node = new FieldsRSIPreventor($element['#media_gallery_entity']); - $media_entity = $element['#media_gallery_media_entity']; + $file = $element['#file']; // Page number for next and previous pages and current page. $i_next = NULL; $i_previous = NULL; @@ -260,7 +275,7 @@ function theme_media_gallery_media_item_detail($variables) { // text. $num_items = count($media_ids); foreach ($media_ids as $i => $id) { - if ($id == $media_entity->fid) { + if ($id == $file->fid) { $i_current = $i; break; } @@ -279,7 +294,7 @@ function theme_media_gallery_media_item_detail($variables) { } if ($gallery_node->getValue('media_gallery_allow_download') == TRUE) { - $download_link = $element['#bundle'] == 'video' ? ' ' : theme('media_gallery_download_link', array('file' => $media_entity)); + $download_link = $element['#bundle'] == 'video' ? ' ' : theme('media_gallery_download_link', array('file' => $file)); } else { // Very ugly fix: This prevents the license info from being either hidden @@ -298,7 +313,7 @@ function theme_media_gallery_media_item_detail($variables) { '