Score:2

Why does “ Nesting level too deep - recursive dependency?” occur in a compiled template?

pk flag

After upgrading from 9.4.8 to 9.5.3 a problem came up (see below) so when I add more than one image in articles media field node-image, I get a 500 error.

PHP Fatal error:  Nesting level too deep - recursive dependency? in /home/www/sites1/mysite.gr/astrosnews/web/sites/default/files/php/twig/63dee130963f5_field--field-node-image.h_OFwaH9BedIr0dxUMjz2WTf6If/M-GDNVdt5tDdsesr-7jYR7Xyr_xWOPal71qHp-7K3yk.php on line 89

The line 89 mentioned above is the below but is not produced by theme code.

if (((($__internal_compile_2 = ($context["items"] ?? null)) && is_array($__internal_compile_2) || $__internal_compile_2 instanceof ArrayAccess ? ($__internal_compile_2[0] ?? null) : null) != $context["item"])) {

Do you have any idea how to solve this?

Update

code which might be the source of the problem although it is not changed

  {% if items|length > 1 and view_mode == "full" %}
    <div class="image-listing-items clearfix">
      {% for item in items %}
        {% if items[0] != item %}
          <div class="image-listing-item">
            <div{{ item.attributes.addClass('field__item') }}>{{ item.content }}</div>
          </div>
        {% endif %}
      {% endfor %}
    </div>
  {% endif %}
4uk4 avatar
cn flag
This is theme code, the Twig engine compiles Twig to PHP. You can force recompiling by clearing the Twig PHP cache `drush ev "\Drupal::service('twig')->invalidate();"`. If this doesn't help then check the template `field--field-node-image` for anything unusual.
isin avatar
pk flag
Clearing caches didn't help and there were no changes in theme and template field--field-node-image.twig The code added in the original post seems to be the source althought nothing changed for years.
Score:3
cn flag

This code line is expensive to calculate and could even cause a fatal error when the arrays/objects are very large:

{% if items[0] != item %}

Instead, use the Twig loop variable:

{% if not loop.first %}
isin avatar
pk flag
Thank you @4uk4! The change you proposed in code worked! I am using a paid theme that is not updated anymore. Even if the items where only 2 or 3, when saving article I got http 500.
4uk4 avatar
cn flag
item(s) contains render arrays for the field content. Such render arrays change often by updating or installing and configuring additional modules. You've reached a point where the level of nesting simply was too much or somehow the render array built a recursive loop, which only causes problems when trying to traverse the array and the objects included to compare it,
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.