TransWikia.com

How to check if an entity is referenced before deleting?

Drupal Answers Asked by HaloFX on December 2, 2021

Is there a module or a way to use Rules to make sure that a parent node isn’t referenced by a child node before deleting the parent?

In my case the children require a parent to be created. However if the parent gets deleted, the children all get screwed up.

2 Answers

You can use $entity->referencedEntities() to get a list of all entities referenced by $entity. So if you have a list of child nodes, you call referencedEntities() on each of them and see if you find the parent node.

Answered by Stefan on December 2, 2021

(Entity)Reference Field Synchronization module should be able to help:

Having 2 Entity reference fields pointing back at eachothers entities, they can be set to be updated so that if an entity A with field 1 references to an entity B with field 2, entity B automatically points to entity A.

This is very helpful when you need to be able to reference from any entity, and not just be able to perform back reference queries (as provided by Entity reference).

Just add an synchronized reference field to a parent, and on delete test if it has values or not.

Also, see this feature request in Entity reference issue queue.

Answered by Mołot on December 2, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP