Problem/Motivation

Drupal 11.4 renamed the #item_attributes render array key produced by
ImageFormatter::viewElements() to #attributes. The key is deprecated
in 11.4 and removed in 12.0 (see change record
3554585
).

EasyLqpImagesFormatter extends ImageFormatter and reads
$elements[$delta]['#item_attributes'] at line 175. On Drupal 11.4 that key no
longer exists, which produces two PHP warnings per rendered image:

Warning: Undefined array key "#item_attributes" in
Drupal\easy_lqp\Plugin\Field\FieldFormatter\EasyLqpImagesFormatter->viewElements()
(line 175 of modules/contrib/easy_lqp/src/Plugin/Field/FieldFormatter/EasyLqpImagesFormatter.php)

Warning: foreach() argument must be of type array|object, null given in
Drupal\Core\Template\Attribute->__construct()
(line 89 of core/lib/Drupal/Core/Template/Attribute.php)

The missing key yields NULL, which is passed to new Attribute(NULL), whose
constructor then foreaches over NULL — hence the second warning.

Two consequences:

  • Drupal logs both warnings on every image render. On a page rendering 32 images that is 64
    watchdog entries per request, which quickly evicts everything else under the default
    dblog row limit of 1000.
  • The loading attribute set by ImageFormatter is silently lost,
    because the Attribute object is constructed empty. Verified on a page with 32
    easy_lqp images: 0 of 32 carried loading= on 11.4, and 32 of 32 did after the
    fix.

Steps to reproduce

  1. Install Drupal 11.4 (tested on 11.4.4) with easy_lqp 2.0.2 on PHP 8.4.
  2. Configure an image field display to use the "Easy LQP images" formatter.
  3. View a page rendering that field with error display enabled.
  4. Two warnings appear per image; they are also written to dblog regardless of
    the error display setting.

Proposed resolution

Read #attributes first and fall back to #item_attributes, so the
formatter works on both Drupal 10/11.3 and 11.4+:

$item_attributes = $elements[$delta]['#attributes']
  ?? $elements[$delta]['#item_attributes']
  ?? [];
$elements[$delta]['#item_attributes'] = new Attribute($item_attributes);

The module keeps writing to #item_attributes because that is the variable its
own easy_lqp_formatter theme hook declares and its template renders. Renaming the
module's own theme variable would be a separate, template-breaking change and is not proposed
here.

Remaining tasks

  • Review the merge request
  • Commit and tag a release

User interface changes

None.

API changes

None.

Data model changes

None.

Issue fork easy_lqp-3614120

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

shiraz dindar created an issue. See original summary.

shiraz dindar’s picture

Status: Active » Needs review

MR !5 reads
#attributes first and falls back to #item_attributes, so the formatter
works on Drupal 10/11.3 and 11.4+ without a version check.

Tested on Drupal 11.4.4 / PHP 8.4 with easy_lqp 2.0.2:

  • Both warnings are gone — 0 new dblog entries across five page loads that
    previously logged them on every image.
  • The loading attribute is restored: on a page rendering 32 easy_lqp images,
    0 of 32 carried loading= before the fix and 32 of 32 after. alt,
    width and data-multiplier are unchanged.

  • shiraz dindar committed 3033f225 on 2.0.x
    Issue #3614120: Fix undefined #item_attributes key on Drupal 11.4
    
paulsheldrake’s picture

Status: Needs review » Fixed

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.