diff --git a/file_entity.api.php b/file_entity.api.php index a7d9e8a..6d9320f 100644 --- a/file_entity.api.php +++ b/file_entity.api.php @@ -110,9 +110,17 @@ function hook_file_formatter_FORMATTER_settings($form, &$form_state, $settings) } /** - * @todo Add documentation. + * Alter file display settings before display. + * + * @param array $displays + * An array of file display formatters from file_displays_load(); each display + * contains 'weight' and 'status' keys, as well as an array of settings. + * @param stdclass $file + * A Drupal file entity. + * @param string $view_mode + * The view mode being used to display the file. */ -function hook_file_displays_alter($displays, $file, $view_mode) { +function hook_file_displays_alter(&$displays, $file, $view_mode) { } /** diff --git a/file_entity.module b/file_entity.module index e2bf6c3..1cd5c67 100644 --- a/file_entity.module +++ b/file_entity.module @@ -138,6 +138,15 @@ function file_entity_entity_info_alter(&$entity_info) { $entity_info['file']['bundles'][$type] = array_intersect_key($info, drupal_map_assoc(array('label', 'admin'))); $entity_info['file']['view callback'] = 'file_view_multiple'; } + + // Add a view mode for each core image style. + $image_styles = image_styles(); + foreach ($image_styles as $style_name => $style) { + $entity_info['file']['view modes']['image__' . $style_name] = array( + 'label' => 'Core style: ' . $style['name'], + 'custom settings' => TRUE, + ); + } } /** @@ -353,7 +362,14 @@ function file_entity_file_formatter_file_image_view($file, $display, $langcode) * * Returns form elements for configuring the 'file_image' formatter. */ -function file_entity_file_formatter_file_image_settings($form, &$form_state, $settings) { +function file_entity_file_formatter_file_image_settings(&$form, &$form_state, $settings) { + // If this view mode is based off of a core image style but the display has + // not been configured, use the core image style for the form defaults. + if (empty($settings['image_style']) && strpos($form['#view_mode'], 'image__') === 0 && !empty($form['displays']['status']['file_image'])) { + $settings['image_style'] = substr($form['#view_mode'], strlen('image__')); + $form['displays']['status']['file_image']['#default_value'] = TRUE; + } + $element = array(); $element['image_style'] = array( '#title' => t('Image style'), @@ -366,6 +382,19 @@ function file_entity_file_formatter_file_image_settings($form, &$form_state, $se } /** + * Implements hook_file_displays_alter(). + * + * If this view mode is based off of a core image style but the display has not + * been configured, default to using the core image style. + */ +function file_entity_file_displays_alter(&$displays, $file, $view_mode) { + if (empty($displays['file_image']['settings']['image_style']) && strpos($view_mode, 'image__') === 0) { + $image_style = substr($view_mode, strlen('image__')); + $displays['file_image']['settings']['image_style'] = $image_style; + } +} + +/** * Menu access callback for the 'view mode file display settings' pages. * * Based on _field_ui_view_mode_menu_access(), but the Field UI module might not