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
" 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
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
Comment #3
debdeep.mukhopadhyay commentedHi @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
Comment #4
ltrainHi @debdeep,
Yes there's currently no tagged release. I'm using 11.4.x-dev.
Thanks,
Laura
Comment #5
tobiasbComment #6
sébastien-frI 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.
Comment #7
mark_fullmerConfirming that on Drupal 11.4.0 (released today) the proposed change prevents the fatal error. Marking as RTBC.
Comment #8
philyMR!60 works for me too, thanks for the quick fix.
Comment #10
anybodyPlease 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?
Comment #11
phily@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)
Comment #12
daften commentedNot 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)
Comment #14
lluvigneHello!
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:
Back to Needs review, since the code changed since the RTBC.
Comment #15
tobiasbThumbs-up ;-)