0

I put this code in Rules Action PHP Evaluation. What I want is getting the field_price's value, changing it for negative and returning it for Rule. With this I will be able to charge points because in Rules (Drupal 7) it is impossible to put "-" before data-selector (in this case, field_price's value) and make it negative to charge these points instead of adding them.

<?php
if (arg(0) == 'node') {
   $nid = arg(1);
}
   $node = node_load($nid);
   $value = $node->field_price['und'][0]['value'];
   return $value;
?>
2
  • Please provide some more information about your rule. What are you trying to achieve? What do you mean with executing it outside the Drupal network? Which settings do your rule have? Also, as this is obviously a continuance of your previous question drupal.stackexchange.com/questions/110027/…, try to make clear what your purposes are from the beginning. All the seperate questions you ask now, won't be very useful for other users.
    – Jeroen
    Apr 13, 2014 at 23:16
  • Sorry. Now I wrote everything.
    – Michael D
    Apr 13, 2014 at 23:20

2 Answers 2

2

The reason you're getting the error "Undefined variable: nid" in some cases is obvious. You're only assigning a value to $nid if the following is TRUE: arg(0) == 'node'. If arg(0) is something else, $nid never becomes defined, causing the error.

Since it doesn't make sense to use $nid unless it is defined, you can get rid of error by moving the construct where you use the value of $nid inside the if-test, like this:

<?php
if (arg(0) == 'node') {
  $nid = arg(1);
  $node = node_load($nid);
  $value = $node->field_price['und'][0]['value'];
  return $value;
}
else {
  return FALSE;
}
?>

Note that this will return the value stored in the field field_price if arg(0) is equal to the string 'node', and return FALSE otherwise.

You write:

What I want is getting the field_price's value, changing it for negative and returning it for Rule.

Please note that your original code does not do that. Nor does mine. I just told you how to fix the bug. The business logic is still up to you.

Please also note that this has nothing to do with Drupal - it is Programming 101.

-1

Assumptions:

  1. You have a numeric array named arg where you are storing node type in even indexes and node id in odd indexes.
  2. You want to load the specific node into memory and call the member function field_price of node object which is a custom field added using the hook node_field_extra_fields().

Now if my assumptions are correct, then the solution to you problem should be :

<?php
function get_node_price{
if ($arg[0] == 'node') {
   $nid = $arg[1];
}
   $node = node_load($nid,NULL,FALSE);
   //Not sure whether the args and the method are correct here as it's custom.
   $value = $node->field_price['und'][0]['value'];
   return $value;
}
?>
1
  • Reasons for downvote: 1) It does not fix the bug in the OPs code. 2) It wraps the OPs buggy code in a function. This is not how PHP Evaluation inside Rules work. 3) It is pointless to pass additional arguments to node_load that are the same as the defaults. Apr 14, 2014 at 9:16

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.