Prevent referenced content from being deleted

I would like to prevent a content from beeing deleted if it is referenced by other contents.

For example, if I have an “article” that references an “author”, this author cannot be deleted.

I checked the ContentDto in the delete method of the ContentsPageComponent, but it seems there is no field that can help me to know if the content beeing deleted is referenced.

Is there a current way to do this without many customizations in the Squidex core?

No, right now there is nothing. But there are plans to add a sidebar to a content item to show all contents that reference this item and then we can also implement this feature.

1 Like

Hello, Sebastian.

I saw that there is a GetReferencedIds method, but it does not return the ids that are beeing referenced inside an array field that contains a reference field.

I tried to implement a new method including those ids, but I faced some problems using this method in a query.

So, is there a current way to do this? That is, return all ids referenced by a content, including those ones inside array fields?

Thank you.

Hi, are you sure. That sounds like a bug then.

1 Like

There are tests for that: https://github.com/Squidex/squidex/blob/master/backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/ExtractReferenceIds/ReferenceExtractionTests.cs

Thank you, Sebastian. I’ll check it now.

I found that the problem was in the method Visit(IArrayField field). The TryGetValue(nestedField.Name, out var nestedValue) was always returning false, because the keys in the object was the fields’ ids, not the names.

If we change it to (or add) item.TryGetValue(nestedField.Id.ToString(), out var nestedValue), it works and the method then returns all referenced ids, including those ones inside array fields.

Depends where we use the code.

When the content is stored int the DB all names are converted to Ids, but otherwise we always use the names.