I apologize if this is a no-brainer question...none of my searches have found answers that are on-point. I am trying to use fields from a fetched entity as a token in a direct input field of a rule action.

The rule I'm constructing is an email rule that emails license keys to customers who have made online purchases. I have a license key table that is related to orders, and I have defined that table as an entity through the entity API.

When the purchase event triggers, I have specified an "entity exists by property condition." The condition makes sure that there is an instance of my custom entity that's related to the order id in question.

Two actions result when that condition is met. First there's a "fetch entity by property." I fetch my custom entity by the related orderid value. Second, there's a "send mail" action. In the body of that email, I would like to include a token representing a field value of the fetched entity. I can do this if I set the email body to "data selector." The selector allows gives me the option [entity-fetched:0:desired_field]. This produces an email with a body that contains the data from the desired field.

Now, here's the issue. In an email body, you never just want one piece of data. You want a direct input scenario, where you can enter message text and tokens for variable fields. When I switch from data selector to direct input, and try to use [entity-fetched:0:desired_field] as a token, the resulting email does not interpret it as a token. It merely shows me the literal text "[entity-fetched:0:desired_field]".

It feels like I am so frustratingly close, and this must be a common problem. But I cannot find any answers on the issue. What am I doing wrong?

Comments

fenster’s picture

Title: Fetched entity fields accessible with data selector, but not direct input » Fetched entity fields not accessible as tokens
Project: Rules » Token
Version: 7.x-2.x-dev » 7.x-1.x-dev
Component: Documentation » Code
Dave Reid’s picture

Project: Token » Rules
Version: 7.x-1.x-dev » 7.x-2.x-dev
Component: Code » Documentation

I'm not sure I'll be able to help you better than the Rules maintainers, so I'll move this issue back into their queue.

mitchell’s picture

Title: Fetched entity fields not accessible as tokens » How to access tokens from fetched entity fields?
Component: Documentation » Provided Rules integration
Priority: Normal » Major
nmc’s picture

I believe #1336826: Chained object token replacements not working is related or a duplicate of this.

mitchell’s picture

Component: Provided Rules integration » Rules Core

Updated component.

jlporter’s picture

http://drupal.org/project/rules_conditional

Get this module and add an if condtional that checks if the field exists on the entity you fetched. Then it is magically available in the data selector. I had to delete my create entity action(php error when I just dragged into condtional). After creating a fresh create entity action within the if entity has field I was able to select that field.

It does however baffle me that fetch entity in rules doesn't fetch the whole entity(or at least doesn't expose it)

eminencehealthcare’s picture

Thanks jlporter, this helped me so much. I am also baffled on why fetch entity does not fetch all the fields associated with that entity automatically.

BassPlaya’s picture

Oh yeah! So cool! Cheers!
Just to give some extra info, you'll have to add the action 'inside' that conditional otherwise you won't see the field of that fetched entity.

miromarchi’s picture

#6 is working, thank you very much, I spent days on this. thanks

jetwodru’s picture

Agree, I'm totally crippled without Conditional Rules coz the concept of Rules goes far beyond my existing knowledges of programming logic in workflow. Conditional Rules empowers Rules to the greatest level of usability.

Mehrdad201’s picture

Thanks a lot Dear jlporter

Your mentioned module really helped me.

jweirather’s picture

+1 for #6: http://drupal.org/project/rules_conditional

Specifically working with fetched and created entities, which were not available in the standard "conditions" section. This worked exactly as I needed, adding "entity has fields" conditions in the "actions" section after fetching/creating the entities, which granted access to the values/fields that were otherwise unavailable.

jeroenwa’s picture