Looping through user networks and take action on changes¶
Originally published on 23. October 2022 by Barry (btown)
Problem/Motivation¶
Even though it's really quite simple, it took me a while to understand how to setup a loop - I was looking for something more complex. I'm sharing my solution here in case it helps others.
On my site, user profiles have an entity reference field that points to taxonomy terms called "networks". A user can belong to an unlimited number of networks (currently there are about a dozen). When a user profile is updated, I wanted a model to check if there had been any changes to the networks the user had selected. If there have been changes, I wanted to take different actions based on additions and deletions.
For demonstration purposes, the end result displays a message for each addition, deletion or no change. The final product will be somewhat simplified - unnecessary actions will be removed as I don't care about no-changes, just additions and deletions. However, I included them as I was building my model to ensure I understood and checked each step.
Commentary¶
Sorry there are so many steps in my example. I wanted to provide an opportunity to review each event, condition and action so that details weren't missed.
Example with Explanation¶
1. Starting event: Update Content Entity¶
- Type (and bundle):
user:user
2. Action: Entity Load¶
- Name of token:
unchanged-user
- Load entity from:
current scope
(default value) - Entity type:
user
- Load unchanged values:
yes
3. Condition: Compare two scalar values¶
- First value:
[unchanged-user:field_user_networks]
- Comparison operator:
equals
- Second value:
[user:field_user_networks]
4. Action: Display a message to the user¶
- Message:
No changes to networks
5. Condition: Compare two scalar values¶
Same as step 3 but negated.
- First value:
[unchanged-user:field_user_networks]
- Comparison operator:
equals
- Second value:
[user:field_user_networks]
- Negate the condition:
Yes
6. Action: Entity - get field value¶
Get the new values:
- Field name:
field_user_networks
- Name of token:
new-networks-list
- Entity:
user
6a. Action: Tamper: Encode/Decode¶
Encode new list of networks for later comparison.
- Data:
[user:field_user_networks]
- Token name:
new-networks
- Serialization mode:
Json Encode
7. Action: Entity - get field value¶
Get the original values:
- Field name:
field_user_networks
- Name of token:
orig-networks-list
- Entity:
unchanged-user
7a. Action: Tamper: Encode/Decode¶
Encode original list of networks for later comparison.
- Data:
[unchanged-user:field_user_networks]
- Token name:
orig-networks
- Serialization mode:
Json Encode
8. Action: Trigger a custom event (entity aware)¶
Trigger event for additions:
- Event ID:
additions
- Tokens to forward:
orig-networks
- Entity:
new-networks-list
9. Action: Trigger a custom event (entity aware)¶
Trigger event for deletions:
- Event ID:
deletions
- Tokens to forward:
new-networks
* - Entity:
orig-networks-list
* - *** Note reversal from previous step**
10. Action: ECA Custom Event (entity-aware)¶
- Event ID:
additions
11. Action: Entity Load¶
This is the current item from the list.
- Name of token:
loaded-term
- Load entity from:
Current scope
(default value) - Entity type:
Taxonomy term
12. Condition: Compare two scalar values¶
Does the list of original networks contain the current item?
- First value:
[orig-networks]
- Comparison operator:
contains
- Second value:
>[loaded-term:name]<
- Negate the condition:
yes
13. Action: Display a message to the user¶
Show the new item's name.
- Message:
new item: [loaded-term:name]
14. Condition: Compare two scalar values¶
Same as step 12 but not negated.
- First value:
[orig-networks]
- Comparison operator:
contains
- Second value:
>[loaded-term:name]<
15. Action: Display a message to the user¶
Show the existing item's name.
- Message:
item exist already: [loaded-term:name]
16. Action: ECA Custom Event (entity-aware)¶
- Event Id:
deletions
17. Action: Entity Load¶
Same as step 11, but in a different pathway.
- Name of token:
loaded-term
- Load entity from:
Current scope
(default value) - Entity type:
Taxonomy term
18. Condition: compare two scalar values¶
Check if the new list of networks contains the current item:
- First value:
[new-networks]
- Comparison operator:
contains
- Second value:
>[loaded-term:name]<
- Negate the condition:
yes
19. Action: Display a message to the user¶
Show the deleted item's name.
- Message:
deleted item: [loaded-term:name]
20. Condition: Compare two scalar values¶
Same as step 18 but not negated.
- First value:
[new-networks]
- Comparison operator:
contains
- Second value:
>[loaded-term:name]<
21. Action: Display a message to the user¶
- Message:
item remains: [loaded-term:name]
22. Save and test your model!¶
- Save the model
- Edit a user
- Remove one item from their list of networks
- Add one item
- Save user
- Check the status message
Download¶
This ECA model is available for download.