Contentful logo

Contentful Community

How to restrict entry to be referenced only once

Hi!

We are creating a hierarchical page structure to support our page hierarchy i.e. frontpage/secondary/tertiary. To do this we have a secondary page where tertiary pages are referenced. We would like to restrict one tertiaryPage content model entry to be reference only once in order to deduce the parent of each reference.

Is this doable?

We have a search parameter called links_to_entry which allows you to find which entries link to a given entry. See https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters/links-to-entry for details. Does that help?

Thank you @charlie - this is very useful information and it solves another issue i had a workaround to, but it doesn’t however fix the issue mentioned here.

I have a tertiaryPage instance which could be i.e “Tertiary page 1”. I need to restrict it to be referenced on secondaryPage (Secondary page 1) field: tertiaryPages (reference) only once. Say that if I have another secondaryPage (Secondary page 2). The “Tertiary page 1” could not be referenced any longer because it has been referenced in the “Secondary page 1” already.

I guess this could be done with a custom ui extension where the selected entry_id would be fetched with the link_to_entry in case it returns any items in an array? Unless you can come up with something else?

You got it! That the approach I would take. UI extension which checks for secondary pages that the links_to_entry doesn’t have any tertiary pages and let’s the user select one then inserts the reference.

I have commented on a post regarding native extension here: Source code of native widgets

Do you think that oob references extension would be possible to be retrieved somewhere? Or would there be some standard examples that would help me get cracking with this?

there isn’t a single API query that will get you secondary pages which are not linked to any tertiary page that I know of or can figure out. so you would need to get all entries of the secondary page content type then 1-by-1 query each one with the links_to_entry parameter and only include it in the available list if the links_to_entry returns

{
	"sys": {
		"type": "Array"
	},
	"total": 0,
	"skip": 0,
	"limit": 100,
	"items": []
}

Another way would be to get all the tertiary pages and extract out the linked secondary pages, then compare that array to the list of all secondary pages.

As far as the display of the resulting list, I don’t know of any open source resources we have to make it look like the native reference selectors, so I think you’d need to build your own.

Generally speaking, why do you want to set it up this way? The point of references is to allow the same content be referenced from multiple places; to be reused.

If you only want tertiary pages to be referenced from a single secondary page, why not include the fields from the tertiary page content type in the secondary page content type and get rid of the tertiary page content type?

Hey! I know this is an old question, but I wanted to raise this again. This is a very common use case, and your suggestion here doesn’t make sense. What if you have a secondary page that can have multiple tertiary pages but one tertiary page cannot be referenced by multiple secondary page? This is a very common use case, no? We are currently running at the same issue at the moment.