Closed (works as designed)
Project:
Drupal core
Version:
10.5.x-dev
Component:
theme system
Priority:
Major
Category:
Bug report
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
22 Aug 2025 at 20:39 UTC
Updated:
6 Jan 2026 at 23:37 UTC
Jump to comment: Most recent
Comments
Comment #2
cilefen commentedBecause this release has been out for a couple of weeks and this is the only bug report of its kind we need more information about the conditions in which the bug occurs.
Comment #3
bdanin commentedI've found this has to do with the
|lastfilter in twig.Comment #4
bdanin commentedThis turned out not to be a Drupal core regression but a side-effect of the Twig upgrade to 3.21.x in Drupal 10.5.2. The newer Twig runtime iterates Traversables differently. Using the
|lastfilter on field item lists or render arrays (which are lazy iterables) now causes recursion and “Nesting level too deep” errors.The fix is to avoid
|last(or|length) on render arrays/field iterators. Instead, use loop.last inside a for loop or compute the last item in a preprocess hook and print that in the template.I was specifically using
|lastinside a{% for item in items %}loop, likely hitting this with the embedded Twig upgrade surfacing a fragile Twig pattern (|laston lazy/recursive iterables)Closing this since the error is due to a Twig behavior change, not a bug in Drupal core itself.
Comment #5
bdanin commentedComment #6
bdanin commentedNote, in case anyone else hits this error, here was the problem and solution for me.
Before where `|last` caused the recursive twig error:
After, which resolved the error:
Replacing
|lastwithloop.lastworked. I'm adding a pipe between list items in a twig field template, but don't want it on the last item. I use ` ` because a plain empty space is removed with html-minification.Comment #7
miguelarber commentedAdditionally, using
items|sortinside a loop (not the best practice tbh) also causes a similar error: Fatal error: Nesting level too deep - recursive dependency? in /var/www/vendor/twig/twig/src/Extension/CoreExtension.php on line 1023The best way to prevent this is by simply not using
items|sortinside a loop (if needed, just sort your items first and then loop over them).Comment #8
miwayha commentedWe got the error with sorting. For us, the resolution was to use an arrow function described in https://twig.symfony.com/doc/3.x/filters/sort.html. Just using `|sort` might not work if you're rendering field items. I don't fully understand the issue, but that resolution worked for us.
Comment #9
paramnida commentedWe also saw this with the reduce filter. It looks like any filter that is doing too much processing needs to be switched so that it happens in the code side instead of the theme side.