I'm trying to update a field using PHP evaluation, but my code is being ignored.

My code:

$value = 75;
return $value;

I know the rule settings work otherwise -- if I change the data selector to one of the provided options (such as site:current-user:uid), it performs as expected.

Also, no value parameter is being displayed on the Rules edit screen. Screenshot attached.

CommentFileSizeAuthor
data-value_none-set.png2.37 KBjesss
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

PeterFawcett’s picture

Component: Rules Core » Rules Engine

I'm getting this same problem too:

$uid = 0;
print($uid . ':');
$total_points = db_select('userpoints_total', 'u');

$total_points->fields('u',array('points'));
$total_points->condition('UID', $uid);

$total_points = $total_points->execute()->fetchField();
print($total_points . '\n');
$tid = db_select('taxonomy_vocabulary', 'tv');
$tid->join('taxonomy_term_data', 'ttd', 'tv.vid = ttd.vid');
$tid->join('field_data_field_req_points', 'fdfrq', 'fdfrq.entity_id = ttd.tid');
$tid->condition('fdfrq.field_req_points_value', $total_points , '>');
$tid->condition('tv.name', 'Rank', '=');
$tid->condition('ttd.vid', 'tv.vid', '=');
$tid->condition('fdfrq.bundle', 'rank', '=');
$tid->condition('fdfrq.entity_type', 'taxonomy_term', '=');
$tid->condition('fdfrq.deleted', 0, '=');
$tid->orderBy('field_req_points_value', 'DESC');
$tid->range(0, 1);
$tid->fields('ttd', array('tid');
$tid= $tid->execute()->fetchField();

print($tid . ' is the new rank id');

$rank = db_update('field_data_field_rank');
$rank->fields(array('field_rank_tid' => $tid));
$rank->condition('entity_id', $uid, '=');
$rank->condition('entity_type', 'user', '=');
$rank->condition('bundle' = 'user');
$rank->execute();
PeteS’s picture

Having this problem as well. I tried switching to an earlier version of Rules and it didn't seem to work then either. I tried tracing through the code but it was hard to figure out exactly when it was supposed to pull out the code from the action 'code' field and evaluate it. It seems like it doesn't have an understanding of what the 'code' field is, so it never tries to access that data.

If it helps anyone, a temporary workaround is to use the "Execute custom PHP code action", and just update the field in the node object that should be available (i.e. "The following variables are available and may be used by your PHP code:")

fnapo’s picture

From the author of

"In contrast to the php input evaluator, the php data processor doesn't support accessing variables yet. Patch welcome." http://drupal.org/node/1231418

and

"there is no way to save the translations nid into the added variable" http://drupal.org/node/917290

It really seems that if you need a more complex integration and you don't want to learn apis the solution is the one suggested above. This worked for me:

$node->field_nodetmpfield['und'][0]['value'] = "666";
field_attach_update('node', $node);
videographics’s picture

Title: Value in action using PHP evaluation isn't set » In "Set a data value", the "Data selector" field should not be required to set/modify a value using PHP evaluation
Component: Rules Engine » Rules Core

...just worked through this. It turns out that in the "Set a data value" action, you need to set a value in the 'Data selector' field even if you're planning on completely ignoring it in your PHP code. Leaving the field blank empties the value and ignores all of the PHP code.

The "Set a data value" action should allow the PHP code to return (set) a value even if the "Data selector" field is left empty.

videographics’s picture

To clarify, this was found to be an issue with integer data types. It doesn't apply to text.

Ananya MultiTech’s picture

Priority: Normal » Minor

I wasted my 2 days just to understand that "Data selector" field should be provided for using PHP evaluation. I have some complex PHP processing to be performed on field. I also realized a fact that none of the variables are available here in this PHP block. Not even the variables passed to Rules Component. But then I have a workaround. Now I am using "Calculate a value" just prior to "Set a data value". I collect all my data there, do processing and then pass the result as variable. And then I use it directly...

Actually we need a good documentation on Rules, I feel. I have been involved in development of course curriculum of computer institute. So I am confident that I should be able to write a nice walk-through for Drupal Configuration without using PHP. But i dont know where to write this and how.. Can you people please guide me so that this can be a help for new comers..

zterry95’s picture

ahah, I have encounted this problem too and have spent a lot time on it.

Actually the solution is very simple. In the data select, at least you should select one data, even this selected data make nonsense, but pls make sure you have SELECT ONE.

Then you can write your own php code and write your own logic. and at the end of your php code, pls don't forget return $value;

Hope this helps:)

linus79’s picture

#7 works! it's a little bit strange way but works

KerriO’s picture

Confirmed — a data value must be set for the PHP rule to evaluate for an integer data field. Definitely seems like a bug to me…

ETA: That worked on one action I was working on, but now in a new Rule with a new Action that requires a PHP evaluation, I can't get anything to work (assigning a data variable doesn't work, and assigning an initial static value doesn't work, as it's cleared out after I switch to the PHP evaluation. I'm testing with a very very basic declaration just until I can actually get a result to return (e.g. $value = 2; return $value;). Nothing.

Also, I just want to point out that comments #1 & #3 are a bit misleading regarding the specific issue cited by the op. This has nothing to do with accessing variables. The issue is with successfully returning a value to set as a rules variable, and exists whether accessing variables or not (see my example above and the example from the op).

benjaminbradley’s picture

Issue summary: View changes

subscribe +1
this is very non-intuitive!

jason.fisher’s picture

I believe I had to switch back to direct input mode, set an actual value (1), save the form, then come back in and switch to data selector mode, provide a selector, then provide PHP that set $value and returned $value;

jason.fisher’s picture

On a related note, shouldn't the PHP evaluation be associated with direct input mode instead anyway?

Hanno’s picture

bump. Stuck with this with setting a date field. returning a value by php doesn't have any effect. if I add return date('Y-m-d', strtotime('+2 weekdays'));, it is not set. using the selector will give me the value of the selector, not of the outcome of the php code.

mpmumau’s picture

bump again. I too can't seem to get this to work, even using the above methods. Has anyone had any luck with is? Is there some alternative method or a hook I can use or something?

jeromewiley’s picture

Is there any documentation on creating a rule and working with system values so as to return a particular computed (with PHP) result?

sistro’s picture

Solution #3 works also for me

$node->field_nodetmpfield['und'][0]['value'] = "666";
field_attach_update('node', $node);

Sinan Erdem’s picture

When using #3, be aware that any rule depending on the update of a node will not fire.