Problem/Motivation

The image theme function requires an array for the attributes variable:

'image' => [
      'variables' => ['uri' => NULL, 'width' => NULL, 'height' => NULL, 'alt' => '', 'title' => NULL, 'attributes' => [], 'sizes' => NULL, 'srcset' => [], 'style_name' => NULL],
    ],

But template_preprocess_image_formatter uses its item attributes for the image render array:

function template_preprocess_image_formatter(&$variables) {
  //...
  $variables['image']['#attributes'] = $variables['item_attributes'];
  //...
}

According to the theme definition these can be NULL, which is not valid:

function image_theme() {
  return [
    //...
    'image_formatter' => [
      'variables' => ['item' => NULL, 'item_attributes' => NULL, 'url' => NULL, 'image_style' => NULL],
      'file' => 'image.field.inc',
    ],
  ];
}

Proposed resolution

Cast the attributes to an array in template_preprocess_image_formatter.

Issue fork drupal-3190843

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

Matthijs created an issue. See original summary.

matthijs’s picture

Title: template_preprocess_image_formatter doesn't respect signature of image theme function » template_preprocess_image_formatter() doesn't respect signature of image theme function
matthijs’s picture

Status: Active » Needs review

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative, +Needs tests

This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.

This will need a test case to show the issue and that it's been resolved please

Thanks!

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

acbramley’s picture

This popped up as part of BSI.

It needs a reroll on to an MR as well as the above mentioned tests.

We could also just NULL coallesce instead of casting to an array.

kim.pepper’s picture

From what I can see, casting to an array would produce an error trying to convert a string or NULL to an array?

Perhaps we could do something like

is_array($var) ? $var : [$var] ?

This needs a test to verify we aren't breaking anything.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.