[3.x]: Using SORT filter errors with "Nesting level too deep - recursive dependency"
Closed this issue · 5 comments
What happened?
Description
Upgrading to Craft 3.9.13 introduced this bug.
Using the SORT filter is causing a "Nesting level too deep - recursive dependency?" error.
Steps to reproduce
My error is happening with this code:
{% set films = craft.entries.section("filmography").limit(null).orderBy("title").all %}
{% set years = craft.categories.group("filmographyYears").relatedTo(films).all() %}
{% for year in years | sort | reverse %}
{{ year.title }}
{% endfor %}
Expected behavior
I expect the code to sort the years in reverse order
Actual behavior
The code throws this error:
Nesting level too deep - recursive dependency?
Here is the TRACE:
\vendor\twig\twig\src\Extension\CoreExtension.php at line 942
} elseif (!\is_array($array)) {
throw new RuntimeError(sprintf('The sort filter only works with arrays or "Traversable", got "%s".', \gettype($array)));
}
if (null !== $arrow) {
twig_check_arrow_in_sandbox($env, $arrow, 'sort', 'filter');
uasort($array, $arrow);
} else {
asort($array);
}
return $array;
}
Craft CMS version
3.9.13
PHP version
8.1.25
Operating system and version
Linux 5.14.0
Database type and version
MariaDB 10.5.22
Image driver and version
GD 8.1.25
Installed plugins and versions
Hey Anna!
Usually that error occurs only if Xdebug is enabled. Sometimes it occurs for actual infinite recursion bugs, but not always.
Can you try loading the same page with Xdebug disabled, and see if it works?
Thank you for your quick reply Brandon!
I'm seeing this error in both production and dev environments and neither have Xdebug enabled according to PHPinfo report.
This was working code that appears to have broken with the latest upgrade that I made to 3.9.13.
Assuming you are trying to sort the categories by their titles, try this:
{% for year in years|sort((a, b) => a.title <=> b.title)|reverse %}
Hi Brandon,
Thank you! This got the sorting working again.
The issue should be reproduceable with PHP 8.1.25 and Craft 3.9.13.
Glad to hear that solved it!