diff --git a/file_entity.module b/file_entity.module index 77ec6ea..c1026bd 100644 --- a/file_entity.module +++ b/file_entity.module @@ -607,7 +607,8 @@ function file_entity_file_formatter_info() { 'default settings' => array( 'image_style' => '', 'alt' => '', - 'title' => '' + 'title' => '', + 'enforce_attribute_values' => FALSE, ), 'view callback' => 'file_entity_file_formatter_file_image_view', 'settings callback' => 'file_entity_file_formatter_file_image_settings', @@ -728,6 +729,28 @@ function file_entity_file_formatter_file_image_view($file, $display, $langcode) 'clear' => 1, 'sanitize' => 0, ); + + // Set the default attibutes title. + $attributes_title = $file->filename; + + // Set alt and title. + $alt_text = token_replace($display['settings']['alt'], array('file' => $file), $replace_options); + $title_text = token_replace($display['settings']['title'], array('file' => $file), $replace_options); + + // Make sure alt and title have at least some value. + // If token_replace() returns empty, 2 things can be the case: + // - 1: Token not installed. + // - 2: Submitted fields are empty. + // We fix these situations here by checking if we have to enforce. + // If we enforce and 1+2 return empty we render the filename as value. + if (empty($alt_text) && $display['settings']['enforce_attribute_values']) { + $alt_text = $attributes_title; + } + if (empty($title_text) && $display['settings']['enforce_attribute_values']) { + $attributes_title = explode('.', $attributes_title); + $title_text = $attributes_title[0]; + } + if (!empty($display['settings']['image_style'])) { $element = array( '#theme' => 'image_style', @@ -735,8 +758,8 @@ function file_entity_file_formatter_file_image_view($file, $display, $langcode) '#path' => $file->uri, '#width' => $file->image_dimensions['width'], '#height' => $file->image_dimensions['height'], - '#alt' => token_replace($display['settings']['alt'], array('file' => $file), $replace_options), - '#title' => token_replace($display['settings']['title'], array('file' => $file), $replace_options), + '#alt' => $alt_text, + '#title' => $title_text, ); } else { @@ -745,10 +768,18 @@ function file_entity_file_formatter_file_image_view($file, $display, $langcode) '#path' => $file->uri, '#width' => $file->image_dimensions['width'], '#height' => $file->image_dimensions['height'], - '#alt' => token_replace($display['settings']['alt'], array('file' => $file), $replace_options), - '#title' => token_replace($display['settings']['title'], array('file' => $file), $replace_options), + '#alt' => $alt_text, + '#title' => $title_text, ); } + + // Consistency NOTE: + // Core sets the title field ONLY if it's not empty. + // Let's do what core does, do not render an empty title. + if (empty($title_text)) { + unset($element['#title']); + } + return $element; } } @@ -799,6 +830,15 @@ function file_entity_file_formatter_file_image_settings($form, &$form_state, $se '#default_value' => $settings['title'], ); + // Enforce the rendering of a value for alt + title, + // especially handy when the saved values are empty. + $element['enforce_attribute_values'] = array( + '#title' => t('Enforce non-empty attributes'), + '#description' => t('Use the file->filename when token is not installed, or in the case the submitted fields are empty. This prevents empty alt and title attributes.'), + '#type' => 'checkbox', + '#default_value' => $settings['enforce_attribute_values'], + ); + if (module_exists('token')) { $element['alt']['#description'] .= t('This field supports tokens.'); $element['title']['#description'] .= t('This field supports tokens.');