Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
image-fields in nodes that are being rendered on the frontend as rendered entity contain empty alt-attribute on the img-tags. If I configure the settings to show the ALT tags as plain text they are being shown. Just not as alt-attrubute on images.
Is this a known bug in D8? And is there maybe a known solution?
Proposed resolution
Update \Drupal\file_entity\Plugin\Field\FieldFormatter\FileImageFormatter to have configuration variable for this. They would say which field on the file entity should be used as a source for alt and title. Use auto-created alt and title fields as a default. Make sure it is tested.
Comment | File | Size | Author |
---|---|---|---|
#30 | interdiff-2697817-30.txt | 3.37 KB | thenchev |
#30 | alt_text_and_title_text-2697817-30.patch | 10.19 KB | thenchev |
#28 | alt_text_and_title_text-2697817-28.patch | 10.28 KB | thenchev |
#28 | interdiff-2697817-28.txt | 1.26 KB | thenchev |
#27 | alt_text_and_title_text-2697817-27.patch | 10.17 KB | thenchev |
Comments
Comment #2
garbo CreditAttribution: garbo commentedComment #3
tusharbodke CreditAttribution: tusharbodke as a volunteer and at Faichi Solutions Pvt Ltd commentedComment #4
nicholas.alipaz CreditAttribution: nicholas.alipaz at L.A. BioMed commentedI too would like to see this fixed. It seems there might be some complexity UI-wise? Should the site administrator be able to choose which field to use as the alt, title, description, etc? What is the plan for this?
Comment #5
BerdirThere already token based configuration that makes the title/alt fields available when using the normal image formatter.
We need to find a way to inject that back into the special image formatter that we use to display the image.
Comment #6
slashrsm CreditAttribution: slashrsm at MD Systems GmbH commentedCould we add two configuration variables to the formatter that would define which fields to use as a source for this information (and default to auto-created alt and title fields)?
Another related problem is if image field is being used. Content creators can add alt and title as part of it, which will be completely ignored if image is displayed as a rendered entity. This will be even trickier to resolve. Out of this issue's scope I think.
Comment #7
BerdirSee file_entity_file_load(), we used to have settings for this, we probably just need to make those configurable again on a global settings page and make sure they are also applied in \Drupal\file_entity\Plugin\Field\FieldFormatter\FileImageFormatter somehow.
Comment #8
slashrsm CreditAttribution: slashrsm at MD Systems GmbH commentedPseudo-fields that are added in
file_entity_file_load()
doesn't seem to be used at all. I think that making this configurable on the formatter level is the simplest and most self-documenting.Maybe we should even consider removing code from
file_entity_file_load()
?Comment #9
BerdirWe still need the part that loads metadata I think, although we could also consider to change that to do lazy-loading as we have methods for it now. But then we'd need to change how we store it as well.
The alt/title stuff can possibly go away yes. But as I wrote, formatter level isn't that easy, actually, because it is not our formatter. We'd have to replace the core image formatter or provide our own and then people have to use that one.
The one that we have is only for displaying ourself, not for image fields. There it is indeed pretty easy.
Comment #10
slashrsm CreditAttribution: slashrsm at MD Systems GmbH for Acquia commentedHandling metadata through methods would be nice. Separate issue?
It seems that we have few possible situations:
It seems that we could make cases 2. and 3. work by fixing our image formatter. 1. is a bit more tricky as we're dealing with the core formatter. I am inclined to override it to prevent confusion with many similar formatters. But we have another problem there, which is a big UX mess in my opinion. How do we sync alt/title between image field and file entity? Do we try to sync? What happens if data was changed on both sides after sync? Should we override the field widget too and pass alt/title from the field to the entity? I am not sure....
I'd propose that we fix 2. and 3. as part of this issue and open separate issue(s) for the other problem.
Comment #11
thenchev CreditAttribution: thenchev at MD Systems GmbH for Acquia commentedHere the initial patch ready for review.
Comment #14
thenchev CreditAttribution: thenchev at MD Systems GmbH for Acquia commentedThis should fix the tests.
Comment #15
CTaPByK CreditAttribution: CTaPByK at MD Systems GmbH commentedLooks ok, just few small notices:
This is ok, but check if we can just extend this default image settings with new fields?
We can cover this with some simple test too.
Comment #16
hefterbrumi CreditAttribution: hefterbrumi commentedIs this something to do with the problem discovered here:
Image Alt and Title displayed in the wrong language IF field format is not "rendered file"
Comment #17
thenchev CreditAttribution: thenchev at MD Systems GmbH for Acquia commentedAddressed feedback from #15
@hefterbrumi i don't think this is related but i will check.
Comment #20
thenchev CreditAttribution: thenchev at MD Systems GmbH for Acquia commentedTest should be green now.
Comment #21
CTaPByK CreditAttribution: CTaPByK at MD Systems GmbH commentedLooks good to me.
Comment #22
slashrsm CreditAttribution: slashrsm at MD Systems GmbH for Acquia commentedThis is not the actual text. Maybe rename variable names to reflect the fact that this is a field.
Let's inject this.
Comment #23
BerdirWondering if we should use token for this instead of hardcoding that it is a field. Since that actually requires the token contrib module, maybe not.
However, if not, we should use ->hasField() instead of isset()
Ah, you actually show a list of options. Fine, that's easier to use. Might want to set a propert empty option, so it says "No alt text" or something like that.
Also, I agree with naming the fields then title/alt_field, not .. text.
Comment #24
BerdirAnd on 1., we should alos check if we actually have a value before hasField(). no value should be a valid option.
Comment #25
thenchev CreditAttribution: thenchev at MD Systems GmbH for Acquia commentedAddressed the feedback also expanded the tests a bit.
Comment #26
slashrsm CreditAttribution: slashrsm at MD Systems GmbH for Acquia commentedImage style storage is missing in @params.
This reads strange. Maybe replace "value" with "attribute" or "text"?
You can combine both conditions into one.
You could use #empty_option.
Alt/Title attribute.
Comment #27
thenchev CreditAttribution: thenchev at MD Systems GmbH for Acquia commentedShould cover the above. Made some changes to element name to combine better for 3.
Comment #28
thenchev CreditAttribution: thenchev at MD Systems GmbH for Acquia commentedUsed empty option and empty value in element.
Comment #29
slashrsm CreditAttribution: slashrsm at MD Systems GmbH for Acquia commentedExtra newline.
Conditions can be merged.
Comment #30
thenchev CreditAttribution: thenchev at MD Systems GmbH for Acquia commentedfixed above. also a few other stuff.
Comment #31
slashrsm CreditAttribution: slashrsm at MD Systems GmbH for Acquia commentedCommitted.
Comment #33
slashrsm CreditAttribution: slashrsm at MD Systems GmbH for Acquia commentedFollow-up: #2735111: Decide how to handle alt/title on images when core formatter is used
Comment #35
joseph.olstadFYI: the Follow-up issue has a D7 core fix that needs porting to D8 core.
#2735111: Decide how to handle alt/title on images when core formatter is used