I've ran into the following issue in Drupal 7:
I need to be able to, upon saving a new (or existing) node, set one of the fields with the value of a field of another node (of different type). The field type is node_reference. These is my current setup:
Event -> Before Saving Content
Condition -> Data Comparison: [node:type] -> TYPEA
Condition -> PHP Code:
$result = db_query('SELECT 1 FROM {field_data_field_myfield} n WHERE n.bundle=:mybundle AND n.field_myfield_value=:myvalue', array(':mybundle' => 'TYPEB',':myvalue'=>'[node:field_myfield]'));
if($result->rowCount()==1) { return true; } else { return false;}I've tested the condition above and it works, no prob.
Action -> this is where I don't know what to do. I need to be able to get n.field_myfield_value that's returned by $result in the PHP condition and set it to the node:field_myfield of the current node (the one being edited). It's almost like I want to run the same PHP code, return the value from the $result and then use a "Set a Data Value" Action, but I doubt that's how it's done.
Any help is truly appreciated; first time dealing with PHP in rules
Thanks, Adrian.-
Comments
Hi, I'm not a coding expert
Hi, I'm not a coding expert but I would try writing my own action in PHP and triggering it from Rules.
In the tutorials
http://nodeone.se/blogg/learn-drupal-coding-with-nodeone-part-7-example-...
and
http://nodeone.se/node/895
you can find pretty good step by step information on how to do so.
Hope it helps
regards
Almost there, but not quite
Thanks clanet; I'll take a look at the links you provided.
After looking at the other actions provided, I see that there is a "Fetch Entity by Property" action. I think this is exactly what I want, except that I need it to be "Fetch Entity by Properties" or something like that. In other words, I would like to fetch an entity of type Node, and then specity: Node Type = TYPEB, field_myfield = 'some value', field_myfield2 = 'some other value'
While I take a look at how rules actions are made. Anyone familiar with how the current "Fetch Entity by Property" is made and how I can, perhaps, modify it or clone it to take a couple of more properties?
Once I fetch the entity, I figure I can use entity_fetched to get whatever data I want from it using the "Set a Data Value" action.
Thanks again!
Hello,I'm curious about this
Hello,
I'm curious about this myself. I'm grabbing data from a web service (triggered by a node save in Rules), and need to populate multiple fields using PHP with the returned results. If I had to do this one field at a time I'd have to make multiple API calls to an external site which would be sad. :(
I'm happy someone asked the same question first. :)
Best regards,
Chris
EDIT:
I wonder if this would help:
http://stackoverflow.com/questions/2278093/drupal-rules-adding-field-dat...
Worlds to explore. Worlds to create.
Blog: http://www.christopherstevens.cc/blog
Twitter: http://www.twitter.com/owntheweb
Possible solution
Ok,
This may be considered "wrong", but worked for what I needed and kept code minimal. Under conditions I ran my custom PHP code to take field data, send it to a web service, and get some results back. If the character data in my case was verified, I went back to the basics and updated my node right there on the spot like this:
<?php
//...
if($char_match == true) {
//update fields with character info
$update_node = node_load($node->nid);
$update_node->field_api_verified["en"][0] = array("value"=>1);
$update_node->field_api_last_verified["en"][0] = array("value"=>gmdate("Y-m-d H:i:s"),
"all_day"=>"",
"show_todate"=>"",
"value2"=>gmdate("Y-m-d H:i:s"),
"timezone"=>"UTC",
"offset"=>0,
"offset2"=>0,
"timezone_db"=>"UTC",
"date_type"=>"date_type");
$update_node->field_character_id["und"][0] = array("value"=>$eve_verify_char_id);
$update_node->field_character_corp_id["und"][0] = array("value"=>$eve_verify_corp_id);
$update_node->field_character_corp_name["und"][0] = array("value"=>$eve_verify_corp_name);
node_save($update_node);
return false; //won't meet condition so will skip actions below (which resets values and shows mean messages in my case)
} else {
//no character with specified name, so run actions below.
return true;
}
//...
?>
As seen in the comments, if the character data didn't validate, true would be returned and the actions would be called. In my case several fields for the node are being reset one at a time by Rules, and a wonderful error message is shown on the page.
Hope that helps! Let me know if you find a better way. I was dragging on this all day.
Best regards,
Chris
Worlds to explore. Worlds to create.
Blog: http://www.christopherstevens.cc/blog
Twitter: http://www.twitter.com/owntheweb
The devil is in the details!
Chris,
Thanks for the API call node_save(), which is exactly what I wanted to do.
I'm just starting out with Drupal, although it's fair to say I have got a very long way already with what I want to do in just a couple of days of intensive coding.
My problem was how to conditionally update a field value. I couldn't use the "Set a data value" action since I had to update the field in the same Action (which was custom PHP), and I am not really sure how persistence works between Actions :=)
However, by modifying the field value as you have shown, i.e.
$update_node->my_field_name["und"][0]['value'] = $my_new_value;
and calling node_save(), everything worked fine, since I could update the field from the if{} condition in my code where I was generating a file (on the system).
I'm not sure if there is a more elegant Field CRUD API way of doing things (from within a Rule) – and I gave up on making the field a File field because that brought in about 100 other field parameters (*G) – but it works flawlessly on my test box and thus enables me to update the field I want programmatically based on the original Action condition.
Well, back to the coding and the 100 or so other content types :=)