I am trying to update content_profile fields when I add a node, using rules.

Under the PHP evaluation section I have added some code, but when I look at the output I realise that the PHP Variables are not being loaded. How do I access rules PHP variables in the PHP evaluation section?

The code I am using is:

$result_user = db_query("SELECT uid FROM users WHERE name = '" . $user_added . "'");
print "SELECT uid FROM users WHERE name = '" . $user_added . "'";
while($data_user = db_fetch_array($result_user)) {
	$users_id = $data_user['uid'];
}
$sql = "SELECT field_firstname_value, field_lastname_value, field_address1_value, field_address2_value, field_citytown_value, field_county_value, field_country1_value, field_postcode_value, field_tel_value FROM content_type_studentmembers WHERE nid = '" . $node . "'";
print "SELECT field_firstname_value, field_lastname_value, field_address1_value, field_address2_value, field_citytown_value, field_county_value, field_country1_value, field_postcode_value, field_tel_value FROM content_type_studentmembers WHERE nid = '" . $node . "'";
$result = db_query($sql);
while($data = db_fetch_array($result)) {
	db_query("INSERT INTO profile_values (`fid` ,`uid` ,`value`) VALUES ('1', '" . $users_id . "', '" . $data['field_firstname_value'] . "')");
	db_query("INSERT INTO profile_values (`fid` ,`uid` ,`value`) VALUES ('2', '" . $users_id . "', '" . $data['field_lastname_value'] . "')");
	db_query("INSERT INTO profile_values (`fid` ,`uid` ,`value`) VALUES ('3', '" . $users_id . "', '" . $data['field_address1_value'] . "')");
	db_query("INSERT INTO profile_values (`fid` ,`uid` ,`value`) VALUES ('4', '" . $users_id . "', '" . $data['field_address2_value'] . "')");
	db_query("INSERT INTO profile_values (`fid` ,`uid` ,`value`) VALUES ('5', '" . $users_id . "', '" . $data['field_citytown_value'] . "')");
	db_query("INSERT INTO profile_values (`fid` ,`uid` ,`value`) VALUES ('6', '" . $users_id . "', '" . $data['field_county_value'] . "')");
	db_query("INSERT INTO profile_values (`fid` ,`uid` ,`value`) VALUES ('7', '" . $users_id . "', '" . $data['field_country1_value'] . "')");
	db_query("INSERT INTO profile_values (`fid` ,`uid` ,`value`) VALUES ('8', '" . $users_id . "', '" . $data['field_postcode_value'] . "')");
	db_query("INSERT INTO profile_values (`fid` ,`uid` ,`value`) VALUES ('9', '" . $users_id . "', '" . $data['field_tel_value'] . "')");
}

Does anyone have any ideas, I cant find a forum post on this other than one saying to use TOKEN's but this causes an error. Help would be really great as I'm sure this is very close...

Thank you

Chris

Comments

chrispeat’s picture

I have managed to get around the issue with tokens, which turned out to work.

As an example this is how the code changed:

$sql = "SELECT field_firstname_value, field_lastname_value, field_address1_value, field_address2_value, field_citytown_value, field_county_value, field_country1_value, field_postcode_value, field_tel_value FROM content_type_studentmembers WHERE nid = '[node:nid]'";
#print "SELECT field_firstname_value, field_lastname_value, field_address1_value, field_address2_value, field_citytown_value, field_county_value, field_country1_value, field_postcode_value, field_tel_value FROM content_type_studentmembers WHERE nid = '[node:nid]'";
$result = db_query($sql);
while($data = db_fetch_array($result)) {
	db_query("INSERT INTO profile_values (`fid` ,`uid` ,`value`) VALUES ('1', '[user_added:uid]', '" . $data['field_firstname_value'] . "')");
	db_query("INSERT INTO profile_values (`fid` ,`uid` ,`value`) VALUES ('2', '[user_added:uid]', '" . $data['field_lastname_value'] . "')");
	db_query("INSERT INTO profile_values (`fid` ,`uid` ,`value`) VALUES ('3', '[user_added:uid]', '" . $data['field_address1_value'] . "')");
	db_query("INSERT INTO profile_values (`fid` ,`uid` ,`value`) VALUES ('4', '[user_added:uid]', '" . $data['field_address2_value'] . "')");
	db_query("INSERT INTO profile_values (`fid` ,`uid` ,`value`) VALUES ('5', '[user_added:uid]', '" . $data['field_citytown_value'] . "')");
	db_query("INSERT INTO profile_values (`fid` ,`uid` ,`value`) VALUES ('6', '[user_added:uid]', '" . $data['field_county_value'] . "')");
	db_query("INSERT INTO profile_values (`fid` ,`uid` ,`value`) VALUES ('7', '[user_added:uid]', '" . $data['field_country1_value'] . "')");
	db_query("INSERT INTO profile_values (`fid` ,`uid` ,`value`) VALUES ('8', '[user_added:uid]', '" . $data['field_postcode_value'] . "')");
	db_query("INSERT INTO profile_values (`fid` ,`uid` ,`value`) VALUES ('9', '[user_added:uid]', '" . $data['field_tel_value'] . "')");
}

The query still remains though, how do you access the PHP variables, or cant you?

HTH Other people with the same query...

Chris

nickrice’s picture

Chris, you can access node PHP variables in Rules.

A snippet from your SQL in the first example says, "... WHERE nid = '".$node."' ...".

But $node is the whole node object. The node id is $node->nid.

There's a lot you can do with $node (and the other variables Rules can see as documented on the Rules forms themselves).

To see what's there you can create a text area field and populate it with a dump of $node using Rules:

  • Create text field field_test. Make it plain text so you don't have to set the format when you populate it below.
  • Create a rule to fire on (say) "content going to be saved" with condition "content has type" for your content type.
  • Add an action CCK:Populate a field and choose field_test from the list of fields.
  • Take a look in PHP Evaluation on the next page to see what PHP variables are available. These differ for different triggers so it's worth exploring.
  • Open "Advanced: Specify the field's value with PHP code". (You should have the PHP filter enabled in admin/modules.)
  • Underneath the code box it tells you the format of the return array you need to populate the field. This can be different for different CCK types. I tend to cut and paste it to get the right structure for whatever field I'm writing to.
  • Dump the node into a string and return it. eg
    $text = print_r($node, true);
    return array(0=>array('value'=>$text));
  • Save that then update a node so the rule fires. The result looks a bit of a mess but if you edit the node again it will be laid out much more helpfully and you can see what's there. eg you'll see that the content of the field we've just populated (prior to populating it) is in $node->field_test[0]['value'], and you'll also see the node id and lots of other stuff, all of which you can get hold of and use.

If instead of "CCK:Populate a field" you choose the "PHP:Execute custom PHP" action then you can do a whole load of stuff in one place. You can change field values etc there by updating them in $node and then doing return array("node" => $node); as documented above the code text area. It's really powerful!

Junro’s picture

Hello; I created a Triggered rule

Created content's field 'field_xxx' has value

I must set a value : OK, no problem

but I want to use : Advanced: Specify the fields value with PHP code

For example, this works:

return array(
  0 => array('nid' => 24),
);

but I must use a node referrence

$noderefnode = node_load($field_yyy[0][nid]);
$noderefnode->field_zzz[0]['nid']

How to integrate the code above with return array.... (the php code of the value...)

Any idea? Thanks :)

drupalreggie’s picture

+ 1

Patribus’s picture

Hello,

I'm having a problem wich I describe here
http://drupal.org/node/1455240#comment-5751152
and apparently I'm not the only one.

Since the subject seems very similar to the one discussed here, maybe you can help me.

Thanks
Cheers

ayesh’s picture

Couldn't read/understand what you needed to do actually, but looks like you just need a dpm() (devel module) to get the vars available.
When a Rule is executed in a certain event, almost every variable is available to the View. On user insert, user's data are just available in given variables. I never needed to use database queries to grab these data.
and last: There is no good reason to use Rules and PHP together. You can either do it all in just php, or do it in Rules' Load user, Load node, and other given actions.

Sorry if my reply doesn't make sense or crappy.