On my website, users can rate nodes via comments. The Fivestar field is called 'Stars'. I'd like to use the user's voting value in a rule that's activated when a new comment has been saved. (The rule should update a radioactivity field, called 'Popularity', from the node that's commented on.)
When debugging the 'Stars' field in a comment, I'm able to get exactly the value I need via field_stars['und'][0]['rating']
. However, I can't find a way to grab it in the Rules UI. I'm always ending up with comment:field-stars:
in an action.
I tried adding a condition 'Entity has field: Parameter: Entity: [comment:node], Field: field_stars'. This gave me the option to dig deeper to comment:field-stars:user-rating
, but when I'm using it further on in my rule, it always throws an error "The variable or parameter value is empty."
What's going wrong and how can I solve this?
Edit: After reading this question, I tried solving my problem with the following rule.
{ "rules_new_vote" : {
"LABEL" : "New vote",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules", "php", "radioactivity", "comment" ],
"ON" : { "comment_insert--comment_node_article" : { "bundle" : "comment_node_article" } },
"IF" : [
{ "entity_has_field" : { "entity" : [ "comment:node" ], "field" : "field_popularity" } }
],
"DO" : [
{ "variable_add" : {
"USING" : { "type" : "integer", "value" : "0" },
"PROVIDE" : { "variable_added" : { "null" : "null" } }
}
},
{ "php_eval" : { "code" : "$rating = \u0026drupal_static(\u0027rating\u0027, $default_value = $comment-\u003Efield_stars[\u0027und\u0027][0][\u0027rating\u0027]);" } },
{ "radioactivity_emit" : {
"data" : [ "comment:node:field-popularity" ],
"value" : { "select" : "null", "php" : { "code" : "return $rating;" } }
}
}
]
}
}
Unfortunately, it doesn't work properly: when saving a comment, the following error is thrown:
Notice: Undefined variable: rating in eval() (line 1 of C:\wamp\www\mysite\sites\all\modules\rules\modules\php.eval.inc(146) : eval()'d code).
How can I solve this?
return $rating;
, not the other code. Are you sure that those two code blocks have access to the same PHP variables?return
, but I don't know how I can solve it. I thought usingdrupal_static
would make the variable$rating
available later on in the rule, but apparently it doesn't.return drupal_static(...);
instead ofreturn $rating;
. It is not a problem withdrupal_static()
but with using a variable that is not set. In other words, the second block of code doesn't know about$rating
because it doesn't share PHP variables with the first block of code, where you calldrupal_static()
.return
part).drupal_static
. :)