A patch was recently developed that will add the alt and title attributes to the image fields using the media file selector: http://drupal.org/node/1307054

The patch is supposed to work on a fresh installation but it it does not provide a migration path to convert the already existing image fields that have data in them.

Opening this issue in order to develop a patch/upgrade/migration path/anything that will add the alt and title attributes to the already existing and populated image fields.

Comments

kirilius’s picture

Moving this up as the problem is still there and the fix itself does not accommodate any existing fields.

MXT’s picture

I need this feature too.

But I don't know if this is the correct issue queue to use, because Media migration is currently handled in migrate_extras module.

The absurd is that migrate_extras is currently unsupported due to the reason explained in the migrate_extras project page: in short, all migration stuff should be managed directly by involved modules.

So I think is time for Media module to manage by itself all migration stuff, but in the same time all the media migration management should be removed from migrate_extras to avoid conflicts...

I don't know, it would be useful the maintainers opinions...

Thank you very much for considering this

MXT’s picture

I've tried to set title and alt values in my prepare or complete functions, but without success.

No values are saved:

  public function prepare(stdClass $node, stdClass $row) {
    dpm($node);
    $node->field_media['und'][0]['field_file_image_alt_text']['und'][0]['value'] = 'My ALT text';
    $node->field_media['und'][0]['field_file_image_alt_text']['und'][0]['format'] = NULL;
    $node->field_media['und'][0]['field_file_image_alt_text']['und'][0]['safe_value'] = 'My ALT text';
    dpm($node);
  }

What is strange, is that the last dpm($node) shows the values setted correctly, but at the end values are not registered.

Same thing with complete function.

Where is the error?

Thank you for any help

fietserwin’s picture

I wrote some SQL queries myself. This could be used by others, assuming your situation is more or less the same as mine. so use at your own risk but do post your results/findings/improvements here.

Data to migrate

The data can be found in the data tables for all image fields. Revisioning and deletions are ignored here.

Source

table name

field_data_field_{name} (thus 1 table per image field)

fields

  • entity_type
  • bundle
  • deleted
  • entity_id
  • revision_id
  • language
  • delta
  • field_{name}_fid
  • field_{name}_alt
  • field_{name}_title
  • width
  • height

Destination

table name

field_data_field_file_image_alt_text

Fields (and the source of their value)

  • entity_type: 'file' (this field belongs to the file entity, not the image field).
  • bundle: 'image' (we are only considering images here, no video or audio).
  • deleted: 0 (or: field_data_field_{name}.deleted, we have to filter on non-deleted fields first anyway).
  • entity_id: field_data_field_{name}.field_{name}_fid
  • revision_id: field_data_field_{name}.field_{name}_fid (managed files were not revisioned before).
  • language: field_data_field_{name}.language
  • delta: 0 (cardinality for the alt text field within an image type file entity is always 1).
  • field_file_image_alt_text: field_data_field_{name}.field_{name}_alt
  • field_file_image_alt_format: NULL (alt and title texts in image fields are not formatted. Moreover, in html these attributes may only contain plain text anyway.)

table name

field_data_field_file_image_title_text

Fields (and the source of their value)

  • other fields: see above..
  • field_file_image_title_text: field_data_field_{name}.field_{name}_title
  • field_file_image_title_format: NULL (see above).

table name

field_revision_field_file_image_alt_text
field_revision_field_file_image_title_text

Exact copies of the field_data_field_... versions.

Problems to expect

  • Duplicate keys if multiple field entries refer to the same managed file. If the alt/title texts are the same we "only" have a technical problem with the SQL query. But if images are used in different contexts it would be very logical if they have different alt/title texts, as these texts are context dependent (and not only language wise, i.e. when used in translated content.
  • Inserting already existing keys if there is already an alt/title text for a given file entity. Note that if that alt/title text is entered via the UI, the alt/title text of the field is set to null, therefore adding an additional where clause may alleviate this problem.

Queries

The above observations result in 4 queries per image field:

INSERT INTO
 field_data_field_file_image_alt_text
SELECT
 'file' as entity_type,
 'image' as bundle,
 0 as deleted,
 field_{name}_fid as entity_id,
 field_{name}_fid as revision_id,
 language,
 0 as delta,
 field_{name}_alt as field_file_image_alt_text,
 null as field_file_image_alt_format
FROM
 field_data_field_{name}
WHERE
 deleted = 0
 and (field_{name}_alt is not null and field_{name}_alt <> '')
INSERT INTO
 field_revision_field_file_image_alt_text
SELECT
 'file' as entity_type,
 'image' as bundle,
 0 as deleted,
 field_{name}_fid as entity_id,
 field_{name}_fid as revision_id,
 language,
 0 as delta,
 field_{name}_alt as field_file_image_alt_text,
 null as field_file_image_alt_format
FROM
 field_data_field_{name}
WHERE
 deleted = 0
 and (field_{name}_alt is not null and field_{name}_alt <> '')
INSERT INTO
 field_data_field_file_image_title_text
SELECT
 'file' as entity_type,
 'image' as bundle,
 0 as deleted,
 field_{name}_fid as entity_id,
 field_{name}_fid as revision_id,
 language,
 0 as delta,
 field_{name}_title as field_file_image_title_text,
 null as field_file_image_title_format
FROM
 field_data_field_{name}
WHERE
 deleted = 0
 and (field_{name}_title is not null and field_{name}_title <> '')
INSERT INTO
 field_revision_field_file_image_title_text
SELECT
 'file' as entity_type,
 'image' as bundle,
 0 as deleted,
 field_{name}_fid as entity_id,
 field_{name}_fid as revision_id,
 language,
 0 as delta,
 field_{name}_title as field_file_image_title_text,
 null as field_file_image_title_format
FROM
 field_data_field_{name}
WHERE
 deleted = 0
 and (field_{name}_title is not null and field_{name}_title <> '')

Conclusions

I tested the queries locally and everything went well. However, changing this into an update hook or migrate function or module is not straightforward. The problems mentioned should be tackled because eventually there will be a site that does have multiple usages of the same image with different alt/title texts. Also the way that revisioning and deletions are handled should be decided on before creating a function/feature/module for general use.

Media module (maintainers) should ask themselves if this is the correct level to store alt and title text info or that it should be possible to define/override it at the usage level.

MJD’s picture

Issue summary: View changes

I've just had the same problem as #3 above https://www.drupal.org/node/1905764#comment-7378688
I think that should have worked but in the end I did a work around as the image alt / title texts never survived my node_save.

        $fid = $file_temp->fid;// fid from file_save_data... called as part of setting up my $node object before node_save

        $mediafiles = entity_load('file', array($fid));

        $mediafiles[$fid]->alt   = 'my alt text'; 
        $mediafiles[$fid]->title = 'my title text';

        $mediafiles[$fid]->field_file_image_alt_text['und'][0]['value']   = 'my alt text; 
        $mediafiles[$fid]->field_file_image_title_text['und'][0]['value'] = 'my title text';  
             
        entity_save('file', $mediafiles[$fid]);

Thanks to this post https://www.drupal.org/node/1438668#comment-5603114 that gave me the start I needed

mgifford’s picture

This other issue is fixed now - #1307054: Accessibility - Media browser image alt and title fields

I don't know if there is "anything that will add the alt and title attributes to the already existing and populated image fields."

Chris Matthews’s picture

Status: Active » Closed (outdated)
Issue tags: -Accessibility

Closing this issue as outdated. However, if you think this issue is still important, please let us know and we will gladly re-open it for review.
sincerely,
- the Drupal Media Team