diff --git a/src/RemoteMenuParser.php b/src/RemoteMenuParser.php index a10d585fd76035b39f281dc841b4ab17b9d28e20..b757e4b844805bf8b09c482e72bf54ab6987e172 100644 --- a/src/RemoteMenuParser.php +++ b/src/RemoteMenuParser.php @@ -84,7 +84,8 @@ class RemoteMenuParser { $data = []; // Convert a flat JSON into a tree array. foreach ($json_parsed->linkset[0]->item as $item) { - $data = static::insertLink($item->{'drupal-menu-hierarchy'}[0], $item->href, $item->title, $data); + $location = static::parseLocation($item); + $data = static::insertLink($location, $item->href, $item->title, $data); } // Convert the tree array into a tree of RemoteMenuLink objects. $base_url = parse_url($url, PHP_URL_SCHEME) . '://' . parse_url($url, PHP_URL_HOST); @@ -145,7 +146,7 @@ class RemoteMenuParser { */ protected static function insertLink(string $location, string $link, string $title, array $data): array { $pointer = &$data; - $fragments = array_filter(explode('.', $location)); + $fragments = array_filter(explode('.', $location), 'strlen'); // Traverse the tree. while (TRUE) { if (count($fragments) === 1) { @@ -165,4 +166,24 @@ class RemoteMenuParser { return $data; } + /** + * Parses the location string. + * + * @param object $item + * The menu item object. + * + * @return string + * The location string. + */ + protected static function parseLocation(object $item): string { + if (property_exists($item, 'drupal-menu-hierarchy')) { + // This is the behaviour in contrib module decoupled_menus. + return $item->{'drupal-menu-hierarchy'}[0]; + } + // This is the behaviour in 10.1 and onwards. + assert(property_exists($item, 'hierarchy')); + $hierarchy_array = $item->hierarchy; + return '.' . implode('.', $hierarchy_array); + } + }