2

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?

5
  • The error could be caused from return $rating;, not the other code. Are you sure that those two code blocks have access to the same PHP variables?
    – apaderno
    Jun 8, 2014 at 2:41
  • @kiamlaluno: Indeed, the error is caused by the return, but I don't know how I can solve it. I thought using drupal_static would make the variable $rating available later on in the rule, but apparently it doesn't.
    – Jeroen
    Jun 8, 2014 at 12:03
  • Use return drupal_static(...); instead of return $rating;. It is not a problem with drupal_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 call drupal_static().
    – apaderno
    Jun 8, 2014 at 15:58
  • See line #146 in php.eval.inc and rules_execute_php_eval() (the function calling the one causing the error you are seeing). It doesn't seem that the variables used in your first block of code are going to be shared with your second block of code (the return part).
    – apaderno
    Jun 8, 2014 at 16:04
  • @kiamlaluno: I've figured it out (at last!) without using drupal_static. :)
    – Jeroen
    Jun 10, 2014 at 15:37

1 Answer 1

2

I finally found a solution for my problem. This is my exported 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" : {
              "select" : "comment:cid",
              "php" : { "code" : "$comment = comment_load($value);\r\n$vote = $comment-\u003Efield_stars[\u0027und\u0027][0][\u0027rating\u0027\r\nif ($vote == 20) {return -4;}\ r\nif ($vote == 40) {return 2;}\r\nif ($vote == 60) {return 3;}\r\nif ($vote == 80) {return 4;}\r\nif ($vote == 100) {return 5;}" }
            }
          },
          "PROVIDE" : { "variable_added" : { "rating" : "Rating" } }
        }
      },
      { "radioactivity_emit" : { "data" : [ "comment:node:field-popularity" ], "value" : [ "rating" ] } }
    ]
  }
}

The trick was to store the cid as a new variable and to use that value in the PHP evaluation (the proposed values in my code can be changed to your needs). I've no idea why this wasn't possible in an easier way via the UI, but maybe it's a bug in the Firestar module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.