From c739b1375f27136e911209281441fc9339b6bf9d Mon Sep 17 00:00:00 2001 From: Alec Smrekar <alec.smrekar@gmail.com> Date: Thu, 6 Jul 2023 08:39:06 +0200 Subject: [PATCH] Add support for 10.1 location structure --- src/RemoteMenuParser.php | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/RemoteMenuParser.php b/src/RemoteMenuParser.php index a10d585..b757e4b 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); + } + } -- GitLab