Problem/Motivation

It's difficult to determine the Entity for a Menu Item, or the Menu Item for an entity.

Steps to reproduce

1. Install Drupal and JSON:API Menu Items module
2. Create Node with Menu link
3. Visit `/jsonapi/menu_items/{{menu_name}}` or `/jsonapi/node/{{type}}/{{uuid}}` and try to find link between items.

Proposed resolution

1. Use the JSON:API Hypermedia to make a link between the resources.
2. Create computed relationships between the resources.

Link types:
- menu_item: Node/Entity -> Menu Link Content
- related: Menu Link Content/JSON:API Menu Item -> Node/Entity
- menu_subtree: Node/Entity -> JSON:API Menu Items filtered by menu_item as parent
- menu_siblings: Node/Entity -> JSON:API Menu items filtered by menu_item parent as parent

Remaining tasks

1. Proof of concept approaches
2. Make a decision on correct way to move forward
3. Write code, tests and documentation

User interface changes

N/A

API changes

Unknown

Data model changes

N/A

Release notes snippet

Original report by kentr

I have a menu which contains links to nodes.

I want to get the JSON:API URLS for the nodes that the menu contains.

I don't see a path in the data to connect the dots.

Example

Here's the JSON for the menu item.

Edit: Removed.

That's a page content type.

The direct URL for the resource is https://example.com/jsonapi/node/page/70f51f4b1-a7d4-4f7d-9530-489a5e6db...

How can I determine the direct URL for the node from the menu item response?

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

kentr created an issue. See original summary.

kentr’s picture

Title: How to get node API url from menu item? » How to get node data from menu item?
deciphered’s picture

I personally use the Decoupled Router module to turn the link.uri into the JSON:API Resource data.

deciphered’s picture

Title: How to get node data from menu item? » Add link to related JSON:API resources.
Category: Support request » Feature request

However, I think this should become a feature request, because there's no reason that the Decoupled Router should be required.

Instead, links via the JSON:API Hypermedia module should be added to any related resources:
- Node for a Node link
- View / JSON:API Views for a View link
- etc

deciphered’s picture

Version: 1.1.1 » 1.2.x-dev
Component: Miscellaneous » Code
deciphered’s picture

Issue summary: View changes
realityloop’s picture

Is there any reason not to add relationships on a given menu entries json data, seems like a lot of queries in frontend could be drastically reduced if each menu entry contained a relationships for:

  1. parent menu item
  2. sibling menu items
  3. children menu items

let the discussion commence..

deciphered’s picture

Given that the data can be easily filtered out using the ?fields filter, I don't see that it should be an issue.

larowlan’s picture

What does the json api spec say for stuff like this?

deciphered’s picture

https://jsonapi.org/format/#document-resource-object-relationships

The most relevant statement I could see was this one:

A relationship object that represents a to-many relationship MAY also contain pagination links under the links member, as described below. Any pagination links in a relationship object MUST paginate the relationship data, not the related resources.

I couldn't find a clear separation of what should be considered a relationship and what should be considered a related resource link though.

realityloop’s picture

Status: Active » Reviewed & tested by the community

Seems this has been merged without being marked so here?

deciphered’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll

No, it does not appear that this has been merged, but the patch no longer appears to apply.

Need re-roll.

deciphered’s picture

Issue tags: -Needs reroll

I've rebased the feature branch against 1.2.x and all seems well.

I don't believe this feature is entirely finished, and could definitely do with some automated tests regardless, so leaving this @ Needs work for the moment.

othermachines’s picture

I'm setting up a decoupled Gatsby/Drupal site... Never mind, I went another route. :)

sanjay.maharjan made their first commit to this issue’s fork.

sanjay.maharjan’s picture

Hi @Deciphered ,
I have added a small change to your MR ( added accessCheck() on getQuery() method ) to make it compatible with D10.

Thank you
Sanjay