Hi there,

After upgrading to the latest version (7.x-2.11) I noticed that the alt and title text for my image field (not a "Media" field) is no longer displayed. After a bit of debugging I figured that the values are still available in table field_data_field_image but seem to be overridden by field_file_image_alt_text and field_file_image_title_text respectively. Needless to say that these values for my ordinary image field are empty and thus the displaying of alt and title texts are empty too.

This all happens in function file_entity_set_title_alt_properties() (file_entity.file.inc) which is being called by file_entity_set_title_alt_properties_on_file_fields() originating in file_entity_entity_load(). These 2 functions have been added in the release mentioned above.

As the logic to detect if it's a file/image/something field (in file_entity_set_title_alt_properties_on_file_fields()) is alike between media field and ordinary image field I tried to intercept at a deeper level. Here's the code which overrides the file's alt and title text:

  foreach ($files as $file) {
    // Load alt and title text from fields.
    if (!empty($alt)) {
      $output = token_replace($alt, array('file' => $file), $replace_options);

      // @todo Remove once https://www.drupal.org/node/1713164 is fixed.
      // There is currently no way to get the raw alt text returned from the
      // token so we revert the encoding done during tokenization.
      $file->alt = decode_entities($output);
    }
    if (!empty($title)) {
      $output = token_replace($title, array('file' => $file), $replace_options);

      // @todo Remove once https://www.drupal.org/node/1713164 is fixed.
      // There is currently no way to get the raw title text returned from the
      // token so we revert the encoding done during tokenization.
      $file->title = decode_entities($output);
    }
  }

By checking if $output is empty (or rather not) and only then overriding the original alt/title text it seems to do the trick. Like this:

    // Load alt and title text from fields.
    if (!empty($alt)) {
      $output = token_replace($alt, array('file' => $file), $replace_options);

      if (!empty($output)) {
        // @todo Remove once https://www.drupal.org/node/1713164 is fixed.
        // There is currently no way to get the raw alt text returned from the
        // token so we revert the encoding done during tokenization.
        $file->alt = decode_entities($output);
      }
    }
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

sitiveni created an issue. See original summary.

sitiveni’s picture

joseph.olstad’s picture

is this a new issue or an older one?

When did you first notice this? with which tagged release?

see this possibly related issue or the other possibly related issues that are linked to the following issue.
#2665960: Image Alt and Title displayed in the wrong language IF field format is not "rendered file"

jerry’s picture

I started seeing this problem as well as of 7.x-2.11 (I think) on an older site which uses ImageField Tokens. The patch in #2 appears to have resolved the problem here. (Thanks, sitiveni.)

steveoriol’s picture

The patch in #2 appears to have resolved the problem too.
Thank you sitiveni.

  • sitiveni authored fde81bc on 7.x-2.x
    Issue #2918049 by sitiveni: Image field alt and title text overridden by...

  • sitiveni authored fde81bc on 7.x-3.x
    Issue #2918049 by sitiveni: Image field alt and title text overridden by...
joseph.olstad’s picture

Status: Active » Fixed
joseph.olstad’s picture

reviewed patch, seems reasonable and fairly low risk.
thanks for the reviewers feedback as well.
fixed in 7.x-2.x dev and 7.x-3.x dev

bradallenfisher’s picture

The dev version works.

donaldwbabcock’s picture

Can confirm that this works.

Any way to get this rolled as 7.x-2.12?

joseph.olstad’s picture

donaldwbabcock’s picture

Joseph, "This" is the patch in #2 against 7.x-2.x, which was committed to dev on Oct 24, 2017. The issue in #2919383: ALT and TITLE tag conflict between Field Storage and File Entity Storage is separate and more complex. As best I can tell, the patch as committed gets us back to the behavior before it broke.

Ambient.Impact’s picture

Can also confirm that 7.x-2.x fixes this. Was worried for a bit that the data might have been lost. Thanks!

joseph.olstad’s picture

I'm holding off a release of this fix due to the observations in #2919383: ALT and TITLE tag conflict between Field Storage and File Entity Storage

If you want to help out, please follow up in:
#2919383: ALT and TITLE tag conflict between Field Storage and File Entity Storage

Yazzbe’s picture

I seem to have lost some ALT & TITLE tranlations on a multi lingual website. Probably overridden. The latest dev version didn't bring those back.

My website uses a standard image file field, with alt and title field enabled in the node edit form (not the media widget).

edit: reverted back to 7.x-2.8 on a live site to get the previous multilingual behaviour back.

fsignoret’s picture

Thank you for the fix on 7.x-2x!
The release of this fix should be considered as urgent: in case you have several images and you save one change in the form, data is lost...
Hopefully I could manually get my data from a database backup.

joseph.olstad’s picture

The reported issues here are confusing because I haven't noticed them.

However the commit looks pretty safe.

joseph.olstad’s picture

Ok, after several reports of success, and I myself tested it (I didn't notice a change before or after) I've tagged a new release . 7.x-2.12.

to me this commit has no effect either way. Maybe because my site is multilingual with the latest entity_translation and latest media module, not sure.

dat deaf drupaler’s picture

Confirmed this patch and updating to 7.x-2.12 solve this problem.. thank you @sitiveni for quick catch and posting patch! You saved my day!

@fsignoret: data was not lost as they were still in the database, it was refusing to save new entries when original img tags disappeared however upon updating to 2.12 all data came back, thank god.

fsignoret’s picture

@dat deaf dupaler: in my case, it did not refuse to save.

Yazzbe’s picture

I was not so lucky. Updating to 7.x-2.12 does not show the translated alt & title values form the file widget like in 7.x-2.8 on my site.
It shows the alt & titles value in 1 language across all pages.

joseph.olstad’s picture

@yazzybe, I have multilingual and all alt and title values are showing up in the right language on my system. I suggest possibly a cache clear
if that doesn't work, try running the 'hacked' module to see what modules might be altered (patched) or just plain hacked in your filesystem.

also, to diagnose your system, run the dmf module to see if you've got duplicate module files in your filesystem
https://www.drupal.org/project/dmf

Also check the status report, and make sure to run update.php (or drush updb) if you haven't already.

Also make sure that if you're using the entity_translation module that you have the latest release. the latest release of entity_translation is 7.x-1.0-beta7, this is what we're using and it's working nicely for us.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

joseph.olstad’s picture

Do you have the "token" module enabled? If "token" is not enabled the alt and title text will not appear. I think the solution here is to first upgrade file_entity to latest version and then enable the "Token" module. The alt and title text are now tokens in the latest version. The token module should probably be a required dependency. However most people already have this module enabled.

Yazzbe’s picture

Yes, token was enabled.
Entity Translation 7.x-1.0-beta7, cc all, updb, no hacked or patched modules.

still on file entity 2.8 though.

joseph.olstad’s picture

are you using the entity cache module? any other caching modules? are you using memcache or some other type of php caching ? what version of php?
I am on php 7.0.22
no memcache here.

joseph.olstad’s picture

A-ha , yes that explains it, you need to upgrade file_entity to version 2.12

David Rothstein (one of the Drupal 7 core maintainers) wrote a few fixes to the alt and title logic and it is included in 2.12

joseph.olstad’s picture

upgrading to file entity 2.12 should fix this.

Let me know. Thanks

sherkajon’s picture

Dears,

I need your help, couple of days ago I have upgraded into Drupal 7.58 and updated all modules to the latest versions.

Now i'm facing a problem with Image Field. It is not keeping ALT and TITLE values on node creation and updating.

File Entity module: Version: 7.x-2.18
Token module: Version: 7.x-1.7
Entity Tokens module: Version: 7.x-1.9

Appreciate your recommendations.

Thank you.

perpignan’s picture

+1 , same issue with File Entity module: Version: 7.x-2.18 no alt and title for the file field.

sherkajon’s picture

perpignan,

I fixed it this way on file_entity.file.inc line number 273 (function file_entity_set_title_alt_properties):

Instead of:

$file->title = file_entity_replace_title($file);
$file->alt = file_entity_replace_alt($file);

I put:

if(!empty($title)){
	$file->title = token_replace($title, array('file' => $file), $replace_options);
}
if(!empty($alt)){
	$file->alt = token_replace($alt, array('file' => $file), $replace_options);
}

and it worked for me.

web226’s picture

Thank you @sherkajon your solution fixed it for me with File Entity 7.x-2.17+1-dev. Is anyone able to roll this solution into a patch?

joseph.olstad’s picture

Status: Closed (fixed) » Postponed (maintainer needs more info)

The ones that have issues here, are you using mysql for a database driver and database system? or PostgreSQL or sqlsrv ?
or some other system?
what version of php
any other contrib modules not up to date?

Note file_entity 7.x-2.19 has been tagged and released with performance improvements over 2.17 and also some edge case bug fixes.

sherkajon’s picture

@joseph.olstad ,

All my dependent modules are up to date. I'm using MySQL (MariaDB) as a database server and PHP 5.6.35.

maki3000’s picture

+1 I have the same settings as sherkajon

perpignan’s picture

Thank you @sherkajon ! it works on 7.x-2.17
I updated File entity to 7.x-2.19, it's not fix on this version !!! When i apply the patch on 7.x-2.19, it works.
I hope, on next version ( 2.20 ) it will be integrated !

joseph.olstad’s picture

I'm looking into this. possible related issue as described in this issue summary.
perhaps affects those not using the entity module.
#1713164: field_tokens() isn't respecting sanitize option

joseph.olstad’s picture

everyone here, if you run this drush command, what is the result?

drush vget file_entity_title

if the result is: file_entity_title: '[file:field_file_image_title_text]'

then in version 7.x-2.19 file_entity.module the following code block gets processed:

1275   // If the defaults are not changed then inlining replacement is much faster
1276   // than dealing with the token system.
1277   if ($title === $title_default) {
1278     $title_items = field_get_items('file', $file, 'field_file_image_title_text');
1279     return $title_items ? $title_items[0]['value'] : '';
1280   }

otherwise:

1281   elseif (!empty($title)) {
1282     return token_replace($title, array('file' => $file), $replace_options);
1283   }

none of the suggestions above would apply to 7.x-2.19
I was unable to make any patch using the suggestions above. In fact the comments seem to illustrate perhaps accidental errors on the part of the authors writing incomplete or half baked comments.

joseph.olstad’s picture

if someone can shed some light here, provide guidance, that'd be appreciated. It seems there's an opportunity for improvement here because several reports here.

joseph.olstad’s picture

The changes were made to file_entity by David Rothstein and some other high profile Druaplistas, important changes were made to the handling of alt and title due to performance reasons and to perfect multilingual alt and title field accuracy.

I also worked on the related issues and had previously came up with a core patch, but that core patch should no longer be needed due to the fix by David Rothstein. It's better to fix things in contrib than core, only when absolutely necessary to patch core.

For those people still experiencing issues with this, it could be due to a number of factors varying on setup and configuration.

What core patches are people using?

anyone have make files that contain up-to-date versions and list of modules and patches and core version?

ABaier’s picture

We are experiencing the same issue. In our case the regular image titles (core image fields) are not printed anymore and the node edit form shows empty fields.
The previous values are still present in the database but get overridden, empty or with new input, if the node form is saved. This seems critical to me, because the form still shows empty fields if you load it again, but things were saved in the background.

All modules and core are up to date and we recently updated file_entity to 7.x.2.19 (before 7.x.2.17 and 7.x.2.18). Unfortunately I don't know which version caused this behavior.

Drupal core 7.58 (no patches, single language installation)
Memcache 3.0.9-dev
PHP 7.0.27

Arm77’s picture

Downgrade to 7.x-2.17 fixed the problem. I use ImageField Tokens 7.x-1.x-dev. with the token [node:title] for both Alt and Title field. in File Entity configuration 7.x-2.17 I leave Alt & Title attribute as Blanks and everything else unchecked. everything works fine until I upgraded to 7.x-2.19. (I guess the problem starts in 7.x-2.18).

joseph.olstad’s picture

ok, use 7.x-2.17 if this affects you, hopefully figure out the regression quickly and fix it.

joseph.olstad’s picture

Status: Postponed (maintainer needs more info) » Needs work
joseph.olstad’s picture

Status: Needs work » Fixed

ok, 7.x-2.20 was released
I backed off 2642764
let us know if there's still an issue with 7.x-2.20
based on reports, it should work like 7.x-2.17 does
knock on wood

Kniekel’s picture

Thank you! I installed the new version 7.x.2.20 and everything seems to work ok.

joseph.olstad’s picture

thanks for reporting

Arm77’s picture

Thanks, 7.x.2.20 works.

perpignan’s picture

Thank you ! 7.x-2.20 works for me too :-)

sherkajon’s picture

Thank you! 7.x-2.20 also works well for me!
Good job, Joseph.

joseph.olstad’s picture

Thanks to Arm77 for reporting his findings, led to a quick solution! cheers

mtoscano’s picture

Fixed, thanks.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

IckZ’s picture

Hey,
there seems to be still a bug in the module. If the entity has an alt and a title tag it does not update the title or alt tag if I edit it by node/edit.. I can only edit the tags if i go directly to /file/../edit/

If I disbale the line //$file->alt = decode_entities($output); from the patch the alt tag gets updated. But this cant be the right solution. What do you mean?

l-laziz’s picture

Priority: Normal » Major

I second IckZ's #55 comment. The issue still persists. Can't save alt text/title via node/edit. Can save it though via file/edit.

joseph.olstad’s picture