Accessing entity (content) data within rules conditions - odd behavior

2.4k views Asked by At

I am pretty new to drupal but since friend asked me to help him, I got into it a bit. I managed to get many things working but there is one issue I can't get my head around. Here is what I am trying to achieve:

There are going to be courses represented as OG group with Node entity. Each course is of content type "Course" (entity is of type Node) and has its price. In rules, I am trying to check if user has enough credit to join the course, thus triggering it on "Before saving a og membership" event. If user does not have sufficient funds, I want to abort OG membership save and throw some error.

Pretty straightforward but yet, there are some issues I didn't expect to arise. This is what I do/have:

  • each user has custom field called "field-credit", there is no problem with that since I can access it via site:current-user:field-credit any time and anywhere in rules
  • course is nothing else than just simple OG group with Node entity with custom content type of "Course", which has defined custom field "field-price" for storing course price

In Rules module (via UI) I making such rule:

  1. triggering on event "Before saving an og membership"
  2. Conditions are as follows (in that order):
    • Entity [og-membership:group:entity] is of type Node (ensuring I can access content type)
    • Content [og-membership:group:entity] is of type Course (ensuring I can access Course specific fields)
    • Data comparison: [site:current-user:field-credit] is lower than [og-membership:group:entity:field-price]

And here comes something I can't fully comprehend - After adding those conditions (Entity type to say I am working with Node, and Node content type to say I am working with Course), I can see all fields I need in data selector, thus Rules are supposed to be aware of them and use them. But in reality, they are not there! When I show message with [og-membership:group:entity:field-price] in it (which is still accessible via data selector), it does not show courses price, it acts like there is no such variable. I could do entity fetching in actions, but that would be of no use because fetched entity (content) wouldn't be seen in conditions and it is not even accessible via data selector for further use for example with "Calculate a value" action because it does not know its (entities) content type (however [fetched-entity:field-price] is translated to actual course price in shown message, it just isn't listed in data selector).

Can anyone tell, why is this happening or better, how to fix it? I don't want to write my own module for something, that can be achieved with rules right out of the box, but this behavior is bugging me for 2 days already.

System is running on Drupal 7 btw.

EDIT: In rule triggering on "After saving new content", everything is set as expected with checking Content type (Content [node] is of type Course), so I am pretty sure all fields for course are available.

1

There are 1 answers

0
Jan Tovarnak On

Reluctant as I was, I had to get this working with components. Component takes two parameters - Node entity (of type Course) and User entity. In component User credit and Course price are being compared. Everything (well, everything regarding this issue) works as intended, I just had to fetch the Course entity (by entity id) in rule action first and provide resulted variable to the component.