I am trying to create an Automatic Registration with rules. Basically when a new asset is created a rule will kick off and create a new registration node with custom PHP but the new node is not being created. I don't see any errors in the reports. Here is the code:

$node = new stdClass;
$node->type = 'asset_event';
$node->title = 'Automatic Registration';
$node->[field_first_name]['und'][0]['value'] = [site:current-user];
$node->[field_asset_event_type]['und'][0]['tid'] = 791;
$node->[field_asset_additional_informati]['und'][0]['value'] = 'Event created by automatic registration';
$node->[field_link_to_asset_id]['und'][0]['value'] = [node:field-link-to];
node_object_prepare($node);
node_save($node);

Thanks for any help.

share|improve this question
Is this field name a typo: "field_asset_additional_informati"? Also are you enclosing the value (Event created by...) in quotes? – Topsitemakers Jan 23 at 15:10
FYI, there is an action to create a new node. I'd never do this in PHP unless I can't do in existing Rules actions. we use Rules to avoid making many tiny modules, right ? – Ayesh K Jan 23 at 15:13
@Topsitemakers No that's not a typo. I did miss the quotes but that did not work. – Brian Jan 23 at 15:25
2  
You can set fields with Rules after creating the node by fetching the node you just created, with "fetch an entity" ; then, set value... – Gregory Kapustin Jan 23 at 15:36
1  
Actually, you don't even have to fetch it, sorry ! When you "create a new entity" with Rules, it provides you the entity, already fetched in the "Variable" specified at the end of that action. You just have to "Set a value" then, on that entity. – Gregory Kapustin Jan 23 at 15:59
show 2 more comments

2 Answers

up vote 1 down vote accepted

In Rule:

  1. Create a new entity
  2. Of type Node
  3. At the end of your action, the "Provided variables" section will give you the label of the entity created
  4. Use that entity, already fetched, to set fields (with "Set a value") of that node.

Enjoy !

share|improve this answer

I did something like that, but I was on Drupal 6.

Anyway I can spot some strange things in your code. I will try to rewrite it as I believe it is correct.

$node = new stdClass(); //please note the ()
$node->type = 'asset_event';
$node->title = 'Automatic Registration';
$node->field_first_name['und'][0]['value'] = [site:current-user]; //field names without []
$node->field_asset_event_type['und'][0]['tid'] = 791;
$node->field_asset_additional_informati['und'][0]['value'] = 'Event created by automatic registration';
$node->field_link_to_asset_id['und'][0]['value'] = [node:field-link-to];
node_object_prepare($node);
node_save($node);

I'm not sure this qualifies as an answer, but I hope it helps a bit.

share|improve this answer
I appreciate the help but this did not work. – Brian Jan 23 at 16:06
I suggest to install Devel module, create a node 'Automatic Registration' with the normal content creation with Drupal and inspect it with Devel. Then put in your code "kpr($node);" before node_save() function and compare the structure with the one before. Then fix the differences. – ermannob Jan 23 at 16:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.