Deprecated function: pathinfo() on template_preprocess_image_style

Created on 26 August 2022, almost 2 years ago
Updated 21 November 2023, 7 months ago

Problem/Motivation

After upgrading to PHP 8.1, we get a deprecated warning when the Image style Uri is not set on template_preprocess_image_style.

%type: @message in %function (line %line of %file) @backtrace_string. | a:6:{s:5:"%type";s:19:"Deprecated function";s:8:"@message";O:25:"Drupal\Core\Render\Markup":1:{s:9:" * string";s:77:"pathinfo(): Passing null to parameter #1 ($path) of type string is deprecated";}s:9:"%function";s:45:"Drupal\image\Entity\ImageStyle->supportsUri()";s:5:"%file";s:78:"/mnt/www/html/myproject/docroot/core/modules/image/src/Entity/ImageStyle.php";s:5:"%line";i:385;s:17:"@backtrace_string";s:16143:"#0 /mnt/www/html/myproject/docroot/core/includes/bootstrap.inc(346): _drupal_error_handler_real(8192, 'pathinfo(): Pas...', '/mnt/www/html/d...', 385)
#1 [internal function]: _drupal_error_handler(8192, 'pathinfo(): Pas...', '/mnt/www/html/d...', 385)
#2 /mnt/www/html/myproject/docroot/core/modules/image/src/Entity/ImageStyle.php(385): pathinfo(NULL, 4)
#3 /mnt/www/html/myproject/docroot/core/modules/image/image.module(274): Drupal\image\Entity\ImageStyle->supportsUri(NULL)
#4 /mnt/www/html/myproject/docroot/core/lib/Drupal/Core/Theme/ThemeManager.php(287): template_preprocess_image_style(Array, 'image_style', Array)
#5 /mnt/www/html/myproject/docroot/core/lib/Drupal/Core/Render/Renderer.php(422): Drupal\Core\Theme\ThemeManager->render('image_style', Array)
#6 /mnt/www/html/myproject/docroot/core/lib/Drupal/Core/Render/Renderer.php(201): Drupal\Core\Render\Renderer->doRender(Array, false)

This seems because the URI is not defined on the variable URI when the method supportsUri is called, function template_preprocess_image_style.
https://git.drupalcode.org/project/drupal/-/blob/9600a0b13a4301c0071dad3...

....
if ($style->supportsUri($variables['uri'])) {
    $variables['image']['#uri'] = $style->buildUrl($variables['uri']);
  }
  else {
...

Steps to reproduce

Try to generate an image style without URI (can be tried with a render array).

Proposed resolution

Check if the URI is set before calling supportURI, because the method expects a string, or change the param to string|null and verify there is the param is a string.

It is possible that this fix conflicts with issues:

πŸ› Bug report
Status

Needs work

Version

9.5

Component
Image systemΒ  β†’

Last updated about 14 hours ago

Created by

πŸ‡ͺπŸ‡ΈSpain Eduardo Morales Alberti Spain, πŸ‡ͺπŸ‡Ί

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024