1

I created a rule with the actions "Provide a variable: mynumber" and "Grant points to a user". I'd like to evaluate the points with PHP, using "mynumber". For some reason, I can't use $mynumber, although that is possible in the PHP evalution of the sections 'Operation' and 'Description'. As the replacement pattern [mynumber] is available, I tried to use it in my code with and without the square brackets, but in both cases, I get the error:

Notice: Use of undefined constant mynumber - assumed 'mynumber' in eval() (line 1 of C:\wamp\www\mysite\sites\all\modules\rules\modules\php.eval.inc(146) : eval()'d code).

Is this a missing feature, a bug in the Rules module or did I do something wrong?

2 Answers 2

1

You can achieve this, use variable module and define variable which you want use in rule.

To define a variable you need to write custom module , which will contain following function

function My_module_variable_info() {
  $variables['rainbow_count'] = array(
  // 'name' isn't really necessary, as it will get populated from the key of this array above.
 'name' => 'rainbow_count',
 'title' => t('Number of Rainbows'),
 'type' => 'number',
 'group' => 'UserPoints Config',
 'token' => TRUE,
'default' => 0,
'description' => t('Specify the number of rainbows.')
); 

}

Set value for variable which you want use in rule through :

configuration>>variables>>modules>>My_module_...

Now go to rules >action> grant points to a user > Points:

Set data type =current-user:uid and put return variable_get('rainbow_count'); code in PHP evaluation.

This solution worked for me,hope will help you also.

0

It turned out that the problem is caused by the way Rules handles integers and numbers in general. Instead of trying to calculate the Userpoints in the action "Grant points to a user", one should provide a new variable earlier.

  1. Provide a new variable (text type!), called "Number", with the necessary PHP code (wrapped with <?php ... ?>). One can then use earlier calculated results like $mynumber.

  2. Convert "Number" to an integer, called "Points".

  3. Grant "Points" to the user.

Hopefully, Rules will cope a little bit better with numbers in the future, but until then, I hope this trick helps a lot of people.

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.