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

  1. Write a patch
  2. Review
  3. Commit

User interface changes

None.

API changes

Linked images can now render link attributes.

Data model changes

None.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

lcube created an issue. See original summary.

cilefen’s picture

In 8.3.0 you will be able to get the URL separately.

cilefen’s picture

Title: Image formatter and url options » Image formatter does not support URL options
Kingdutch’s picture

Title: Image formatter does not support URL options » Image formatter does not support URL/Link options
Version: 8.2.3 » 8.5.x-dev

This 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.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

lamp5’s picture

Status: Active » Needs review
FileSize
672 bytes

I 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.

lamp5’s picture

Issue tags: +image, +url, +Twig
Krzysztof Domański’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
433 bytes

I removed the unnecessary code (vendor) from the patch #6.

larowlan’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

Thanks, but we need a test here to make sure it doesn't have a regression

idebr’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -image, -url, -Needs tests
FileSize
1.96 KB
3.26 KB
3.25 KB

Attached patch implements the following changes:

  1. Added a new test method to \Drupal\Tests\image\Kernel\ImageFormatterTest showing the shortcoming of the current API.
  2. Removed the empty optional arguments to twig's link() function.
  3. Extended the change in core/modules/image/templates/image-formatter.html.twig to all image-formatter.html.twig files in Drupal Core.

The last submitted patch, 10: 2829990-10-test-only.patch, failed testing. View results

Krzysztof Domański’s picture

Version: 8.6.x-dev » 8.8.x-dev
Status: Needs review » Reviewed & tested by the community
FileSize
1.76 KB
901 bytes
3.04 KB

The changes look good.

I just removed the unnecessary code.

--- a/core/modules/image/tests/src/Kernel/ImageFormatterTest.php
+++ b/core/modules/image/tests/src/Kernel/ImageFormatterTest.php
@@ -178,9 +178,7 @@ public function testImageFormatterSvg() {
    * Tests Image Formatter URL options handling.
    */
   public function testImageFormatterUrlOptions() {
-    $field_formatter_configuration = $this->display->getComponent($this->fieldName);
-    $field_formatter_configuration['settings']['image_link'] = 'content';
-    $this->display->setComponent($this->fieldName, $field_formatter_configuration);
+    $this->display->setComponent($this->fieldName, ['settings' => ['image_link' => 'content']]);

The last submitted patch, 12: 2829990-12-test-only.patch, failed testing. View results

idebr’s picture

#12 Nice cleanup!

Note that responsive-image-formatter.html.twig uses an identical pattern:

{% if url %}
  <a href="{{ url }}">{{ responsive_image }}</a>
{% else %}
  {{ responsive_image }}
{% endif %}

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.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 12: 2829990-12.patch, failed testing. View results

Krzysztof Domański’s picture

Status: Needs work » Reviewed & tested by the community

#15 Unrelated random test failure. Back to RTBC.

  • larowlan committed 3e8c2b3 on 8.8.x
    Issue #2829990 by Krzysztof Domański, idebr, lamp5: Image formatter does...
larowlan’s picture

Version: 8.8.x-dev » 8.7.x-dev
Status: Reviewed & tested by the community » Fixed

Committed 3e8c2b3 and pushed to 8.8.x. Thanks!

c/p to 8.7.x

  • larowlan committed aa785d0 on 8.7.x
    Issue #2829990 by Krzysztof Domański, idebr, lamp5: Image formatter does...
idebr’s picture

#14 Filed a follow up issue for the Responsive Image formatter at #3064751: Responsive image formatter does not support URL/Link options

Status: Fixed » Closed (fixed)

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