Contentful logo

Contentful Community

Getting the root content entry for a given entry in the tree

We’ve organized our content such that there are a bunch of top level Page entries. Each page has the content for that page on the website. The structure varies depending on the page that it is in and there are a bunch of linked entries below the parent Page entry.

When I publish an entry somewhere in the tree, it fires a webhook and in that webook request the JSON for the single entry is included. I need to know the Pages that include the changed entry so that I can clear a cache for that page. For instance, if I change and publish a “Product Card” that is used on two pages, I would like to know what two pages contain the changed product card.

Is it possible to query all the top-level entries for a given child entry? I don’t see a way.

I think my solution may be to query all entries in our app of type “page” and then recursively walk them to see if the changed entry is in that tree.

4 Likes

I’m interested in hearing an answer to this as well. We’re going to run into the same issue.

If references were “bi-directional”, it seems that you’d simply be able to walk backwards up the tree to find all Pages.

We call this Incoming Links and it is on our product roadmap. We are working on it and plan to release this feature soon.

In the meantime, you can search for entries based on the value of a reference. For example, you could search for all pages which have a reference to a specific Product Card. See https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters/search-on-references for details.

There are some limitations though; the biggest one is that it only works on single reference fields, not multiple reference fields. Also it is only possible to search one level deep (e.g., if your model was like this: Page -> Product Group -> Product Card, you wouldn’t be able to directly find the pages which have a specific product card. It would take 2 api calls to get that).

We hear you though and are working hard to implement this as fast as we can!

2 Likes

Update!!! We’ve released a new set of API search parameters which filter entries that have a field linking to a given entry (links_to_entry) or asset (links_to_asset). Available under the /entries endpoint, these search parameters ease the process of navigating the content graph from children up to their parents in a single API call.

See these links for more info:

4 Likes

Is there any way I can find the root content in the graph from the children which is deep more than 2 levels?

Trying with the updated API to get links_to_asses it’s returning just an array of the immediate parent of the current child. Is there any way I can access the parent that is more than 1 level above in the content graph in just one API call?