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

marco.b created an issue. See original summary.

marco.b’s picture

Issue summary: View changes
jurgenhaas’s picture

The key is the event, that's more important than the action to begin with. Let's describe the entity related events briefly:

  • Insert content entity: this is fired after a new entity was successfully saved to the database. If you use that and modify something in the entity, then you need to make sure that you also save the entity after the modification.
  • Create content entity: this event is fired when PHP creates an entity in memory. At that point, this is a PHP object living in memory and nobody knows what's going to happen with it later on. It seems very rarely used in ECA models.
  • Update content entity: similar to "Insert content entity" but only for existing entities after they have been saved, in contrast to new entities for the other one.
  • Presave content entity: this is fired when Drupal is about to save an entity, i.e. right at the beginning of that process and before anything got stored in the database. This is the most common event to be used in ECA models because it's perfect to modify an entity and leave it to Drupal, to store everything properly to the database, after the ECA modification has been completed.

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.php which 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.

marco.b’s picture

Thx 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.

jurgenhaas’s picture

I 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 remove entity: from your example above as [node:field_ref_category] should already represent the [term] entity.

marco.b’s picture

StatusFileSize
new46.63 KB

Ok. I tried this out and it doen't work. Here is the condition how I have understood you:
token term_machine_name-in-condition.png

jurgenhaas’s picture

Unfortunately, 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.

marco.b’s picture

@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?

jurgenhaas’s picture

StatusFileSize
new2.73 KB

I 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):

  • For a single value tag field: [node:field_tag:entity:machine_name]
  • For a multi value tags field: [node:field_tags:1:entity:machine_name] where the number is the zero-based index of the item you're interested in

I've used node as 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_tag and the 2nd tag in field_tags. You can trigger that model with drush eca:trigger:custom_event e1

marco.b’s picture

Thanks 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.

marco.b’s picture

Title: How to set value in a list text field after creating new node depending on a avalue of another field » How to set value in a list text field after creating a new node depending on a a value of another field
Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.