3

How can I fetch the complete main navigation menu via jsonapi?

I've tried:

curl --header 'Accept: application/vnd.api+json' https://cms.example.com/jsonapi/menu/menu/

and

 curl --header 'Accept: application/vnd.api+json' https://cms.example.com/jsonapi/menu/menu/232fc942-51f3-4478-8535-e5dc482a166e

where 232fc942-51f3-4478-8535-e5dc482a166e is the id of the main-navigation menu BUT only the first level nodes are returned:

{
  "data": {
    "type": "menu--menu",
    "id": "232fc942-51f3-4478-8535-e5dc482a166e",
    "attributes": {
      "label": "Main navigation"
    },
    "links": {
      "self": {
    "href": "https://cms.garanteasy.com/jsonapi/menu/menu/232fc942-51f3-4478-8535-e5dc482a166e"
      }
    }
  },
  "jsonapi": {
    "version": "1.0",
    "meta": {
      "links": {
    "self": {
      "href": "http://jsonapi.org/format/1.0/"
    }
      }
    }
  },
  "links": {
    "self": {
      "href": "https://cms.garanteasy.com/jsonapi/menu/menu/232fc942-51f3-4478-8535-e5dc482a166e"
    }
  }
}
1
  • 1
    It's confusing, but instead of looking for menus you need to look for menu_link_content. g.rocchini posted in issue #3012062, comment #6 a few days ago about this very thing. Unfortunately you need to filter on your own based on the menu name after getting all of the menu link content.
    – mradcliffe
    Nov 27, 2018 at 22:17

3 Answers 3

2

I did not get it to run with JSON:API, there are several long-standing issues...

However you can get it to run if you additionally install the REST Menu Tree module.

Be aware that this has several direct and indirect dependencies:

It has a direct dependency on Menu Normalizer and according to this issue you need to then activate that endpoint for example with the REST UI module - which in turn will activate the core REST module.

Then in your site activate the endpoint via the Rest UI in /admin/config/services/rest.

Afterwards you might also want to allow everyone to GET this endpoint via /admin/people/permissions#module-rest

And finally you get a JSON array in /entity/menu/main/tree. Or query a different menu via /entity/menu/{internal_menu_name}/tree.

2

It is possible to pull all the menu items using the JSON:API exclusively. The main "long standing issue" is that the links are not available to non admin users, which probably defeats the purpose for most users. There are a couple patches, however, that allow this:

https://www.drupal.org/project/drupal/issues/2915792

And to add the url property:

https://www.drupal.org/project/drupal/issues/3066751

This allows you to hit this endpoint:

/jsonapi/menu_link_content/your-menu-machine-name

And get a standard JSON:API result.

Key takeaway: The state of Drupal's headless menu fetching is abysmal. 😕

0

There is also the rest_menu_items module. This doesn't have any dependencies, the only thing you need to do is enable the endpoint. This also enables you to specify which values you want, and specify the depth of the tree.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.