Problem/Motivation

Follow up issue for #2829990: Image formatter does not support URL/Link options

When using a responsive image field formatter and adding a link to a file or hosting entity, URL options are not handled by the native image formatter so it is impossible to add a class or other attributes to the generated link...

function : template_preprocess_responsive_image_formatter
template : responsive-image-formatter.html.twig

Proposed resolution

Use the link() Twig method to render the linked responsive image, so URL options are output as expected.

Remaining tasks

  1. Review
  2. Commit

User interface changes

None.

API changes

Linked responsive images can now render link attributes.

Data model changes

None.

Issue fork drupal-3064751

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

idebr created an issue. See original summary.

krzysztof domański’s picture

Status: Active » Needs review
StatusFileSize
new4.01 KB
new4.54 KB

The last submitted patch, 2: 3064751-test-only.patch, failed testing. View results

idebr’s picture

Status: Needs review » Needs work
Related issues: +#2829990: Image formatter does not support URL/Link options

Change looks good! However, the change to the template should also be applied to the template in core/themes/stable/templates/field/responsive-image-formatter.html.twig

krzysztof domański’s picture

Status: Needs work » Needs review
StatusFileSize
new524 bytes
new5.04 KB

Status: Needs review » Needs work

The last submitted patch, 5: 3064751-5.patch, failed testing. View results

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

ocastle’s picture

I can also confirm that the patch works well for me.
Just a case of fixing the failing tests.

idebr’s picture

Status: Needs work » Needs review
StatusFileSize
new2.55 KB
new5.66 KB
new7.61 KB

template_preprocess_responsive_image_formatter() documents $variables['url'] as An optional \Drupal\Core\Url object. However, the code path for 'Linked to file' currently returns a relative path ('/sites/simpletest/...') instead of an Url object. I have updated the code accordingly.

The last submitted patch, 9: 3064751-9-test-only.patch, failed testing. View results

lendude’s picture

Status: Needs review » Reviewed & tested by the community

Patch looks good, nice test coverage and fix is in line with the fix in #2829990: Image formatter does not support URL/Link options

lendude’s picture

Issue summary: View changes

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

xjm’s picture

Status: Reviewed & tested by the community » Needs work

The 9.1.x test failed with:


mage.Drupal\Tests\responsive_image\Kernel\ResponsiveImageFormatterTest
✗	
Drupal\Tests\responsive_image\Kernel\ResponsiveImageFormatterTest
fail: [Other] Line 0 of sites/default/files/simpletest/phpunit-516.xml:
PHPunit Test failed to complete; Error: PHPUnit 8.5.3 by Sebastian Bergmann and contributors.

Testing Drupal\Tests\responsive_image\Kernel\ResponsiveImageFormatterTest
W                                                                   1 / 1 (100%)

Time: 2.27 seconds, Memory: 4.00 MB

There was 1 warning:

1) Drupal\Tests\responsive_image\Kernel\ResponsiveImageFormatterTest::testImageFormatterUrlOptions
Using assertContains() with string haystacks is deprecated and will not be supported in PHPUnit 9. Refactor your test to use assertStringContainsString() or assertStringContainsStringIgnoringCase() instead.

/var/www/html/vendor/phpunit/phpunit/src/Framework/TestResult.php:691

WARNINGS!
Tests: 1, Assertions: 8, Warnings: 1.

Remaining self deprecation notices (1)

  1x: Declaring ::setUp without a void return typehint in Drupal\Tests\responsive_image\Kernel\ResponsiveImageFormatterTest is deprecated in drupal:9.0.0. Typehinting will be required before drupal:10.0.0. See https://www.drupal.org/node/3114724
    1x in DrupalListener::startTest from Drupal\Tests\Listeners
ravi.shankar’s picture

Status: Needs work » Needs review
StatusFileSize
new7.63 KB
new849 bytes

Here I have added a patch that might pass failed tests.

Status: Needs review » Needs work

The last submitted patch, 15: 3064751-15.patch, failed testing. View results

ravi.shankar’s picture

Status: Needs work » Needs review
StatusFileSize
new7.63 KB

This patch might pass the tests.

ravi.shankar’s picture

StatusFileSize
new7.63 KB
new639 bytes

There was an issue in the patch #17, so added a new patch for review.

idebr’s picture

Status: Needs review » Reviewed & tested by the community

#14 The test failures in 9.1.x have been addressed.

Setting back to RTBC.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record
+++ b/core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php
@@ -230,7 +230,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
-        $url = $file->createFileUrl();
+        $url = Url::fromUri($file->createFileUrl(FALSE));

Why are we mixing this change with a change to whether this is absolute or not. That doesn't seem something that should occur without discussion and doesn't seem to be in-scope.

Also as we're changing the type of a theme variable from a string to a Url object we should have a change record.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

ocastle’s picture

StatusFileSize
new5.98 KB

Re-rolled patch #18 for 9.3.x

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

idebr’s picture

Status: Needs work » Needs review
Issue tags: -Needs change record
StatusFileSize
new4.51 KB
new4.04 KB
new8.88 KB

#20.1 The URL is now relative again, so this is unchanged compared to HEAD

#20.2 Change record is available at https://www.drupal.org/node/3291487

The last submitted patch, 26: 3064751-26-test-only.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 26: 3064751-26.patch, failed testing. View results

dydave’s picture

Status: Needs work » Needs review

The failure of tests for patch 3064751-26-test-only.patch from #26 above, demonstrate the tests are working and the patch fixes the isse:

Setting back to Needs review.
Thanks in advance!

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative, +Needs change record

This issue is being reviewed by the kind folks in Slack, #need-reveiw-queue. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge require as a guide.

IS proposed solution appears to match what is being accomplished in the patch. - Good
Tests only patch is showing red/green - Good

At this time we will need a D10.1.x version of the patch. (Should be close but instead of stable it's stable9 now)

Also the CR think could use an update to mention the change being done to the twig templates. If themers have their own responsive-image-formatter.html.twig they will have to update to work correct.

ravi.shankar’s picture

StatusFileSize
new8.88 KB
new2.8 KB

Added reroll of patch #26 on Drupal 10.1.x. keeping the status to needs work for other things of comment #31.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

idebr’s picture

Status: Needs work » Needs review
Issue tags: -Needs change record
  1. Updated the draft change record with "Impact on theme": A theme that interacts with the Url in a preprocess hook will have to update to match the new data type. Output in Twig is not affected, as Twig converts Url objects to strings automatically. See https://www.drupal.org/node/3291487
  2. The patch is now available as a merge request: https://git.drupalcode.org/project/drupal/-/merge_requests/14965
smustgrave’s picture

Status: Needs review » Needs work

Small comments, but super close!

idebr’s picture

Status: Needs work » Needs review
  1. Implemented php constructor property promotion for ResponsiveImageFormatter
  2. Added typed properties to ResponsiveImageFormatterTest, but I don't think the test can drop them as they are used in the setUp and test method
smustgrave’s picture

I meant we could drop the @var since it’s typed. Sorry

idebr’s picture

The @var declarations are now removed from the typed properties:

@var string
@var \Drupal\Core\Entity\Display\EntityViewDisplayInterface
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Thanks sorry I wasn’t clear the first time. Feedback appears to be addressed nothing else to add

godotislate made their first commit to this issue’s fork.

godotislate’s picture

I made a couple minor changes to the MR:
In ResponsiveImageFormatter.

  • Removed property promotion of the new $fileUrlGenerator constructor parameter. This is to avoid making the property itself nullable.

In ResponsiveImageFormatterTest:

  • Removed the mb_strolower wrapping of $this->randomMachineName(), since the generated random machine name is already all lower.
  • Generated only 1 sample file, since that's all that's needed for the test

If someone could give this a +1 for RTBC, I can commit.

idebr’s picture

Thanks @godotislate, the changes look good to me!

  • godotislate committed 458eb456 on 11.x
    fix: #3064751 Responsive image formatter does not support URL/Link...

  • godotislate committed a85f1db8 on main
    fix: #3064751 Responsive image formatter does not support URL/Link...
godotislate’s picture

Version: main » 11.x-dev
Status: Reviewed & tested by the community » Fixed

Committed and and pushed a85f1db to main and 458eb45 to 11.x. Thanks!

I forgot to mention I made some small edits to the CR the last time I reviewed. I have published the CR now as well.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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