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
When using a image field formatter and adding a link to file or hosting entity, URL options are none handled by native image formatter so impossible to add class or other attributes to the generated link...
file : image.field.inc
function : template_preprocess_image_formatter
template : image-formatter.html.twig
Proposed resolution
Use the link()
Twig method to render the linked image, so URL options are output as expected.
Remaining tasks
- Write a patch
- Review
- Commit
User interface changes
None.
API changes
Linked images can now render link attributes.
Data model changes
None.
Comment | File | Size | Author |
---|---|---|---|
#12 | 2829990-12.patch | 3.04 KB | Krzysztof Domański |
#12 | interdiff-10-12.txt | 901 bytes | Krzysztof Domański |
#12 | 2829990-12-test-only.patch | 1.76 KB | Krzysztof Domański |
#10 | interdiff-8-10.txt | 3.25 KB | idebr |
#10 | 2829990-10.patch | 3.26 KB | idebr |
Comments
Comment #2
cilefen CreditAttribution: cilefen commentedIn 8.3.0 you will be able to get the URL separately.
Comment #3
cilefen CreditAttribution: cilefen commentedComment #4
KingdutchThis is still a problem in the latest version of Drupal.
I think the main problem is that the Image object gets a "URL" attribute which doesn't represent an anchor tag (link) so it has no attributes like a class or title.
Comment #6
lamp5I met with this problem when I wanted to extend Thumbnail formatter to show content link in the modal. I think that image-formatter.html.twig should be using link function to generate correctly link. I attach a patch but you can copy image-formatter.html.twig file to your theme and fix it manually.
Comment #7
lamp5Comment #8
Krzysztof DomańskiI removed the unnecessary code (vendor) from the patch #6.
Comment #9
larowlanThanks, but we need a test here to make sure it doesn't have a regression
Comment #10
idebr CreditAttribution: idebr at ezCompany commentedAttached patch implements the following changes:
\Drupal\Tests\image\Kernel\ImageFormatterTest
showing the shortcoming of the current API.link()
function.core/modules/image/templates/image-formatter.html.twig
to allimage-formatter.html.twig
files in Drupal Core.Comment #12
Krzysztof DomańskiThe changes look good.
I just removed the unnecessary code.
Comment #14
idebr CreditAttribution: idebr at ezCompany commented#12 Nice cleanup!
Note that
responsive-image-formatter.html.twig
uses an identical pattern:in core/modules/responsive_image/templates/responsive-image-formatter.html.twig
I am not sure if this should be fixed in this issue as well or in a follow-up.
Comment #16
Krzysztof Domański#15 Unrelated random test failure. Back to RTBC.
Comment #18
larowlanCommitted 3e8c2b3 and pushed to 8.8.x. Thanks!
c/p to 8.7.x
Comment #20
idebr CreditAttribution: idebr at ezCompany commented#14 Filed a follow up issue for the Responsive Image formatter at #3064751: Responsive image formatter does not support URL/Link options