From afa71f596cec4e2cd25b5b8125103ab8b5f883f1 Mon Sep 17 00:00:00 2001 From: Alec Smrekar <alec@smrekar.me> Date: Mon, 5 Aug 2024 15:31:13 +0200 Subject: [PATCH] Include links that have a non-default ref attribute --- src/RemoteMenuParser.php | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/RemoteMenuParser.php b/src/RemoteMenuParser.php index 21fd68f..bbf5571 100644 --- a/src/RemoteMenuParser.php +++ b/src/RemoteMenuParser.php @@ -98,7 +98,7 @@ class RemoteMenuParser { } $data = []; // Convert a flat JSON into a tree array. - foreach ($json_parsed->linkset[0]->item as $item) { + foreach ($this->flattenReceivedJson($json_parsed) as $item) { $location = static::parseLocation($item); $link_href = $item->href ?? ''; $link_title = $item->title ?? (string) $this->t('- Missing Link Title -'); @@ -126,6 +126,34 @@ class RemoteMenuParser { return $data; } + /** + * Extracts all menu links from the received JSON. + * + * @param \stdClass $json + * The JSON object. + * + * @return array<object> + * An array of link objects. + */ + protected function flattenReceivedJson(\stdClass $json): array { + $output = []; + foreach ($json->linkset[0] as $item) { + if (is_object($item)) { + $output[] = $item; + continue; + } + if (!is_array($item)) { + continue; + } + foreach ($item as $subitem) { + if (is_object($subitem)) { + $output[] = $subitem; + } + } + } + return $output; + } + /** * Converts the tree array into a tree of RemoteMenuLink objects. * -- GitLab