Problem/Motivation
In 10.5.x in (and possibly beyond) Drupal\Core\Menu\MenuTreeStorage::getRootPathIds(), there is the following code:
$result = current($subquery->execute()->fetchAll(\PDO::FETCH_ASSOC));
$ids = array_filter($result);
if ($ids) {If $result is FALSE or NULL however, array_filter throws a fatal error.
Here is our error for review and/or help for whatever poor soul finds themselves in the same boat as us:
Exception 'TypeError' with message 'array_filter(): Argument #1 ($array) must be of type array, false given' in /var/www/html/docroot/core/lib/Drupal/Core/Menu/MenuTreeStorage.php:770
in array_filter called at /var/www/html/docroot/core/lib/Drupal/Core/Menu/MenuTreeStorage.php (770)
in Drupal\Core\Menu\MenuTreeStorage::getRootPathIds called atLooking at that particular sanity check, it is at least as old as 2014, if not older since it may have been moved as part of some other coding effort. Perhaps a PHP version change or some other change resulted in it being more strict typed and throwing a fatal error now, whereas before it would return NULL or FALSE.
Steps to reproduce
No good ones, though a unit test could likely replicate the scenario.
We're encountering it with an odd batch of menu items being migrated into group content menu.
Proposed resolution
Replace:
$ids = array_filter($result);
With:
$ids = $result ? array_filter($result) : NULL;
Null and FALSE safe, goes no further, no big deal. Life goes on and the sanity check is finally reachable and then does it's job.
Remaining tasks
-Create a unit test demonstrating this works
-Somehow figure out steps to reproduce this incredibly odd combination
User interface changes
(none)
Introduced terminology
(none)
API changes
(none)
Data model changes
(none)
Release notes snippet
(TBD?)
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | 3580575-1-NullTolerantArrayFilterMenuTree.patch | 742 bytes | jnicola |
Issue fork drupal-3580575
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
Comment #2
jnicola commentedUploading a patch of what worked for us. Yep, it should be a PR. I doubt this will go anywhere though so it's mostly just for whatever poor soul finds themselves in our shoes.
Issue may exist somewhere in Drupal 11. Whomever is welcome to validate that if they want, I won't stop you!
Comment #3
jnicola commentedComment #4
quietone commentedHi, 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.
Comment #5
jnicola commentedUpdating to capture where our issue occurred.
Comment #6
quietone commentedThe menu module was renamed to menu_ui in 2014.
Comment #9
myoussef.linksystemz commentedConfirmed on main (d4d93a83).
current($subquery->execute()->fetchAll(...))returns FALSE when no row matches the ID, soarray_filter()throws a TypeError and theif ($ids)check is never reached.MenuTreeStorageInterface::getRootPathIds()documents that an empty array is returned for an ID that does not exist.I opened !18102 using the approach from the patch in #2, but returning
[]instead of NULL so$idsstays an array. Added a kernel test,MenuTreeStorageTest::testGetRootPathIdsMissingId(). It fails on main with the TypeError and passes with the fix. All Menu kernel tests pass (16 tests), and phpcs and PHPStan are clean.Comment #10
smustgrave commentedHave my same comment as from https://www.drupal.org/project/drupal/issues/3636809
But I did review this one and left a comment on the MR.
Comment #11
myoussef.linksystemz commentedDisclosed in #3636809: AI-assisted, reviewed and tested by me.