- Issue created by @camilo.escobar
- πΊπΈUnited States kevinquillen
+1 to this, would you be willing to share the code in a fork?
- π¦π·Argentina leofishman
I just been asked for that functionality from the frontenders, can you share your solution?
- First commit to issue fork.
- Merge request !21Issue #3447727: Add JSON API Menu Tree submodule with resource for hierarchical menu structure β (Open) created by camilo.escobar
- π¨π΄Colombia camilo.escobar
camilo.escobar β changed the visibility of the branch 3447727-introducing-a-new to hidden.
- π¨π΄Colombia camilo.escobar
Iβve created a new merge request introducing the following:
A new submodule
jsonapi_menu_items_tree
, which provides a new JSON:API resource/jsonapi/menu_items_tree/{menu}
(MenuTreeResource.php
).This resource extends the original one provided by the parent module, leveraging its injected services and existing methods for handling filters, retrieving resource types and other internal logic. The only overridden method is
getMenuItems()
, which has been rewritten to return a tree-structured response.Pending:
Add support for fields added via Menu Item Extras. This was implemented in the original resource as of version 1.2.5.
Iβm currently analyzing how to properly handle entity reference fields and the best way to support includes within the new tree-like structure. - π¨π΄Colombia camilo.escobar
Update:
Setting the merge request to Draft.
I'm currently working on the following updates:
- Add support for fields provided by Menu Item Extras, leveraging the existing implementation in the main module.
- Integration with JSON:API Include, so that entity reference fields (includes) are added directly to the top-level resource. It makes the tree-structured response easier to consume by providing direct access to the related data.
- π¬π§United Kingdom unqunq
I only needed the text version of this and it works pretty well.
- Status changed to Needs review
25 days ago 6:28pm 6 August 2025 - π¨π΄Colombia camilo.escobar
I've worked on the points below, and the merge request is now marked as Ready. I invite maintainers to review and share their feedback.
Menu Items Tree Resource refactored to leverage
MenuItemsResource.php
The new resource
/jsonapi/menu_items_tree/{menu}
now uses the same resource class as the default resource provided by the parent module (Drupal\jsonapi_menu_items\Resource\MenuItemsResource
). This approach leverages existing functionality without duplicating code. Initially, it will rely on the flat array of menu items generated for the default resource, which will then be transformed into a nested tree structure by a new response subscriber ResponseSubscriber.php.This ensures the tree resource remains consistent with the logic built for the default resource and continues to benefit from any future updates made to that resource.
Verified compatibility with JSON:API Include
I have also tested compatibility with the JSON:API Include β module, and it works as expected. For example, if you're using the Menu Item Extras β module to add fields to your menus, such as a taxonomy reference field field_tags, you can display the referenced taxonomy terms directly in the data instead of under the "included" key. This is exactly what the JSON:API Include module is designed for, and that functionality continues to work properly even within the nested tree menu structure. For instance, suppose you have a menu item "Technology" under the parent "News", and the "Technology" item references several taxonomy terms via
field_tags
. Requesting/jsonapi/menu_items_tree/main?include=field_tags
will yield a structure like this:{ "type": "menu_link_content--main", "id": "menu_link_content:A", "title": "News", "url": "/news", "other fields"... "children": [ { "type": "menu_link_content--main", "id": "menu_link_content:B", "title": "Technology", "url": "/news/technology", "field_tags": [ { "type": "taxonomy_term--tags", "id": "ec5810abcded", "drupal_internal__tid": 4, "drupal_internal__revision_id": 4, "status": true, "name": "Space Travel", "changed": "2025-08-11T04:43:49+00:00", "other fields"...