diff --git a/galleria.module b/galleria.module index 29f49f4..c82b6a0 100644 --- a/galleria.module +++ b/galleria.module @@ -289,9 +289,11 @@ function galleria_field_formatter_info() { return array( 'galleria' => array( 'label' => t('Galleria'), - 'field types' => array('image', 'media', 'node_reference'), + 'field types' => array('image', 'media', 'node_reference', 'file'), 'settings' => array( 'optionset' => 'default', + 'title_field' => '', + 'alt_field' => '', 'referenced_fields' => array(), ), ), @@ -324,6 +326,33 @@ function galleria_field_formatter_settings_form($field, $instance, $view_mode, $ '#default_value' => $settings['optionset'], ); + if ($field['type'] == 'file') { + // Provide alternate settings to allow selection of caption and alt tags. + $bundles = field_info_instances('file'); + // Determine if there are additional fields on the image instance. + if (isset($bundles['image']) && !empty($bundles['image'])) { + $options = array('' => t('None')); + foreach ($bundles['image'] as $field_name => $field_details) { + $options[$field_name] = $field_details['label']; + } + $form['alt_field'] = array( + '#title' => t('Alt field'), + '#description' => t('Select an optional field to draw alt tags from.'), + '#type' => 'select', + '#options' => $options, + '#default_value' => isset($settings['alt_field']) ? $settings['alt_field'] : '' + ); + $form['title_field'] = array( + '#description' => t('Select an optional field to draw images titles from.'), + '#title' => t('Title field'), + '#type' => 'select', + '#options' => $options, + '#default_value' => isset($settings['title_field']) ? $settings['title_field'] : '' + ); + } + + } + // Show select box for image fields if we're formatting a node_reference field if ($field['type'] == 'node_reference') { // Find all image fields @@ -383,6 +412,28 @@ function galleria_field_formatter_settings_summary($field, $instance, $view_mode } $summary[] = t('Option set: %optionset', array('%optionset' => $optionset)); + // Summary for file fields. + if ($field['type'] == 'file' && module_exists('file_entity')) { + // Prepare options list. + if (!empty($settings['alt_field']) || + !empty($settings['title_field'])) { + $bundles = field_info_instances('file'); + // Determine if there are additional fields on the image instance. + if (isset($bundles['image']) && !empty($bundles['image'])) { + $options = array('' => t('None')); + foreach ($bundles['image'] as $field_name => $field_details) { + $options[$field_name] = $field_details['label']; + } + } + } + if (!empty($settings['alt_field'])) { + $summary[] = t('Alt field: @title', array('@title' => $options[$settings['alt_field']])); + } + if (!empty($settings['title_field'])) { + $summary[] = t('Title field: @title', array('@title' => $options[$settings['title_field']])); + } + } + // For node_reference fields: Referenced image fields if ($field['type'] == 'node_reference') { $referenced_fields = array(); @@ -431,6 +482,9 @@ function galleria_field_formatter_view($entity_type, $entity, $field, $instance, if ($field['type'] == 'media') { $items = galleria_prepare_media_images($items); } + elseif ($field['type'] == 'file' && module_exists('file_entity')) { + $items = galleria_prepare_file_images($items, $display['settings'], $langcode); + } elseif ($field['type'] == 'node_reference') { // Cleanup referenced_fields array $referenced_fields = &$display['settings']['referenced_fields']; @@ -473,6 +527,13 @@ function galleria_field_formatter_view($entity_type, $entity, $field, $instance, return $element; } +/** + * Util function to parse media $items and return images in format expected by + * galleria formatter. + * + * @param array $items + * Array of field items. + */ function galleria_prepare_media_images($items) { $image_items = array(); foreach ($items as $item) { @@ -486,6 +547,56 @@ function galleria_prepare_media_images($items) { } /** + * Util function to parse file $items and return images in format expected by + * galleria formatter. + * + * @param array $items + * Array of field items. + * @param array $settings + * Array of settings + * @param string $langcode + * Language code of the field + */ +function galleria_prepare_file_images($items, $settings, $langcode) { + $image_fids = array(); + $alt_field = empty($settings['alt_field']) ? FALSE : $settings['alt_field']; + $title_field = empty($settings['title_field']) ? FALSE : $settings['title_field']; + // First pass, fetch the fids. We pass twice so we can minimize calls to + // entity_load which is expensive to call multiple times. + foreach ($items as $delta => $item) { + if ($item['type'] == 'image') { + $image_fids[] = $item['fid']; + // Set a sensible default. + $items[$delta]['alt'] = $items[$delta]['title'] = ''; + } + else { + // Galleria only handles images. + unset($items['key']); + } + } + $file_entities = entity_load('file', $image_fids); + // Second pass, set the alt/title tag. + foreach ($items as $delta => $item) { + if ($item['type'] == 'image') { + $file_entity = $file_entities[$item['fid']]; + if ($alt_field) { + $alt_field_items = field_get_items('file', $file_entity, $alt_field, $langcode); + if (!empty($alt_field_items[0]['safe_value'])) { + $items[$delta]['alt'] = $alt_field_items[0]['safe_value']; + } + } + if ($title_field) { + $title_field_items = field_get_items('file', $file_entity, $title_field, $langcode); + if (!empty($title_field_items[0]['safe_value'])) { + $items[$delta]['title'] = $title_field_items[0]['safe_value']; + } + } + } + } + return $items; +} + +/** * This function loads the required JavaScripts and settings for a Galleria instance. */ function galleria_add_js($id, $optionset) {