Add reference node based on views result

Created on 9 May 2025, 4 days ago

Description: I'm using the ECA module in Drupal 10 to automatically set an entity reference field (field_other_node) based on a Views query result. I have two content types, Type 1 and Type 2, both with:

field_common_term: Entity reference to a taxonomy term (Vocabulary: Common term).
field_other_node: Entity reference to the other content type (Type 1 references Type 2, Type 2 references Type 1).

Both content types are populated via an API (Feeds module). My goal is to set field_other_node on a saved node by finding a matching node of the other content type based on a Views query. The Views query uses a contextual filter on field_common_term to find a single matching node. (In my real use case, I use four contextual filters—taxonomy and date fields—to ensure a unique result, but I've simplified it here for clarity.)

Problem: When I use a Views query in ECA to fetch the matching node and store it in a token, the token appears to be set correctly (I can log the node ID or title). However, when I try to use this token to set the field_other_node entity reference field, it doesn't work—the field remains empty. The original entity and the Views query result tokens are properly set, but the reference field update fails.

Additional Information:

Drupal version: 10.4.6
ECA version: 2.1.7

I'm using the "Entity: set field value" action to set field_other_node with the token [node:nid].

I'm also attaching the ECA model export to this issue for reference.

Could you help identify why the Views query token is not working to set the entity reference field? Is there a specific token format or action configuration needed for Views results to work with entity reference fields? Any guidance or fixes would be appreciated!

Thank you for your support!

💬 Support request
Status

Active

Version

2.1

Component

Documentation

Created by

🇺🇿Uzbekistan biarr

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

Comments & Activities

  • Issue created by @biarr
  • 🇩🇪Germany jurgenhaas Gottmadingen

    Triggering the custom event forwards 2 entities: the implicit entity from the views loop, and the explicit originalentity. So, those are the 2 entities that are known to your custom event and its succeeding actions.

    When you use the "Set field value" action, you're using a token named node, that doesn't exist. Also, in that action, you're not telling which entity should get the field set. So, at the very bottom, you need to tell the action that you want to use originalentity to be used as that entity. Thinking again, maybe you want to set the field in entity? Then you can provide that token name but you don't have to, because it's the default.

    Last but not least, if setting the field value, that's not enough. You also need to save the entity. You can either enable that in that action or add another one to just save the entity.

    Now, your 2 starting events are "insert" and "pre-save". For a new entity, that will trigger twice. In the labels, you're using "New" and "Update", but that's not what your events are doing. The "insert" event is what you get for "New", but the "pre-save" event triggers every time an entity gets saved.

    And that leads to yet another problem: recursion. You may want to build some prevention into your model so that it doesn't try to run in circles.

  • 🇺🇿Uzbekistan biarr

    Hi @jurgenhaas, thanks a lot for your detailed explanation—it's very helpful!

    It's especially important for me to know that the entity forwarded from the Views result is called `entity`. That clarification alone clears up a lot!

    My main goal is to update a reference field in `originalentity` with the entity selected in the Views result.

    Here’s what I’ve tried:

    • If I set the field value directly in the first event (without calling the custom event), it works perfectly—the reference is saved.
    • But when I move the logic into a Custom event, it stops working entirely.
    • I even hardcoded the reference using a known NID (9970), but still no effect.
    • I’ve tried both enabling "Save entity" in the "Set field value" action and adding a separate "Entity: Save" action afterwards—still no success.
    • I simplified things by keeping only the Presave event as the trigger.

    Despite all this, the custom event just doesn’t update the field in `originalentity`. Could it be that the context or saving logic is not correctly bound inside the custom event? Or maybe there's an issue with recursion or how the event chain is executed?

    At this point, I wonder—could this be a bug or am I still missing some very simple thing?

    Thanks again for your help, and I’d appreciate any further thoughts or ideas!

  • 🇺🇿Uzbekistan biarr

    Just a quick update—I ran further tests by trying to set a simple integer field in the Custom event, using originalentity in the "Entity to operate on" section at the very bottom of the action.

    This action uses only the originalentity token directly, without any other tokens or logic.

    Still, the model doesn’t update any value at all.

    At this point, I genuinely don’t understand what I might be doing wrong. Even this minimal case fails, so I feel like I'm either missing a critical detail—or something isn’t functioning as expected.

    Any insight would be very much appreciated!

  • 🇩🇪Germany jurgenhaas Gottmadingen

    Have you tried the debugging which is described at Debugging? This will tell you what's going on, e.g. if there's an access issue or tokens don't have the expected content.

  • 🇺🇿Uzbekistan biarr

    Thank you with this debug hint. I found the issue — my fault after all. Debugging revealed it was an access problem.

    I added an explicit "Switch user" action at the beginning of the model to run as user 1, and after that, the model finally started saving the reference correctly.

    What’s interesting is that I was already running the model as user 1, and I had even selected user 1 in the "Execute models with user" setting. But that alone wasn't enough.

    Only after explicitly adding the Switch user action did it actually work.

    A bit unexpected, but at least I know where the problem was now. Hope this helps someone else too.

  • 🇺🇿Uzbekistan biarr

    Making this as fixed.

  • 🇩🇪Germany jurgenhaas Gottmadingen

    Thanks for your feedback, great this is now resolved.

Production build 0.71.5 2024