From http://drupal.org/node/1553094#comment-6746236, now we can have alt/title in 'Rendered File' display format (Thanks a lot :)). But when it is changed to other format like 'Image', 'ShadowBox', there will be empty alt tag and no title. I think it is also essential to have this feature. It would be great to have it working for all display format.
I tried the code from http://drupal.org/node/1553094#comment-6704686 without success.

Thank you.

Comments

ParisLiakos’s picture

Version: 7.x-2.0-unstable7 » 7.x-2.x-dev
Issue tags: -display format setting, -alt attribute, -title attribute
peximo’s picture

Hi, I use this hook to show the alt and title also in colorbox etc.

/**
 * Implements hook_field_attach_presave().
 */
function YOUR_MODULE_field_attach_presave($entity_type, $entity) {
  if ($entity_type == 'node') {
    foreach (field_info_fields() as $field_name => $field) {
      if ($field['type'] == 'image' && isset($entity->{$field_name})) {
        $langcode = field_is_translatable($entity_type, $field) ? entity_language($entity_type, $entity) : LANGUAGE_NONE;
        if (!empty($entity->{$field_name}[$langcode])) {
          foreach ($entity->{$field_name}[$langcode] as $delta => $item) {
            $file = file_load($entity->{$field_name}[$langcode][$delta]['fid']);
            $alt_items = field_get_items('file', $file, 'field_file_image_alt_text');
            $title_items = field_get_items('file', $file, 'field_file_image_title_text');
            if (!empty($alt_items[0]['value'])) {
              $entity->{$field_name}[$langcode][$delta]['alt'] = $alt_items[0]['value'];
            }
            if (!empty($title_items[0]['value'])) {
              $entity->{$field_name}[$langcode][$delta]['title'] = $title_items[0]['value'];
            }
          }
        }
      }
    }
  }
}

I don't know if this is the right approach but it works.

supradhan’s picture

peximo: Thank you. I will try this.

supradhan’s picture

peximo:
I used the code you provided in my theme template.php for shadowbox but it didn't work. Am I missing anything?

function shadowbox_field_attach_presave($entity_type, $entity) {
    if ($entity_type == 'node') {
        foreach (field_info_fields() as $field_name => $field) {
            if ($field['type'] == 'image' && isset($entity->{$field_name})) {
                $langcode = field_is_translatable($entity_type, $field) ? entity_language($entity_type, $entity) : LANGUAGE_NONE;
                if (!empty($entity->{$field_name}[$langcode])) {
                    foreach ($entity->{$field_name}[$langcode] as $delta => $item) {
                        $file = file_load($entity->{$field_name}[$langcode][$delta]['fid']);
                        $alt_items = field_get_items('file', $file, 'field_file_image_alt_text');
                        $title_items = field_get_items('file', $file, 'field_file_image_title_text');
                        if (!empty($alt_items[0]['value'])) {
                            $entity->{$field_name}[$langcode][$delta]['alt'] = $alt_items[0]['value'];
                        }
                        if (!empty($title_items[0]['value'])) {
                            $entity->{$field_name}[$langcode][$delta]['title'] = $title_items[0]['value'];
                        }
                    }
                }
            }
        }
    }
}
peximo’s picture

You must resave the content; also is the function correctly called? I use this code in a module, I don't know if you can do this in a theme.

HyperGlide’s picture

@peximo can i suggest you sandbox your module. Perhaps that can help with the initial issue of supporting other display modes.

vgutekunst’s picture

Hi,

i which file i have to add this code? I need the alt & title in colorbox too! Plz help me!

regards

peximo’s picture

@HyperGlide: You mean a module for this hook? If this is the right approach I think is better a patch.
@lolhonk: you should create a module (eg: media_misc) and implement this hook (eg: media_misc_field_attach_presave())

supradhan’s picture

@Peximo: Thank you a lot. Now it is working in the shadowbox and I managed to use theme_image_formatter for other displays.
Is it possible to use theme function for shadowbox case also so that I can move the hook function from shadowbox module to theme template?

Thanks

HyperGlide’s picture

@peximo my reference was only as a work around, not a long term solution. A patch is much better!

vgutekunst’s picture

The code doesnt work. I put them in a mymodulename.modul and activated this modul but the alt and title field appears only when i choose "rendered file"

supradhan’s picture

@lolhonk: You need to edit and re-save the content to make it work. Also make sure your hook function declaration is mymodulename_field_attach_presave($entity_type, $entity). If it still not working then try to debug and see if your code is being called or not.

moniuch’s picture

@peximo Thanks for the code, it definitely works. I have been trying other hooks so to workaround having to resave the content, but none of them worked. Have you tried them too? hook_field_prepare_view, hook_field_formatter_prepare_view, hook_field_load - the first being the most appropriate, bot actually none of them was triggered as a dummy dpm was not firing.

Devin Carlson’s picture

Status: Active » Closed (duplicate)

This will be fixed by #1496942: Support making any kind of meta-data available to file_load() and file_load_multiple() consumers.

Marking this as a duplicate since I don't think that there's any other "proper" way to do this in the interim.