-
Notifications
You must be signed in to change notification settings - Fork 10
Using Component Rules to create conditional sub‐rules
A Rules Component can be used to create a Rule (call it 'Rule A') that is acted upon in the Actions section. The Rules Component can have conditional flow so that 'Rule A' effectively has one or more conditions within the Action component. This is an alternative to using Conditional Rules to achieve this functionality and instead uses core Rules functionality.
For this record, I'll use what I was trying to achieve as an example. I use Content Access to grant/deny access to users by role. I have a field called field-content-visibility on a node of type Document. This is a term reference to 'Members' and 'Public'. When set to Members, I want the Document to be visible only to users with the role Member and when set to Public, to be visible to anyone (Anonymous). This can be summarised as:
if node:field-content-visibility is one of 'members only' then grant access to role Member revoke access for role Anonymous else grant access to Member and Anonymous
There will be two Rules Components in this case: one to act when 'Member' access is set and the other when 'Public' access is set.
The first thing to do is to create the components so that they can be added later into 'Rule A'.
Select the Components tab on the Rules admin page and select Add a new component.
- Select the Rule from the Component Plugin choices.
- Enter a name for the component (mine is called 'Content visible to members').
- Under Variables, set data type: Node; Label: node, machine name: node.
- Save changes
- Under Component Conditions:
- Add condition Entity has field:
- Data selector: node
- Field: field_content_visibility
- Save
- Add condition Data comparison:
- Data selector: node:field-content-visibility
- Operator: equals
- Data value: Members only
- Save
- Add condition Entity has field:
- Under Actions:
- Add action Grant access by role:
- Data selector: node
- Role-based access control settings: View any content: Member
- Save
- Add action Revoke access by role:
- Data selector: node
- Role-based access control settings: View any content: Anonymous
- Save
- Add action Grant access by role:
Select the Components tab on the Rules admin page and select Add a new component.
- Select the Rule from the Component Plugin choices.
- Enter a name for the component (mine is called 'Content visible to public').
- Under Variables:
- Data type: Node
- Label: node
- Machine name: node
- Save changes
- Under Component Conditions:
- Add condition Entity has field:
- Data selector: node
- Field: field_content_visibility
- Save
- Add condition Data comparison:
- Data selector: node:field-content-visibility
- Operator: equals
- Data value: Members only
- Tick the 'Negate' option (so this is equates to not visible to members only)
- Save
- Save changes
- Add condition Entity has field:
- Under Actions:
- Add action Grant access by role:
- Data selector: node
- Role-based access control settings: View any content: Anonymous + Member
- Save
- Save changes
- Add action Grant access by role:
This Rule applies when a new node is saved or an existing one is updated. It checks that the node has the field field_content_visibility and then applies Actions that are provided by the Components set up earlier.
- Add a new Rule and set
- Name: Content visibility changed
- React on event: Node - after saving new content
- Restrict by type: Document
- Save
- In the new Rule, add further settings:
- Add event
- React on event: Node - after updating existing content
- Restrict by type: Document
- Save
- Add condition:
- Entity has field
- Data selector: node
- Field: field_content_visibility
- Save
- Entity has field
- Add action:
- Components: Rule: Content visible to members
- Data selector: node
- Save
- Components: Rule: Content visible to anonymous
- Data selector: node
- Save
- Components: Rule: Content visible to members
- Add event
- Save changes to save the new Rule
With thanks to @argiepiano for taking me through this!