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.