In a previous article I described how to use the EVA and Entity Reference modules to create a simple reviews system for the Article content type. To improve this system, it would be good if when an Article is deleted, that all the reviews that reference that Article are also deleted.
To do this we will use Rules, Conditional Rules, and Views Bulk Operations
The rules module allows site administrators to define conditionally executed actions based on occurring events (known as reactive or ECA rules). It's a replacement with more features for the trigger module in Drupal core.
Example use cases
- Build flexible content publishing workflows changes
- Send customized mails to notify your users about important events
- Create custom redirections, system messages, breadcrumbs, ...
- Altering variable or entity data depending on user role
- Executing custom php code after comment added
- Deleting entities that reference an entity that is being deleted
The Rules module reacts to Events and performs various actions when those events occur. Conditions can be applied to rules. Once an event triggers Rules, and the set conditions are met, Rules can perform various actions or loops of actions.
Rules components can be used to organize combinations of actions or conditions. For example, you may have many actions you wish to take after one type of event. You an event may invoke a rules action component which will contain several action rules which will fire when your event happens. This is handy because it allows you to organize and separate each action for simpler editing especially in the case of complicated conditions for executing an action.
Conditional Rules allows much more flexibility in the conditioning of rules actions. Adds If/Else, switch, and while structures to be placed in the actions area of a Rule
Views Bulk Operations
Views Bulk Operations creates an interface to perform actions on views query result entities. In Rules it adds an action to load a list of entity ids or objects which can be acted on by other rules actions.
Create VBO View to use in rule
- Create a new view showing content of type review, no page or other views displays.
- Add a field Bulk Operations: Content and at least one other field
- Settings for the Bulk Operation field, Under 'Selected Bulk Operations', Check the Delete item checkbox,
- Add contextual filter which is the entity reference field
- Pager settings: display all items
- Create new Rule React to event: After deleting content
- Add condition: ‘Content is of type’ and select Article
- Add action: Load a list of entity objects from VBO view
- Select New view that was created in steps 1‐4 and pass the node:nid (of deleted node) as the argument to the view
- Name the List Reviews List
- Add Loop, list to act on is Reviews List, can change name of current item is list (optional)
- Nested in loop add action Delete Entity, use [list‐item] or whatever you named the current list item