diff --git a/src/RemoteMenuParser.php b/src/RemoteMenuParser.php index 21fd68f8e284607f25a08d7aebd8c4695e4b073e..bbf55717dca0717e0f3a7c052868096ce2b2a19b 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. *