5

I have a variable I've defined via rules using 'Add a variable' called 'minimum_reached'. This is available to me for use in custom PHP code.

if($total >= $minimum_fund){
   $minimum_reached = 'yes';
   dpm($minimum_reached, 'min reached');
}

Once this is set to 'yes', the next action fires which Sets a data value of the current node's 'minimum reached' field to yes (depending on that above outcome). The variable is being set as the dpm works, but it's not being carried across to the next action. How do you define this variable in PHP for use later on? Thanks.

3

The Rules module doesn't seem to provide this feature.

I also tried using a rule set with a variable, but the variable was reset between each action. I tried adding return $minimum_reached at the end of each rule executing PHP code, but it didn't help.

The only way seems to use drupal_static(), if you are using Drupal 7 (e.g. with drupal_static('mimimum_reached', 'default value used when the variable is not set')), or a global PHP variable, if you are using Drupal 6.

  • would using variable_set be acceptable in this case as well? – saadlulu Dec 12 '12 at 6:48
  • variable_set() is for saving data that are used in more than one page. If the rule needs to remember the value the successive time it is executed, then you can use variable_set(); differently, a PHP global variable is enough. – kiamlaluno Dec 12 '12 at 8:08
  • I'm having a similar problem: I have a variable called 'Points' and I'd like to change it's value with the execution of PHP code. Unfortunately, the value isn't saved when I use 'Points' in a following action 'Grant points to a user'. How should I exactly use drupal_static? I don't fully understand your answer. I tried adding drupal_static($points); at the end of my PHP code, but that doesn't seem to work. What did I do wrong and how can I solve this? – Jeroen Mar 18 '14 at 22:20
  • @Jeroen See the arguments required from the function; you are using an argument when you should use two arguments. – kiamlaluno Mar 19 '14 at 13:27
  • I don't really understand how I should work with drupal_static or what it does, even after reading the documentation. I've now added $points = drupal_static('points', $default_value = NULL); at the end of my code, but it doesn't work. What should I do to maintain the $points value for my next action in my rule? – Jeroen Mar 19 '14 at 21:51
0

While it's not supported via the Rules module's UI, you can (theoretically) add variables to your evaluation structure by declaring a custom action. See https://drupal.org/node/878972

I'm working to get past the theoretical part, using a variable provided by one custom action in another custom action. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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