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:

Issue fork drupal-3306131

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

eduardo morales alberti’s picture

Issue summary: View changes

eduardo morales alberti’s picture

Status: Active » Needs review
eduardo morales alberti’s picture

Title: Passing null to parameter #1 ($path) of type string is deprecated on template_preprocess_image_style » Deprecated function: pathinfo() on template_preprocess_image_style
maxilein’s picture

I'd test, but there is no patch...

eduardo morales alberti’s picture

@mxilein you can use the diff of the merge request as patch https://git.drupalcode.org/project/drupal/-/merge_requests/2680/diffs.patch

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

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should 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.

Thank you for working on this.

The MR should be updated to 9.5.x and a separate MR for 10.1.x

Tagging for tests to show this error.

robcarr’s picture

The MR (applied as a patch) worked for me.

hoporr’s picture

There is a conflict with another open issue

"Call to a member function transformDimensions() on null in template_preprocess"
https://www.drupal.org/project/drupal/issues/2957368

Both issues change the same section of code. Since the other issue had more changes, and the fix for this issue here is just one line, I added that safety check into the other issue's patch (now in patch #50).

Version: 9.5.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. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

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.