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
dblogrow limit of 1000. - The
loadingattribute set byImageFormatteris silently lost,
because theAttributeobject is constructed empty. Verified on a page with 32
easy_lqp images: 0 of 32 carriedloading=on 11.4, and 32 of 32 did after the
fix.
Steps to reproduce
- Install Drupal 11.4 (tested on 11.4.4) with easy_lqp 2.0.2 on PHP 8.4.
- Configure an image field display to use the "Easy LQP images" formatter.
- View a page rendering that field with error display enabled.
- Two warnings appear per image; they are also written to
dblogregardless 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
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
shiraz dindarMR !5 reads
#attributesfirst and falls back to#item_attributes, so the formatterworks 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:
dblogentries across five page loads thatpreviously logged them on every image.
loadingattribute is restored: on a page rendering 32 easy_lqp images,0 of 32 carried
loading=before the fix and 32 of 32 after.alt,widthanddata-multiplierare unchanged.Comment #5
paulsheldrake commented