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 at

Looking 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?)

Issue fork drupal-3580575

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

jnicola created an issue. See original summary.

jnicola’s picture

Uploading 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!

jnicola’s picture

Issue summary: View changes
quietone’s picture

Title: Sanity check in MenuTreeStorage::getRootPathIds is unreachable and will cause fatal error before then. » Sanity check in MenuTreeStorage::getRootPathIds is unreachable and will cause fatal error before then
Version: 10.5.x-dev » main

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.

jnicola’s picture

Issue summary: View changes

Updating to capture where our issue occurred.

quietone’s picture

Component: menu.module » menu_ui.module

The menu module was renamed to menu_ui in 2014.

myoussef.linksystemz made their first commit to this issue’s fork.

myoussef.linksystemz’s picture

Status: Active » Needs review

Confirmed on main (d4d93a83). current($subquery->execute()->fetchAll(...)) returns FALSE when no row matches the ID, so array_filter() throws a TypeError and the if ($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 $ids stays 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.

smustgrave’s picture

Status: Needs review » Needs work

Have 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.

myoussef.linksystemz’s picture

Disclosed in #3636809: AI-assisted, reviewed and tested by me.