How to code an action to create a new node?

ktnk - June 8, 2007 - 15:05

Hello,

How would I code the following action:

I want to create a workflow for one cck node type
with an associated action upon state change
which automatically creates and fills a new node of a different cck type
with the title and teaser of the current node.

Anyone have sample code?

I can export my node types for you to look at.

Thanks,
ktnk.

Creating the node

bpirkle - June 8, 2007 - 16:40

I'm still learning myself, but here's my code for creating a node, in case it helps you any. I wrote this based on some other postings I found on the drupal.org site.

<?php
function _mymodule_make_node($an_integer) {
 
//  Create new node
 
$node = array();
 
$node['type'] = 'my_node_type';
 
$node['name'] = 'My Node Type';

 
//  FORMATS ON CCK NODES APPLY TO FIELDS, SO NO FORMAT SET HERE
 
$node['format'] = 0;

 
//  ENABLE READ/WRITE COMMENTS
 
$node['comment'] = 2;

 
//  PUBLISHED
 
$node['status'] = 1;

 
//  NOT PROMOTED TO FRONT PAGE
 
$node['promote'] = 0;

 
//  NOT STICKY
 
$node['sticky'] = 0;

 
//  NODE FIELDS HERE
 
$node['title'] = 'My Node Title';

 
//  THIS IS A CCK FIELD
 
$node['field_my_integer]['0']['value] = $an_integer;
 
 
$node = (object)$node;
 
$node = node_submit($node);
 
node_save($node);
 
  return
$node;
}
?>

Hope that helps, at least a little.

Cool...

tjjacobsen - September 28, 2009 - 21:40

thanks! Just what I needed.

how do i get nid

aneesh84 - April 16, 2009 - 19:18

the create function works but i want the nid of the newly created node to use it further in my snippet. thanks for the help in advance

_

WorldFallz - April 16, 2009 - 19:33

$node->nid

_
Don't be a Help Vampire - read and abide the forum guidelines.
If you find my assistance useful, please pay it forward to your fellow drupalers.

 
 

Drupal is a registered trademark of Dries Buytaert.