How to pass entity token through Views query and on to triggered Custom Event?

Created on 7 February 2025, 23 days ago

Problem/Motivation

My intended use case for the ECA Module on Drupal 10.3 is this: I have a custom Content Type -- let's call it "X" -- and I want to send a message to all users who have a specific role whenever a new Content Item of Content Type X is created, providing details from the new Content Item in the email.

I read the blogpost "Send email to users of a given role" and watched the video for a use case, which is somewhat similar, here: https://ecaguide.org/library/simple/send_email_to_users_of_a_given_role/

I would expect that a solution, would work similarly, namely: (1) I would have one Event triggered by the insertion of a new X Custom Item, which would execute a View resulting in a list of users with the given role, and that that would trigger a Custom Event that would pass along the X Custom Item to the users, and (2) I would have an Event "listener" for that Custom Event that would receive the user record as well as the new Content Item.

I don't see how both the record for a user (resulting from executing the View) and the X Content Item can be passed as parameter "tokens" to the Custom Event listener so that an email could be produced.

The "List users" action is Views:Execute Query and the "Name of token list:" parameter (resulting from the execution of the View) is "userlist". There is no parameter to save or pass the X Content Item that was created.

The Action node labelled "Trigger X1" takes a token name given as "userlist". Has the originating X Content Item already been lost at this point?

In the video, JΓΌrgen Haas says that the Custom Event listener (the circle labelled "X1" in the diagram) always gets the associated token under the name "entity." This sole token "entity," however, is the user record (the list is iterated, one user record at a time). I don't see any ability to pass multiple parameters (such as listing multiple entity names), so that the second item -- the new X Content Item that triggered the initial Action -- can be passed as a parameter.

Is there any way to do this?

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

πŸ’¬ Support request
Status

Active

Version

2.1

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States gaelicmichael

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @gaelicmichael
  • πŸ‡©πŸ‡ͺGermany jurgenhaas Gottmadingen

    The "List users" action provides a list of entities. That's the list of users in your case. If such a list action is followed by an Trigger a custom event (entity-aware) action, it will trigger that custom event once for every entity in that list and forward that entity (i.e. the user) to that event. The token name is entity for the receiving event.

    In addition to that, you want to forward the new node to that custom event as well. You can provide the token name node in the field "Tokens to forward" in the trigger custom event action. Just provide the token name, without brackets.

    With that, the custom event X1 receives 2 tokens: entity contains the user, and node contains the node entity.

    You can then use those 2 tokens to send the email containing values from those tokens.

  • πŸ‡ΊπŸ‡ΈUnited States gaelicmichael

    Thanks very much for your response. I'll try this and let you know if it works -- although I may need to ask follow-up questions.

  • πŸ‡©πŸ‡ͺGermany jurgenhaas Gottmadingen

    That's great @gaelicmichael

    Please set the issue status to fixed when this is working.

    Follow-up questions are often worth a new issue, not only such that other users can easier find them when they have similar ones.

  • πŸ‡ΊπŸ‡ΈUnited States gaelicmichael

    It's taken me a while to get to the point of trying to make this work, but I've followed guidance as best as I could, and I can't get it working.

    I know that the Event and Conditions are triggering, because if I just send a single email to myself, it works. It breaks down somewhere thereafter, however.

    Here is a list of the linear chain of Actions in the initial sequence, as well as the Event Listener (marked with a '+') that should be triggered by the Custom Event. What am I missing?

    ...
    - ACTION - USER: SWITCH CURRENT ACCOUNT "Switch to User 1"
    - User ID: 1
    - ACTION - VIEWS: EXECUTE QUERY "List Pubs Committee Members"
    - Name of token: userlist
    - View: People
    - Display: view_users_pubs_committee
    - ACTION - TRIGGER A CUSTOM EVENT (ENTITY) "Trigger Event ..."
    - Event ID: app_email_pubs_committee
    - Tokens to forward: node
    - Entity: userlist

    + EVENT: ECA CUSTOM EVENT (Entity-aware) "Listen to email Trigger"
    - Event ID: app_email_pubs_committee
    - ACTION: SEND MAIL "Email Committee Member"
    - Recipient email address: [entity:mail]
    - Subject: HCHS / SOL Pubs Cmte email review for the abstract related to [node:field_ab_abbr_title]
    - Replace tokens: yes
    - Message: ...

    I have ensured that the display ID view_users_pubs_committee is what is shown in the View configuration at Advanced > Other > Machine name.

  • πŸ‡©πŸ‡ͺGermany jurgenhaas Gottmadingen

    I guess this is the time where exporting the model and uploading it for review may be better for review and bug hunting.

  • πŸ‡ΊπŸ‡ΈUnited States gaelicmichael

    Sure -- here is the export file.

    FYI, there is an Action in the Custom Event trigger sequence that I've bypassed and thus should not be active.

  • πŸ‡©πŸ‡ͺGermany jurgenhaas Gottmadingen

    I'm confused how that filename eca-email-all-pubs-committee.tar.gz comes from. ECA is exporting as bpmn_io-process_wgaz9kq.tar.gz. But that's just an aside.

    The model itself has a simple mistake: the "Trigger Custom Event" action uses the Trigger a custom event action but the event is ECA custom event (entity-aware).

    Note, there are custom events, and there are entity aware custom events. The action to trigger the event should be of the same type.

    And then you should use the Debugging techniques, that will show you which tokens are available at which point while processing the model. If a token that you expect is missing, then you have some indication on what needs to be done to fix that.

  • πŸ‡ΊπŸ‡ΈUnited States gaelicmichael

    Thank you for looking at the model.

    I was unaware of different kinds of Custom Events. Where is "Entity-Aware" and the distinction between event types explained?

  • πŸ‡©πŸ‡ͺGermany jurgenhaas Gottmadingen

    Where is "Entity-Aware" and the distinction between Custom Event types explained?

    I don't know if it's explained anywhere. But there is ECA custom event (entity-aware) and there is ECA custom event. And for each of these there are action plugins to trigger those. It's likely that this was explained in one of the videos in ECA TV.

  • πŸ‡ΊπŸ‡ΈUnited States gaelicmichael

    I can confirm that changing the Action to be entity aware does indeed fix the problem. Thanks for pointing that out, although some clear explanation of this issue is needed on the website.

    There is one further issue on which I would appreciate guidance. After the Event listener gets triggered by the Custom Event, but before the email Action, I'd like to have an Action that does a LOAD VIA REFERENCE, so that I can access the fields in a Content Item referred to in the field_manuscript_number field in the token node (which I know is being passed properly to the Triggered action).

    I tried to configure the Action as follows, but it does not work. I assume that the Load entity from setting is the problem. Given that this is an Action that is getting fired asynchronously, and maybe not be in the same context (??), I assumed that the setting should be set to Defined by token, but that doesn't work. Neither does Current scope. What should the settings be?

    - ACTION - LOAD VIA REFERENCE "Load Biblio Item"
    - Name of token: biblio
    - Field name entity reference: field_manuscript_number
    - Load entity from: Defined by token
    - Entity type: Content
    - Load latest revision: yes
    - Entity: node

    Thank you

  • πŸ‡©πŸ‡ͺGermany jurgenhaas Gottmadingen

    although some clear explanation of this issue is needed on the website.

    You mean the ECA Guide? Contributions welcome.

    Load entity from: Defined by token

    Have you noticed the description below that field? I'm sure that settings is not what you want.

  • πŸ‡ΊπŸ‡ΈUnited States gaelicmichael

    Yes, I have seen and noticed 'When using the "Defined by token" option, make sure there is a token with this name: eca_token_load_entity_ref_from' .

    Is this what I need in principle but I need to rename the token node as eca_token_load_entity_ref_from ? Or do I need a different setting on the configuration of Load entity from? I don't see any information about this use case anywhere.

    The ECA module is a really useful and versatile tool for enhancing functionality, but like much in the Drupal world, in desperate need of well-written documentation. After I get the functionality of my prototypes working, I do intend on writing something up for other people trying to figure it out.

    Thank you

  • πŸ‡©πŸ‡ͺGermany jurgenhaas Gottmadingen

    I'm not sure that even more documentation would have avoided this conversation. Like for example, how could we make it more prominent that the token eca_token_load_entity_ref_from is required when using that option?

    Yes, we need more documentation and a few people are constantly working on the ECA Guide. If you have a demand for more, maybe you can fund more resources to make that happen more quickly.

    And no, the mode you need is not Defined by token. That mode means that a token defines which mode you want to use.

    Why don't you just load from the Current scope like you've done before?

  • πŸ‡ΊπŸ‡ΈUnited States gaelicmichael

    Because it doesn't work, it triggers the error indicated by this excerpt from the error log:

    Failed execution of Load Biblio item (Activity_1v2twte) from ECA Abstract / Presesentation Proposal: Pending Review (process_wgaz9kq) for event eca.content_entity.custom: Field field_manuscript_number does not exist for entity type node/hchs_abstract_presentation..\n\n#0 /var/www/html/testhchs/modules/contrib/eca/modules/content/src/Plugin/Action/LoadEntity.php(56): Drupal\eca_content\Plugin\Action\LoadEntityRef->doLoadEntity() #1 /var/www/html/testhchs/modules/contrib/eca/src/Entity/Objects/EcaAction.php(103): Drupal\eca_content\Plugin\Action\LoadEntity->access() #2 /var/www/html/testhchs/modules/contrib/eca/src/Processor.php(298): Drupal\eca\Entity\Objects\EcaAction->execute() #3

  • πŸ‡ΊπŸ‡ΈUnited States gaelicmichael
  • πŸ‡©πŸ‡ͺGermany jurgenhaas Gottmadingen
  • πŸ‡©πŸ‡ͺGermany jurgenhaas Gottmadingen
Production build 0.71.5 2024