There have been some issues with the TITLE and ALT attributes missing which have been fixed with a patch to the core file module.

See https://drupal.org/node/2065507#comment-7760101

I believe there are still some inconsistencies with the TITLE and ALT tags. I'm putting this as a bug as I consider it one, but this could be down to my understanding of how it all fits together.

The problem (as I see it).

The Title and ALT tags are not syncing properly between the conventional image field and the newer file entity.

If I have a content type field set to the conventional image field and update the ALT and TITLE attributes and save the node, this is not reflected when I go to content -> files and edit that entity.

The same if I edit the entity via content -> files and update the attributes there and save. This is not reflected when I go to the node edit form.

Therefore it seems to be keeping two copies of the ALT and TITLE attributes rather than them being synced. This doesn't seem right as if I delete the image via content -> files the image is no longer present when I edit the node, which I would expect to happen as it's referring to the same image.

Also to note,

If I edit the ALT and TITLE attributes save the node, then go to content type and change the field to media file selector and edit the node again, the changes cannot be seen in the new image view.

However, the reverse it does synch. If I edit the ALT and TITLE tags, save the node, go back to content type and change it back to the conventional image type and view the node once again the ALT and TITLE tags reflect the changes I did.

Forgive any incorrect use of the terminologies, however, while the two methods of creating an image field still exist, I would expect them to be sync'd.

Comments

devin carlson’s picture

Title: Title and Alt attributes not being kept in synch between file entity and standard image field » Sync alt and title text between file entity and standard image field
Category: bug » feature

Take a look at #2029985: Store core image field alt and title text using the File Metadata API which implemented the solution that you're suggesting.

OriginalSauce’s picture

StatusFileSize
new31.84 KB

Hi Devin,

Thanks for quick reply. I did come across this post when I was looking for solution to the missing alt and title fields. I have both the latest dev releases for file entity and media.

Just looking at the patch in #9. Is it ok to patch this in with the latest dev release?

At the time I didn't think this applied to my issues as I was not having a duplicate formatter problem.

See attached image. Will this patch make the title and alt tag appear in the display settings, which I don't currently see?

Thanks for your patience.

OriginalSauce’s picture

Whether it does or not, I've decided to not implement any patches that aren't committed. Doesn't make sense to apply a patch on a module with regular updates.

Not that this is a bad thing of course.

mindgame’s picture

As long as we have duplicate alt and title text data they should at least be synchronized.
Why only doing that for the image dimensions?
See _file_entity_update_image_field_dimensions():

$entity->{$image_field}[$langcode][$delta]['width'] = $file->metadata['width'];
$entity->{$image_field}[$langcode][$delta]['height'] = $file->metadata['height'];

We could just add the image captions to make the set of four image attributes complete:

$entity->{$image_field}[$langcode][$delta]['alt'] = $file->alt;
$entity->{$image_field}[$langcode][$delta]['title'] = $file->title;

where

$file->alt = isset($file->field_file_image_alt_text[LANGUAGE_NONE][0]['value']) ? $file->field_file_image_alt_text[LANGUAGE_NONE][0]['value'] : '';
$file->title = isset($file->field_file_image_title_text[LANGUAGE_NONE][0]['value']) ? $file->field_file_image_title_text[LANGUAGE_NONE][0]['value'] : '';

and replacing the weird condition

if ($file->metadata['width'] != $file->metadata['width'] || $file->metadata['height'] != $file->metadata['height']) {

by one that is more likely to be satisfied.

Now, when updating a file entity the image captions of the image fields will be updated as well.
This solution differs from Devin's proposal above to use the File Metadata API for alt and title text.

mindgame’s picture

Now the opposite direction.
Updating the file entity after changing the captions of image fields, for example when saving a node.

/**
 * Implements hook_field_attach_update().
 */
function file_entity_field_attach_update($entity_type, $entity) {
  if ($entity_type == 'file') {
    // See file_entity_file_update()
    return;
  }
  list(, , $bundle) = entity_extract_ids($entity_type, $entity);
  // Examine every image field instance attached to this entity's bundle.
  $instances = array_intersect_key(field_info_instances($entity_type, $bundle), _file_entity_get_fields_by_type('image'));
  foreach ($instances as $field_name => $instance) {
    if (!empty($entity->{$field_name})) {
      foreach ($entity->{$field_name} as $langcode => $items) {
        foreach ($items as $delta => $item) {
          // Save alt and title text from the image field to the file entity.
          $file = file_load($item['fid']);
          foreach (array('alt', 'title') as $key) {
            $field_key = 'field_file_image_' . $key . '_text';
            if (isset($file->{$field_key})) $file->{$field_key}[LANGUAGE_NONE][0]['value'] = $item[$key];
          }
          field_attach_update('file', $file);
        }
      }
    }
  }
}

Inspired by file_entity_field_attach_load().

spadxiii’s picture

If you allow empty alt/title tags, this is still giving a problem. Empty alt/title will be saved as NULL in the core field-tables. Because of the a core patch (which reverses arguments in array_merge: https://drupal.org/node/2066275#comment-7759503) this results in the file entity having alt/title of NULL. This in turn removes them from the output in theme_image (isset gives a FALSE for NULL values)

In file_entity_field_attach_load() the alt/title tags are loaded from the file-entity but they're not overwritten if you leave the fields empty: if (empty($item[$key]) && !empty($file->{$key})) { .. }

Overwriting these fields in case the field has NULL-values works by changing that if statement to: if (is_null($item[$key]) || !empty($file->{$key})) { .. }

askibinski’s picture

Well, I used 2 hooks to sync alt/title tags between core image field and file entities. This seems to work nicely as long as alt/title tags are required.

/**
 * hook_field_widget_form_alter
 */
function mymodule_field_widget_form_alter(&$element, &$form_state, $context) {
  // This hook is used to prepopulate the alt and title field in any image field widget,
  // with the values which are set in the file_entity fields, 
  // but ONLY if there is a alt/title set in the original file entity,
  // and ONLY if the values are empty.
  if ($context['field']['type'] == 'image') {
    foreach (element_children($element) as $delta) {
      $fid = $element[$delta]['#default_value']['fid'];
      $file = entity_load('file', array($fid), $conditions = array(), $reset = FALSE);
      if ($file) {
        // not 100% sure whether this is the right langcode to use, when entity translations are enabled.
        $langcode = $context['langcode'];
        $alt_field = field_get_items('file', $file[$fid], 'field_file_image_alt_text', $langcode);
        $title_field = field_get_items('file', $file[$fid], 'field_file_image_title_text', $langcode);
        if (!empty($alt_field[0]['value']) && empty($element[$delta]['#default_value']['alt'])) {
          $element[$delta]['#default_value']['alt'] = $alt_field[0]['value'];
        }
        if (!empty($title_field[0]['value']) && empty($element[$delta]['#default_value']['title'])) {
          $element[$delta]['#default_value']['title'] = $title_field[0]['value'];
        }
      }
    }
  }
}

The second one is inspired by mindgame's comment above, but adapted so the file entity fields will only be overwritten when empty.

/**
 * Implements hook_field_attach_update().
 */
function mymodule_field_attach_update($entity_type, $entity) {
  // The other way around: you upload a new image on a node and enter alt/title tags.
  // Save alt and title text from the image field to the file entity,
  // but ONLY if the file entity's alt/title fields are empty.
  if ($entity_type == 'node') {  
  list(, , $bundle) = entity_extract_ids($entity_type, $entity);
  // Examine every image field instance attached to this entity's bundle.
  $instances = array_intersect_key(field_info_instances($entity_type, $bundle), _file_entity_get_fields_by_type('image'));
    foreach ($instances as $field_name => $instance) {
      if (!empty($entity->{$field_name})) {
        foreach ($entity->{$field_name} as $langcode => $items) {
          foreach ($items as $delta => $item) {
            $file = file_load($item['fid']);
            if ($file) {
              if (isset($file->field_file_image_alt_text) && empty($file->field_file_image_alt_text[$langcode][0]['value'])) { 
                $file->field_file_image_alt_text[$langcode][0]['value'] = $item['alt'];
              }
              if (isset($file->field_file_image_title_text) && empty($file->field_file_image_title_text[$langcode][0]['value'])) { 
                $file->field_file_image_title_text[$langcode][0]['value'] = $item['title'];
              }
            }
            field_attach_update('file', $file);
          }
        }
      }
    }
  }
}

Also, the code should be language aware (alltough that needs more testing).

gmaximus’s picture

Thanks askibinski ... Your code worked like a charm, I thought it was just me doing something wrong.

yfma’s picture

I am having the same issue as SpadXIII described. The alt and title text were added to file entity itself with empty string (decorative image). When you are using the media widget on image field with this image, The field_image_alt and field_image_title columns are both NULL in field database tables. So the alt and title got removed from the image field output, the screen reader will always think that the image is present and will read the filename instead.

SpadXIII's code is working great. should we have a patch for it?

IckZ’s picture

Thanks so much for the code! Works like a charm!

rodrigoaguilera’s picture

I just tested the hooks in #7 and they work nicely. Now I can show the img title as displayed text in the view mode :)

I wonder why don't we try to get this into the module. Maybe making the sync always is not the desired outcome so we can alter the image widget config to have a checkbox that reads something like "have the title and alt fields in sync with the file entity fields" and perform the sync only when this option is checked.

The patch seems pretty simple.

What do you think?

mercepedraza’s picture

I also added the code in #7 to hook_field_attach_insert() to get the change when the node is created, otherwise I needed to edit and save the node after having created it to see the img title displaying.

nelslynn’s picture

Any help tweaking #7 to work with images in a product entity (Drupal Commerce)?

joegl’s picture

Status: Active » Needs review
StatusFileSize
new3.25 KB

I added both hooks from #7 to the bottom of the file_entity.module and confirmed they are working. I then pushed both hooks into a patch for the 7.x-2.x branch. I imagine these changes could be better implemented into the core of the module, instead of using hooks. However, I am unfamiliar with this module and wouldn't know where to start!

Mindgame is probably on to something with creating similar functionality to the _file_entity_update_image_field_dimensions() function in file_entity.file.inc. Also possibly putting code directly into the file_entity_field_update function might be ideal.

joseph.olstad’s picture

are you sure this is still needed with the newest release (7.x-2.6) ?
have a look at this recently fixed issue:
#2806081: translated Title and Alt text fix, use optionally with entitycache and entity_translation

joegl’s picture

I had looked at that issue and applied the patch with no effect previously. I will try it again soon, thanks!

joseph.olstad’s picture

Version: 7.x-2.x-dev » 7.x-3.x-dev
Status: Needs review » Reviewed & tested by the community

I had a quick look at the patch #2070183-14: Sync alt and title text between file entity and standard image field
looks ok, I think it should go into 7.x-3.x first.
RTBC for 7.x-3.x , but needs review for 7.x-2.x

  • joseph.olstad committed 294de7e on 7.x-3.x authored by joegl
    Issue #2070183 by joegl, OriginalSauce: Sync alt and title text between...
joseph.olstad’s picture

Status: Reviewed & tested by the community » Fixed

This is now in the 7.x-3.x branch
to try it, use 7.x-3.x-dev

joseph.olstad’s picture

Version: 7.x-3.x-dev » 7.x-2.x-dev
Status: Fixed » Needs review

needs review for 7.x-2.x

joseph.olstad’s picture

Assigned: Unassigned » joseph.olstad
joseph.olstad’s picture

This isn't working for my use case (image style applied to a file_entity in a view)
my working solution is a core patch:
#2835135-1: image formatter needs to handle alt/title from file entities on images for multi language support

joseph.olstad’s picture

Status: Needs review » Postponed

I believe this is completely fixed by the recent david_rothstein fix in a possibly related/duplicate issue.
#2665960: Image Alt and Title displayed in the wrong language IF field format is not "rendered file"

If confirmed as fixed, I will back off the commit that was previously made to 7.x-3.x