Problem/Motivation

All 3 field formatters provided by this module (SvgImageFormatter, SvgImageUrlFormatter and SvgResponsiveImageFormatter) extend core classes. However, they copy most of the code from the parent class instead of calling parent methods and modifying their result.
This is bad practice, because updates to the parent classes have to be introduced in the child classes as well, otherwise we're missing new features.

Proposed resolution

Instead of repeating code, call the parent method and alter its result where necessary.

Remaining tasks

Review. Commit.

User interface changes

None.

API changes

None.

Data model changes

None.

Issue fork svg_image-3257729

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

mrshowerman created an issue. See original summary.

mrshowerman’s picture

Assigned: mrshowerman » Unassigned
Status: Active » Needs review
StatusFileSize
new10.19 KB

Here's a first patch.

mrshowerman’s picture

StatusFileSize
new10.36 KB
new1.11 KB

Make sure the #item_attributes is always an array.

nironan’s picture

Version: 8.x-1.x-dev » 3.x-dev

nironan’s picture

prashant.c’s picture

Status: Needs review » Needs work

There is an issue:

svg_image_responsive/src/Plugin/Field/FieldFormatter/SvgResponsiveImageFormatter.php
$linkFile in isset() is never defined

mrshowerman’s picture

Status: Needs work » Needs review

Adressing #7

mrshowerman’s picture

Issue summary: View changes
grevil’s picture

Pointing out @mably's comment here, as some people don't get mail notifications through gitlab:

Could it get rebased please?

mrshowerman’s picture

Issue tags: -Needs reroll

Re-rolled.

mrshowerman’s picture

Now all tests are green.

mably’s picture

@mrshowerman thanks for the rebase.

The suggested improvement seems to executes some code twice like;

$images = $this->getEntitiesToView($items, $langcode)

which gets executed in the parent first and then is executed a second time in the child formatter.

So I'm not sure the gain is really worth it.

Let's see what others think about it.

gaëlg’s picture

This code is executed twice because it looks like we need $images in the overriding method, to loop on it. It seems there's no clean way to do otherwise without re-implementing the parent method. Ideally, the parent method might be split into several ones so that we don't have to make that second call, but from a contrib module point of view, we can't change core code.

Yes, this is not perfect because it could have a negative impact on performance.

BUT performance is not the only criteria. I this case, I suspect the second call will be very fast and lead to no additional SQL query thanks to various Drupal cache layers. Additionally, the result of this execution can also be cached (render caching).
So I'd say we can trade a tiny performance degradation to get a contrib module that has much more chance to work well with subsequent core versions.

mirakolous’s picture

Sharing in case anyone else runs into this. The current patch is behind and does not apply. I attempted to rebase, but it was too many conflicts for me to resolve confidently. In order to not stall the updates to other modules, I had to change svg_image in composer.json like so:

"drupal/svg_image": "dev-3.x#2fe16c7a9f0c511bbcaa44499a4fb61fa0f9854c",

That is the last hash that this patch applies cleanly to.

gaëlg’s picture

@mirakolous The patch generated from the MR (https://git.drupalcode.org/project/svg_image/-/merge_requests/18.diff) applied on 3.0.1 some days ago.

mrshowerman’s picture

Rebased the MR onto the latest 3.x.
@mirakolous, you should now be able to apply the MR's plain diff to 3.x-dev again.

sagesolutions’s picture

Status: Needs review » Needs work

I could not apply the MR #18 diff to any logical branch. I tried version 3.0.2, version 3.1.0, dev-3.x#2fe16c7a9f0c511bbcaa44499a4fb61fa0f9854c, and the latest 3.x-dev branch.

Patching all of them failed.

mrshowerman’s picture

Status: Needs work » Needs review

Rebased.
@sagesolutions, you should be able to test this now against 3.x-dev.

sagesolutions’s picture

Status: Needs review » Reviewed & tested by the community

Tested on the 3.x-dev branch and Image loading attributes are now working properly. Thanks @mrshowerman!

grevil’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: -Needs manual testing

Changes LGTM! Could you add a few explanatory comments in the code?

Some of the code is not straight forward. Adding comments would speed up understanding the code in the future! 🙂

mrshowerman’s picture

Status: Needs work » Needs review

All feedback has been addressed, except for one thing where @mably requests input from @Grevil. Setting back to NR.

grevil’s picture

Status: Needs review » Needs work

Resolved most threads, but one! Then we can merge this :)

mrshowerman’s picture

Status: Needs work » Needs review

Addressed feedback by @grevil.

  • mably committed f6704dfb on 3.x authored by nironan
    Issue #3257729: Formatters shouldn't repeat core code
    
mably’s picture

Merged! Thanks for all the great work.

mably’s picture

Status: Needs review » Fixed
grevil’s picture

just discovered after merging that having configured a link to the image in the responsive image display formatter generates a WSOD.

Is there an open issue for this already? What are the steps to reproduce this?

mrshowerman’s picture

Is there an open issue for this already?

Not yet. I had a quick look at this yesterday and was able to reproduce. I also have a fix ready.
Will create a new issue with that fix soon.

mrshowerman’s picture

mably’s picture

Thanks @mrshowerman !

mably’s picture

The problem is that ImageFormatter and Responsive ImageFormatter render the link differently:

image-formatter.html.twig:

{% if url %}
  {{ link(image, url) }}
{% else %}
  {{ image }}
{% endif %}

responsive-image-formatter.html.twig:

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

So we just need to also override the #url property when we override the #theme property to image_formatter in the SvgResponsiveImageFormatter class.

mably’s picture

@mrshowerman not sure we need to create a new issue for that, just open a new MR here with the fix.

grevil’s picture

I agree with @mably. I originally thought, that the problem was unrelated based on:

Actually it fails with release 3.1.0 too, so most probably not related to this MR

Fine if we create a follow-up fix here!

mably’s picture

Yep, removed that comment in Gitlab as it is working with 3.1.0 in fact.

Should be an easy fix.

mrshowerman’s picture

@mrshowerman not sure we need to create a new issue for that, just open a new MR here with the fix.

Oops, missed that. I created #3486745: Fatal error when linking SVGs to file in the meantime, but I'll be happy to add the fix to this issue if that is preferred.

mrshowerman’s picture

Created a new MR that addresses all open issues.
Not sure what the right status should be now 🙈

mably changed the visibility of the branch 3257729-formatters-shouldnt-repeat to hidden.

mably changed the visibility of the branch 3257729-formatters-shouldnt-repeat to active.

  • mably committed dd470d35 on 3.x authored by mrshowerman
    Issue #3257729 follow-up: Fatal error when linking SVGs to file
    
mably’s picture

Merged. Thanks for the quick fix @mrshowerman!

grevil’s picture

Great stuff! Thanks all! :)

I guess, we should create a new release pretty soon! Let's wait a couple of days, just in case we find anything else.

Status: Fixed » Closed (fixed)

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

nkind’s picture

Just wondering if there's an ETA for the next release with this since it includes the image attribute fix as well?

Great work to all involved by the way!

mably’s picture

@nkind version 3.2.0 including this fix has been released!

nkind’s picture

@mably Thank you!