Problem/Motivation

On PHP 8.5, TwigExtension::withoutFilter() can trigger: Deprecated function: ArrayIterator::__construct(): Using an object as a backing array for ArrayIterator is deprecated... in TwigExtension::withoutFilter() when flattening filter arguments via
RecursiveArrayIterator / RecursiveIteratorIterator.

This happens when a nested value in $args is an object — commonly when a theme mistakenly passes a render element instead of a string key, e.g.: {{ content|without(content.field_example) }} instead of: {{ content|without('field_example') }}

With error display enabled, the deprecation output can be emitted during rendering and contribute to "headers already sent" / session failures.

Related: #3543711: Fix PHP 8.5 deprecations of chr() and array iterator fixed other ArrayIterator PHP 8.5 cases, but not withoutFilter(). #3093577: Let Twig without() filter take both arrays and strings as arguments introduced RecursiveArrayIterator here.

Steps to reproduce

1. Run Drupal on PHP 8.5 with verbose error reporting.
2. Render a template that calls |without() with a render array / object nested in the arguments (or use a unit test that passes ['#object' => new \stdClass()] among the args).
3. Observe the ArrayIterator::__construct() deprecation.

Proposed resolution

Collect only scalar (and NULL) keys with array_walk_recursive() instead of RecursiveArrayIterator, so nested objects are ignored and no longer trigger the PHP 8.5 deprecation. Existing string/array-of-strings argument behavior is preserved.

Issue fork drupal-3631152

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

trishen created an issue. See original summary.

trishen changed the visibility of the branch 3631152-php-8.5-arrayiterator to hidden.

trishen’s picture

Status: Active » Needs review
quietone’s picture

Hi, Issues for Drupal core should be targeted to the 'main' branch, our primary development branch. Changes are made on the main branch first, and are then back ported as needed according to the Core change policies. The version the problem was discovered on should be stated in the issue summary Problem/Motivation section. Thanks.

I reformatted the issue summary so it was easier to read.

trishen’s picture

Version: main » 11.4.x-dev
smustgrave’s picture

Version: 11.4.x-dev » main
Status: Needs review » Needs work

Issue needs to land in main first so changing the version.

MR appears to have issues so moving to NW for that.

Thanks.