Problem/Motivation
I want to build a model with action: Set value in a list text field after creating new node depending on a avalue of another field.
Steps to reproduce
I wanted to test this first without setting the condition and took as action: `Entity: set field value`, set as fieldname `field_task_status` and the desired key value 'done'.
I tried to do it with the events 'Insert content entity' and with 'Create content entity'. With both events the value isn't set.
I assume I missed something. But what?
By the way: What is the difference between the events 'Insert content entity' and 'Create content entity'? When to use which event?
Proposed resolution
Remaining tasks
User interface changes
API changes
Data model changes
Comments
Comment #2
marco.bComment #3
jurgenhaasThe key is the event, that's more important than the action to begin with. Let's describe the entity related events briefly:
All these events (a.k.a. hooks) are from Drupal core, and they are described in all detail in the file
web/core/lib/Drupal/Core/Entity/entity.api.phpwhich may help you to get more background information, if required.Then, what you described with regard to setting a field value with an action, sounds exactly right. So, as soon as you've replaced the event to use
Presave entity, you should be good to go.Comment #4
marco.bThx for event clarification. It works and the value for `field_task_status` has been set to value 'done'.
And it works also with the condition 'Entity: compare field value'.
This is a well answered support request but in my case a have a special requirement:
I want to trigger the action depending on a referenced taxonomy term and use the modul taxonomy_machine_name, so that I can deploy the terms without change of ID.
When I set up the ECA-condition 'Entity: compare field value' with the term ID, e.g. `3`, it works fine but i want to use the term machine name, e.g. 'special' in the condition.
How can this be achieved? Do I need to set the 'Entity: load via reference' action first to get this property and compare with it? I tried to use the token [node:field_ref_category:entity:machine_name] as Field name but this isn't supported.
Comment #5
jurgenhaasI don't think taxonomy terms have a machine name. Vocabularies do, but not their terms. They only have an ID and a name.
You're right though, with the contrib module taxonomy_machine_name, you also get a token for the machine name, i.e.
[term:machine_name]. This can then be used in your case and the syntax should be[node:field_ref_category:machine_name], just removeentity:from your example above as[node:field_ref_category]should already represent the[term]entity.Comment #6
marco.bOk. I tried this out and it doen't work. Here is the condition how I have understood you:

Comment #7
jurgenhaasUnfortunately, that's not how this is supposed to work. This condition is meant to compare the value of a field with the "Expected field value" and it expects the "Field name" to be a string that just names a field of the current entity.
In your case, you want to compare the value of a token with a string value. For that, the condition "Compare two scalar values" should be working.
You could also build a message action into your model to see if the token
[node:field_ref_category:machine_name]properly resolved to the value you're looking for.Comment #8
marco.b@jurgenhaas
It's a good idea to test the token through a message action:
[node:field_ref_category] is properly resolved
[node:field_ref_category:machine_name] is not resolved.
I tend to use a view that displays the machine_name of the term to put it into the condition. Or can this be done easier?
Comment #9
jurgenhaasI don't see how that would make things easier or even possible. What we need to figure out is the proper token syntax for the value you're looking for.
Thanks to the token browser, I figured the following syntax to work (yes, the
entity:was indeed correct before):[node:field_tag:entity:machine_name][node:field_tags:1:entity:machine_name]where the number is the zero-based index of the item you're interested inI've used
nodeas the token name above, this needs to be whatever your entity is actually named.Attached, please find a simple demo model. It load the node with the ID 5 (you may want to adjust that in your installation) and outputs the machine name of the tag in
field_tagand the 2nd tag infield_tags. You can trigger that model withdrush eca:trigger:custom_event e1Comment #10
marco.bThanks again.
In my case it works with the token (single value):
[node:field_ref_category:entity:machine_name]and I can trigger the action 'NameEntity: set field value' with the condition "Compare two scalar values" comparing the above token with the defined machine name.
Comment #11
marco.b