Problem/Motivation

We add fields that handle alt and title attributes on the image. Core, on the other hand, stores values for those attributes on an image field. This causes some inconsistencies. If an image is rendered through entity view display we use alt/title from the fields and ignore any values that might be available on the field. When using core's image formatter the problem is reversed; fields on the entity are ignored and values from the field are used.

It would be great to figure out how to make this consistent. How do we sync alt/title between image field and file entity? What happens if data was changed on both sides after sync? Should we override core's image field formatter and widget to pass responsibility for alt/title to the file entity? This is certainly possible, but becomes problematic if another module overrides the same plugin.

Before patching:

french without patching

After patching

french
french

english
english

Steps to reproduce

IF we change the format of the image field to 'rendered file' on the content display settings page everything works, alt tags and titles show up properly. However most sites use image styles, other formatters like flex slider, colorbox etc, therefore it is an important functionality.

This can be reproduced on sympletest.me and on our test environments as well.

We use the latest media module, file entity and entity translation modules.

  1. Core and Basic module installation on one language
  2. Enabled 1 more language
  3. File Entity configuration -> in TRANSLATABLE ENTITY TYPES image is configured with the following options:
    • Hide language selector
    • Exclude Language neutral from the available languages
    • Prevent language from being changed once the entity has been created
  4. Created custom content type for testing
    • Content type is translatable with entity translation
    • Added image field with media file selector widget not translatable (unlimited values)
    • Configured display settings of the image field format to 'image'.
  5. Uploaded single or multiple image files through content/files/add files or via media widget, does not make a difference
  6. After upload we translated Alt and title fields on multiple images through content/files/edit/translate
  7. Added images to the content's image field through media browser
  8. After saving and/or cache flushing, our alt and title tags stays on the language (regardless of the chosen language) that is active.

Proposed resolution

See patch for D7 , Needs port to D8.

Note: further testing showed that while this patch ALWAYS works, without this patch you might occationally notice the alt and title working correctly on some records but not others. Sorry I don't have more details

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

joseph.olstad created an issue. See original summary.

joseph.olstad’s picture

joseph.olstad’s picture

Status: Active » Needs review
joseph.olstad’s picture

With this fix for D7 (needs port to D8), now all image file entities in views display the alt and title text in the correct language, this is compatible with views that use image styles and most likely fixes other displays. No longer do we need to always use the 'Rendered file' option without losing alt and title by language.

joseph.olstad’s picture

Issue summary: View changes
joseph.olstad’s picture

Issue summary: View changes
joseph.olstad’s picture

***EDIT*** significant debugging did not prove hypothesis actually, the true source of this problem might not actually be in core for D7, however I have not yet proven this hypothesis.

Here is the unproven hypothesis; that this problem may be originating in the file_entity module in this function:
file_entity_file_load

/**
 * Implements hook_file_load().
 */
function file_entity_file_load($files) {
  // Add alt and title text to images.
  $alt = variable_get('file_entity_alt', '[file:field_file_image_alt_text]');
  $title = variable_get('file_entity_title', '[file:field_file_image_title_text]');


Or, perhaps even more likely, have a look at file_entity_field_attach_load in file_entity.module , it appears that the alt and title field get set but only if they're empty and if they do get set its not specific to the current language.

Meanwhile, until a better fix is found, the above patch #1 does the job.

joseph.olstad’s picture

Version: 7.x-dev » 8.3.x-dev

D8 is affected by this too, for more details, please see the related issues

joseph.olstad’s picture

Assigned: joseph.olstad » Unassigned
Status: Needs review » Needs work

I looked into comment #8 and did not yet find any better solution.

For now patch #1 is the best known working solution for D7. It needs porting to D8

joseph.olstad’s picture

Category: Feature request » Bug report
joseph.olstad’s picture

Issue summary: View changes
joseph.olstad’s picture

Version: 8.3.x-dev » 7.x-dev
FileSize
106.35 KB
111.96 KB

Hmm, for some reason some records display correctly without the patch, however other records do not display correct language.

Here is what we expect to see for all records, english title hover text with english content and french title hover text with french content:
english

french this time
french

joseph.olstad’s picture

However, without this patch, my environment displays incorrectly for most records (but not all) english hover text on the french record despite the content having the translated values looking ok.

I did some dpm() of the entity and the data , didn't see any obvious reason for the inconsistent results prior to the patch, however after patching all records display as we expect.

joseph.olstad’s picture

Issue summary: View changes
joseph.olstad’s picture

Issue summary: View changes
joseph.olstad’s picture

joseph.olstad’s picture

Issue summary: View changes
joseph.olstad’s picture

here's the issue that added alt and title support for images through file_entity. Could provide some insight.

joseph.olstad’s picture

Status: Needs work » Postponed (maintainer needs more info)

Postponing pending a review of the later comments in
#2062721: Add a white list of file fields that can be overwritten when the file is added in the wysiwyg

wondering if this is really a core issue, or if its more likely a contrib issue?

Meanwhile, I'm actually using the above patch without issues but I don't necessarily like it.

joseph.olstad’s picture

file_entity.module (original patches were from:
#1553094: Alt and Title support for Images
)

joseph.olstad’s picture

***EDIT***
Either this solution by @David_Rothstein:
#2473621: Alt and title text don't appear in the correct language when viewing a file in a language other than the current page language

OR this solution by @das-peter:
#2806081: translated Title and Alt text fix, use optionally with entitycache and entity_translation

is the way to go

***DID NOT RESOLVE THE ABOVE USE CASE AS DESCRIBED***
still need patch 1 from this issue.

joseph.olstad’s picture

Status: Closed (duplicate) » Needs review

Ok, I am re-openning this because currently this patch is still needed for the above use case.

I will open a corresponding issue in file_entity to try to co-ordinate a possible alternative fix that doesn't involve a core patch. However for now I need this to work so I'm going to put my core patch back in place until a better solution is found.

joseph.olstad’s picture

it is easy to get mixed up , with the above use case a cache clear will make things 'appear' to be fixed until you load the page in another language other than the language of the cache clear , at which point you will see the other language file alt /title content.

David_Rothstein’s picture

Status: Needs review » Postponed (maintainer needs more info)

With the patch in #2665960: Image Alt and Title displayed in the wrong language IF field format is not "rendered file" for File Entity, is there anything core would still need to do? I think this is mostly File Entity's problem to solve, and it can also solve it in a way that doesn't depend on theme_image_formatter() being used.

It is true that there is a difference between how core handles alt/title (as properties on the image field) and how File Entity handles them (as independent fields attached to a file entity) but I'm not sure if there's much we can do about that. Ultimately it's kind of up to the site builder which one to use.

joseph.olstad’s picture

Category: Bug report » Support request
Status: Postponed (maintainer needs more info) » Fixed

Fixed in file_entity thanks @David_Rothstein for your patch. A new release of file_entity is available with the fix/improvements see release 7.x-2.11 of file_entity.

Appreciate this. I have backed off the core patch, it is no longer necessary with the latest file_entity release.

Status: Fixed » Closed (fixed)

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