Problem/Motivation

On Drupal 11.4, rendering an SVG through the SVG Image field formatter (with an image style configured, i.e. the "render as Only local images are allowed." path) throws a fatal:

TypeError: Unsupported operand types: null + array
in Drupal\svg_image\Plugin\Field\FieldFormatter\SvgImageFormatterTrait->renderAsImg()
(line 130 of src/Plugin/Field/FieldFormatter/SvgImageFormatterTrait.php)

Line 130 does $element['#item_attributes'] += $attributes;. Drupal 11.4 deprecated the image_formatter render-element key #item_attributes in favor of #attributes (change record #3554585: Using #item_attributes with image_formatter and responsive_image_formatter is deprecated); ImageFormatter::viewElements() no longer sets #item_attributes, so it is null and null += array fatals.

Steps to reproduce

  • Drupal 11.4, Svg Image 3.2.2.
  • Add an image field; upload an .svg file.
  • Set the field's display formatter to SVG Image with an image style selected.
  • View the entity → fatal TypeError: Unsupported operand types: null + array.

Proposed resolution

Write to #attributes (the key the 11.4 image theme reads), with a fallback so it keeps working on < 11.4:

// src/Plugin/Field/FieldFormatter/SvgImageFormatterTrait.php  (renderAsImg)
$element['#attributes'] = ($element['#attributes'] ?? $element['#item_attributes'] ?? []) + $attributes;

(See #3554585: Using #item_attributes with image_formatter and responsive_image_formatter is deprecated. Verified on Drupal 11.4 — fixes the fatal and the SVG attributes render correctly.)

Remaining tasks

User interface changes

API changes

Data model changes

Issue fork svg_image-3601394

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

ltrain created an issue. See original summary.

debdeep.mukhopadhyay’s picture

Hi @itrain,

Thanks you for the issue and MR.

I'm currently trying to reproduce the issue locally and review the MR. I was able to test the module on Drupal 11, but I couldn't yet set up the exact Drupal 11.4 environment mentioned in the issue because drupal/recommended-project:11.4 is not available via Composer on my side.

Could you please share the exact Drupal version (for example, 11.4.x-dev, 11.4.0-beta, etc.) or the Composer command you used to create your test environment?

That would help me reproduce the issue and verify the MR more correctly.

I used AI assistance to help with the local setup and to better understand the issue while testing and drafting this comment.

Thanks

ltrain’s picture

Hi @debdeep,

Yes there's currently no tagged release. I'm using 11.4.x-dev.

Thanks,
Laura

tobiasb’s picture

Issue summary: View changes
sébastien-fr’s picture

I encountered the same issue after running composer update which unintentionally pulled in Drupal 11.4.0-rc2. I wasn't targeting a RC release. I rolled back to the latest stable (11.3.13) which resolved the issue, but the fix proposed in MR #60 looks correct for when 11.4 is released as stable.

mark_fullmer’s picture

Status: Active » Reviewed & tested by the community

Confirming that on Drupal 11.4.0 (released today) the proposed change prevents the fatal error. Marking as RTBC.

phily’s picture

MR!60 works for me too, thanks for the quick fix.

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

anybody’s picture

Please check if this already merged issue: #3601247: Module throws error when trying to add media in node edit form which is less disruptive.

I'm a bit afraid what will happen to Drupal installations < 11.4 when merging this one. Could someone please check the consequences?

phily’s picture

@anybody: the mentionned related issue 3601247 seems to have fixed this current issue 3601394 in release 3.2.4 (at least using Drupal 11.4.1, not tested using lower releases)

daften’s picture

Not fully I think, 3.2.4 still writes to the deprecated #item_attributes (Drupal 11.4 ignores it, so width/height attributes are cosmetically dropped)

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

lluvigne’s picture

Version: 3.2.2 » 3.x-dev
Status: Reviewed & tested by the community » Needs review

Hello!

First, I rebased MR !60 onto 3.x.

One thing to fix before this goes in: the rebase writes to #attributes unconditionally, but the module supports ^10.3 || ^11 and only core >= 11.4 reads that key. On 10.3–11.3 the image_formatter theme hook has no attributes variable at all, so the svg_attributes suppression is silently lost there.

renderAsImg() is only called from SvgImageFormatter, which extends ImageFormatter, so the key already in $element is the one the running core reads. So I propose some changes to keep the module compatible in both core versions:

// Drupal 11.4 renamed #item_attributes to #attributes.
// @see https://www.drupal.org/node/3554585
$key = array_key_exists('#attributes', $element) ? '#attributes' : '#item_attributes';
$element[$key] = ($element[$key] ?? []) + $attributes;

Back to Needs review, since the code changed since the RTBC.

tobiasb’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

Thumbs-up ;-)