diff --git a/file_entity.install b/file_entity.install
index 305b80b..014ad4a 100644
--- a/file_entity.install
+++ b/file_entity.install
@@ -151,6 +151,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();
}
/**
@@ -317,3 +320,225 @@ function file_entity_update_7200() {
);
db_create_table('image_dimensions', $schema['image_dimensions']);
}
+
+/**
+ * Add title and alt text to image file types.
+ */
+function file_entity_update_7201() {
+ _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',
+ 'module' => 'text',
+ 'settings' => array(),
+ 'type' => 'text_default',
+ 'weight' => 0,
+ ),
+ 'media_large' => array(
+ 'label' => 'above',
+ 'settings' => array(),
+ 'type' => 'hidden',
+ 'weight' => 0,
+ ),
+ 'media_link' => array(
+ 'label' => 'above',
+ 'settings' => array(),
+ 'type' => 'hidden',
+ 'weight' => 0,
+ ),
+ 'media_original' => array(
+ 'label' => 'above',
+ 'settings' => array(),
+ 'type' => 'hidden',
+ 'weight' => 0,
+ ),
+ 'media_preview' => array(
+ 'label' => 'above',
+ 'settings' => array(),
+ 'type' => 'hidden',
+ 'weight' => 0,
+ ),
+ 'media_small' => 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',
+ ),
+ );
+
+ // 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',
+ 'module' => 'text',
+ 'settings' => array(),
+ 'type' => 'text_default',
+ 'weight' => 1,
+ ),
+ 'media_large' => array(
+ 'label' => 'above',
+ 'settings' => array(),
+ 'type' => 'hidden',
+ 'weight' => 0,
+ ),
+ 'media_link' => array(
+ 'label' => 'above',
+ 'settings' => array(),
+ 'type' => 'hidden',
+ 'weight' => 0,
+ ),
+ 'media_original' => array(
+ 'label' => 'above',
+ 'settings' => array(),
+ 'type' => 'hidden',
+ 'weight' => 0,
+ ),
+ 'media_preview' => array(
+ 'label' => 'above',
+ 'settings' => array(),
+ 'type' => 'hidden',
+ 'weight' => 0,
+ ),
+ 'media_small' => 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',
+ ),
+ );
+
+ // 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
old mode 100644
new mode 100755
index 3b36595..f54bdc2
--- a/file_entity.module
+++ b/file_entity.module
@@ -467,9 +467,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',
);
@@ -580,6 +585,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 {
@@ -588,6 +597,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;
@@ -608,6 +621,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;
}