From 6370cb2fc67aefde68cf3dc1a0f816711b805fa9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tadej=20Ba=C5=A1a?= Date: Thu, 11 Oct 2012 20:53:31 +0200 Subject: [PATCH] Issue #1553094 by @mrfelton, @rootatwc, et al: Alt and Title support for images --- file_entity.install | 238 +++++++++++++++++++++++++++++++++++++++++++++++++++ file_entity.module | 41 ++++++++- 2 files changed, 278 insertions(+), 1 deletion(-) diff --git a/file_entity.install b/file_entity.install index d4114f8..8a76957 100644 --- a/file_entity.install +++ b/file_entity.install @@ -240,6 +240,9 @@ function file_entity_install() { foreach ($roles as $rid => $role) { user_role_grant_permissions($rid, array('view file')); } + + // Create the title and alt text fields + _file_entity_create_alt_title_fields(); } /** @@ -525,3 +528,238 @@ function file_entity_update_7202() { ->condition('type', '') ->execute(); } + +/** + * Add title and alt text to image file types. + */ +function file_entity_update_7203() { + _file_entity_create_alt_title_fields(); +} + +/** + * Function to create the title and alt text fields + * and instances. + */ +function _file_entity_create_alt_title_fields() { + // Create the alt text field and instance. + + // Define the alt text field. + $alt_text_field = array( + 'active' => '1', + 'cardinality' => '1', + 'deleted' => '0', + 'entity_types' => array(), + 'field_name' => 'field_file_image_alt_text', + 'foreign keys' => array( + 'format' => array( + 'columns' => array( + 'format' => 'format', + ), + 'table' => 'filter_format', + ), + ), + 'indexes' => array( + 'format' => array( + 0 => 'format', + ), + ), + 'module' => 'text', + 'settings' => array( + 'max_length' => '255', + ), + 'translatable' => '0', + 'type' => 'text', + ); + + // As long as the alt text field doesn't already exist create it. + if (!field_info_field($alt_text_field['field_name'])) { + field_create_field($alt_text_field); + } + + // Define the alt text instance. + $alt_text_instance = array( + 'bundle' => 'image', + 'default_value' => NULL, + 'deleted' => '0', + 'description' => '', + 'display' => array( + 'default' => array( + 'label' => 'above', + 'settings' => array(), + 'type' => 'hidden', + 'weight' => 0, + ), + 'full' => array( + 'label' => 'above', + 'settings' => array(), + 'type' => 'hidden', + 'weight' => 0, + ), + 'preview' => array( + 'label' => 'above', + 'settings' => array(), + 'type' => 'hidden', + 'weight' => 0, + ), + 'teaser' => array( + 'label' => 'above', + 'settings' => array(), + 'type' => 'hidden', + 'weight' => 0, + ), + ), + 'entity_type' => 'file', + 'field_name' => 'field_file_image_alt_text', + 'label' => 'Alt Text', + 'required' => 0, + 'settings' => array( + 'text_processing' => '0', + 'user_register_form' => FALSE, + ), + 'widget' => array( + 'active' => 1, + 'module' => 'text', + 'settings' => array( + 'size' => '60', + ), + 'type' => 'text_textfield', + 'weight' => '-4', + ), + ); + + // For sites that updated from Media 1.x, continue to provide these deprecated + // view modes. + // @see http://drupal.org/node/1051090 + if (variable_get('media__show_deprecated_view_modes')) { + $alt_text_instance['display'] += array( + 'media_link' => array( + 'label' => 'above', + 'settings' => array(), + 'type' => 'hidden', + 'weight' => 0, + ), + 'media_original' => array( + 'label' => 'above', + 'settings' => array(), + 'type' => 'hidden', + 'weight' => 0, + ), + ); + } + + // As long as the alt text instance doesn't already exist create it. + if (!field_info_instance($alt_text_instance['entity_type'], $alt_text_instance['field_name'], $alt_text_instance['bundle'])) { + field_create_instance($alt_text_instance); + } + + // Create the title text field and instance. + + // Define the title text field. + $title_text_field = array( + 'active' => '1', + 'cardinality' => '1', + 'deleted' => '0', + 'entity_types' => array(), + 'field_name' => 'field_file_image_title_text', + 'foreign keys' => array( + 'format' => array( + 'columns' => array( + 'format' => 'format', + ), + 'table' => 'filter_format', + ), + ), + 'indexes' => array( + 'format' => array( + 0 => 'format', + ), + ), + 'module' => 'text', + 'settings' => array( + 'max_length' => '255', + ), + 'translatable' => '0', + 'type' => 'text', + ); + + // As long as the title text field doesn't exist create it. + if (!field_info_field($title_text_field['field_name'])) { + field_create_field($title_text_field); + } + + // Define the title text instance. + $title_text_instance = array( + 'bundle' => 'image', + 'default_value' => NULL, + 'deleted' => '0', + 'description' => 'Title text attribute', + 'display' => array( + 'default' => array( + 'label' => 'above', + 'settings' => array(), + 'type' => 'hidden', + 'weight' => 1, + ), + 'full' => array( + 'label' => 'above', + 'settings' => array(), + 'type' => 'hidden', + 'weight' => 0, + ), + 'preview' => array( + 'label' => 'above', + 'settings' => array(), + 'type' => 'hidden', + 'weight' => 0, + ), + 'teaser' => array( + 'label' => 'above', + 'settings' => array(), + 'type' => 'hidden', + 'weight' => 0, + ), + ), + 'entity_type' => 'file', + 'field_name' => 'field_file_image_title_text', + 'label' => 'Title Text', + 'required' => 0, + 'settings' => array( + 'text_processing' => '0', + 'user_register_form' => FALSE, + ), + 'widget' => array( + 'active' => 1, + 'module' => 'text', + 'settings' => array( + 'size' => '60', + ), + 'type' => 'text_textfield', + 'weight' => '-3', + ), + ); + + // For sites that updated from Media 1.x, continue to provide these deprecated + // view modes. + // @see http://drupal.org/node/1051090 + if (variable_get('media__show_deprecated_view_modes')) { + $title_text_instance['display'] += array( + 'media_link' => array( + 'label' => 'above', + 'settings' => array(), + 'type' => 'hidden', + 'weight' => 0, + ), + 'media_original' => array( + 'label' => 'above', + 'settings' => array(), + 'type' => 'hidden', + 'weight' => 0, + ), + ); + } + + // As long as the title text instance doesn't already exist create it. + if (!field_info_instance($title_text_instance['entity_type'], $title_text_instance['field_name'], $title_text_instance['bundle'])) { + field_create_instance($title_text_instance); + } +} diff --git a/file_entity.module b/file_entity.module index 23a9338..21822bd 100644 --- a/file_entity.module +++ b/file_entity.module @@ -498,9 +498,14 @@ function file_entity_file_formatter_info() { // Add a simple file formatter for displaying an image in a chosen style. if (module_exists('image')) { + $formatters['file_image'] = array( 'label' => t('Image'), - 'default settings' => array('image_style' => ''), + 'default settings' => array( + 'image_style' => '', + 'alt' => '', + 'title' => '' + ), 'view callback' => 'file_entity_file_formatter_file_image_view', 'settings callback' => 'file_entity_file_formatter_file_image_settings', ); @@ -611,6 +616,10 @@ 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), array('clear' => 1, 'sanitize' => 0)), + '#title' => token_replace($display['settings']['title'], + array('file' => $file), array('clear' => 1, 'sanitize' => 0)), ); } else { @@ -619,6 +628,10 @@ 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), array('clear' => 1, 'sanitize' => 0)), + '#title' => token_replace($display['settings']['title'], + array('file' => $file), array('clear' => 1, 'sanitize' => 0)), ); } return $element; @@ -639,6 +652,32 @@ function file_entity_file_formatter_file_image_settings($form, &$form_state, $se '#default_value' => $settings['image_style'], '#empty_option' => t('None (original image)'), ); + + // For image files we allow the alt attribute (required in HTML). + $element['alt'] = array( + '#title' => t('Alt attribute'), + '#description' => t('The text to use as value for the img tag alt attribute. Can be left empty. Token replacements are available'), + '#type' => 'textfield', + '#default_value' => $settings['alt'], + ); + + // Allow the setting of the title attribute. + $element['title'] = array( + '#title' => t('Title attribute'), + '#description' => t('The text to use as value for the img tag title attribute. Can be left empty. Token replacements are available'), + '#type' => 'textfield', + '#default_value' => $settings['title'], + ); + + if (module_exists('token')) { + $element['tokens'] = array( + '#theme' => 'token_tree', + '#token_types' => array('file'), + '#global_types' => TRUE, + '#click_insert' => TRUE, + ); + } + return $element; } -- 1.7.10.4