drupal-eca-recipe / eca_lib_0005
Compares a user's network references before and after a save to detect exactly which ones were added or removed, and reports each change.
Package info
gitlab.lakedrops.com/drupal/recipes/eca_lib_0005
Type:drupal-recipe
pkg:composer/drupal-eca-recipe/eca_lib_0005
Requires
- drupal/eca: *
- drupal/eca_tamper: *
- drupal/modeler_api: *
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
ID: eca_lib_0005
Users have references to a number of networks and when one gets added or deleted, this model recognizes the exact change and displays different messages about it.
For further reading, please go to this tutorial.
How the comparison works, and why the quotes matter
Both lists are encoded to JSON with "Tamper: encode", which produces the target IDs of the referenced terms rather than their names:
["7","8"]
Each item is then tested for membership with a contains comparison against that string. The needle is the term ID wrapped in the same double quotes JSON puts around every value:
"[loaded-term:id]"
The quotes are not decoration. Without them, looking for 4 would also match inside 14, and the model would report an item as already present when it is not. With them, "4" can only match a whole value, because JSON separates values with ",".
Two habits are worth taking from this:
- Compare what the encoder actually emits. The list holds IDs, so the needle has to be an ID. A needle built from the term name can never match, and nothing reports the mismatch — every item simply reads as new, and the "already present" branch becomes unreachable.
- Give the needle a boundary. Substring matching against a serialized list is only safe when the needle includes the delimiters the format guarantees.
Reading the messages
The four messages use [loaded-term:label] rather than [loaded-term:name]. Both look plausible, but only label is provided by ECA itself for every content entity; name on a taxonomy term depends on a token that may not be present, and resolves to an empty string when it is not. Each message also enables token replacement, without which the message would print the token literally.
Installation
## Import recipe
composer require drupal-eca-recipe/eca_lib_0005
# Apply recipe with Drush (requires version 13 or later):
drush recipe ../recipes/eca_lib_0005
# Apply recipe without Drush:
cd web && php core/scripts/drupal recipe ../recipes/eca_lib_0005
# Rebuilding caches is optional, sometimes required:
drush cr