entity A is related to entity B, when A is deleted B should also be deleted. This scales for larger groups of entities in a relation. Additional conditions might be waiting for an entity to be "orphaned" (aka there are no relations pointing to it).

Suggested to integrate with rules, which I will look at next. I got the basic structure that avoids infinite loops and has the relevant context.

CommentFileSizeAuthor
#1 1282706-delete-related.patch1.1 KBboombatower

Comments

boombatower’s picture

Assigned: Unassigned » boombatower
Status: Active » Needs review
StatusFileSize
new1.1 KB
Letharion’s picture

Assigned: boombatower » Letharion

Assigning it to myself so I can provide the rules support.

naught101’s picture

Status: Needs review » Needs work

Why is this in relation_get_available_types()?? This definitely should not be the default behaviour...

dixon_’s picture

Status: Needs work » Needs review

I think this is a use case that describes the need for a broader Rules integration. I think the Rules integration should work something like this:

On any event where an entity object is available, we should be able to load all related entities, or just entities related through a specific relation type, into a Rules list (array). That's it! Because when we have all those entities in that Rules list, we can loop over them and run any action on them.

The pseudo Rules-configuration for deleting all related entities would be:

---

Event
Entity is deleted

Condition
Entity is of type Foo

Action 1
Load all entities related through the relation type Bar
This action provides a Rules list called related_enties (configurable)

Action 2
Loop over list related_entities

Action 3 (on each item in the list)
Delete entity

---

And the only thing missing is the "Load all entities" action.

dixon_’s picture

Cross posted

boombatower’s picture

Status: Needs review » Needs work

#3: was stub code

#4: Seems like a much better approach (never done rules stuff, so was looking to just solve my usecase).

Letharion’s picture

In London I wrote the code dixon_ suggest in #4. I'm working on a proper test, and will push it then :)

boombatower’s picture

Title: Provide method for deleting related entities when a entity is deleted » Provide rules integration for loading related entities and responding to events

Looking forward to seeing the code. Assuming this allows you to do more then just react to delete action then.

boombatower’s picture

Closing related issue as I would guess most of it will be covered by this #1260262: More Rules actions!. If not there are some thoughts.

itangalo’s picture

Subscribing. Cool stuff.

Letharion’s picture

I opened #1293134: Rules actions with entity argument get their entity arguments unwrapped in the rules queue. It should probably be moved to entity.
The new rules action receives it's source entity unwrapped, meaning there is no way to determine it's entity type. I could have done something wrong, but I believe it's an oversight in the entity api.

mrfelton’s picture

Subscribing. Looking for a way to maintain relationships as entities change. So if you have two entities that are related, and one of them gets updated, you can detect this with rules, delete the relationship then recreate/update it to ensure it's still applicable/accurate

MeDAN’s picture

Subscribing.

itangalo’s picture

Talking to chx on IRC now, and he's working on an action for loading all entities related with a relation type X – the action described in #4.

Just for information.

prodigeek’s picture

Any update on this? I'm eager for a way to delete relations using rules based on one endpoint (specifically, when a user unflag's a node, the relation between the user and that node is deleted).

Thanks so much for the support.

acrazyanimal’s picture

Component: Code » API

I second the request for all the actions posted by Itangalo and found in #1260262: More Rules actions!.

Especially "4. Relation end points as tokens". I was just logging in to report this one as a bug since the endpoints token is made available, but doesn't seem to actually work at all. I assumed that I would at least be able to do a "data comparison" on the bundle type or a "Entity is of type" condition to reveal the endpoints:0 entity's properties like you can do on every other entity. I also thought that I might be able to check the types and pass the endpoints to a second rules component, but they don't validate that way as well. Alas, to my disappointment neither of these approaches work and I cannot imagine any other way of accessing the related entities' data. Should a new issue be opened for this or will it be covered by a solution here?

cheers.

acrazyanimal’s picture

Component: API » Entity API / Rules

Seems the 'code' component was removed from the list so my last comment changed it to api. I think this is more suiting...

mikran’s picture

The feature request in #1418646: API function to load endpoint entities might be somewhat related to this. If something similar gets added to the core it would be neat to have the option to load just one type of entities.

acrazyanimal’s picture

See #1361796: rules: load related entities, entity fields unavailable for a patch to "Loads related entities".

naught101’s picture

Is it just me, or is this issue extremely convoluted? Can someone following it please edit the original post and add a summary of WTF is going on? It might make more sense to split off separate requests into their own issues, so that they are easier to follow.

Re: the original post (delete A related to B when B is deleted), see #1715332: Delete endpoints together with source and #893750: Event: "Content is going to be deleted".

mikran’s picture

Issue summary: View changes
Status: Needs work » Closed (duplicate)

Yes this is probably duplicate of #893750: Event: "Content is going to be deleted" or not applicable anymore as new load related functions have also been added since this issue was first opened.