diff --git a/file_entity.install b/file_entity.install
index 5aca7d8..5c542ee 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 files'));
}
+
+ // Create the title and alt text fields
+ _file_entity_create_alt_title_fields();
}
/**
@@ -541,3 +544,239 @@ function file_entity_update_7203() {
->execute();
}
}
+
+
+/**
+ * Add title and alt text to image file types.
+ */
+function file_entity_update_7204() {
+ _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 8989699..2adb16b 100644
--- a/file_entity.module
+++ b/file_entity.module
@@ -562,10 +562,23 @@ function file_entity_file_formatter_info() {
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',
);
+ // Provide default token values.
+ if (module_exists('token')) {
+ $formatters['file_image']['default settings']['alt'] = '[file:field_file_image_alt_text]';
+ $formatters['file_image']['default settings']['title'] = '[file:field_file_image_title_text]';
+ }
+ elseif (module_exists('entity_token')) {
+ $formatters['file_image']['default settings']['alt'] = '[file:field-file-image-alt-text]';
+ $formatters['file_image']['default settings']['title'] = '[file:field-file-image-title-text]';
+ }
}
return $formatters;
@@ -666,6 +679,13 @@ function file_entity_file_formatter_file_image_view($file, $display, $langcode)
}
if (file_entity_file_is_local($file) && isset($file->image_dimensions)) {
+ // We don't sanitize here.
+ // @see http://drupal.org/node/1553094#comment-6257382
+ // Theme function will take care of escaping.
+ $replace_options = array(
+ 'clear' => 1,
+ 'sanitize' => 0,
+ );
if (!empty($display['settings']['image_style'])) {
$element = array(
'#theme' => 'image_style',
@@ -673,6 +693,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),
);
}
else {
@@ -681,6 +703,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),
);
}
return $element;
@@ -701,6 +725,33 @@ 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.'),
+ '#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.'),
+ '#type' => 'textfield',
+ '#default_value' => $settings['title'],
+ );
+
+ if (module_exists('token')) {
+ $element['alt']['#description'] .= t('This field supports tokens.');
+ $element['title']['#description'] .= t('This field supports tokens.');
+ $element['tokens'] = array(
+ '#theme' => 'token_tree',
+ '#token_types' => array('file'),
+ '#dialog' => TRUE,
+ );
+ }
+
return $element;
}