Help on hook_insert for CCK content types
mixersoft - May 7, 2007 - 10:00
I created a CCK content type and now I'm trying to figure out how to create an instance in code. I've been playing around with coding modules and I'm familiar with the following constructs:
function node_create() {
$node = new stdClass();
$node->type = [node type];
...
node_save($node);
}
But I'm digging through the CCK for Developers Handbook and it's not immediately clear how to do the same for a CCK content type. Do I have to create each CCK field/wiget separately and then tie them together into 1 content type instance? Can someone point me in the right direction?
TIA

in case it's not clear
what I'm trying to do is to create a CCK nodetype from another module in php.
to 'rebuild' a cck node you
to 'rebuild' a cck node you actually need to go through all the fields and widgets. have a look into content.module which hooks are called in the different phases.
and have a look here http://drupal.org/node/101746
is there a newbie version of that? :-)
I'm brand new to Drupal, and have been trying to do something similar... I created a module, and have a module_nodeapi function in it. There, I'm capturing "insert" operations against a particular node type. When found, I want to create several other (CCK-defined) nodes , based on what's in the node being inserted right now.
Problem is, whereas the core stuff is just, e.g., $node->title, the CCK stuff is a couple arrays deep. And even when I try to mimic what I see when I var_dump a CCK node, it doesn't seem to work.
My current attempt looks more or less like so (excuse the extra temporary variables; I was trying to track down PHP errors):
function exploder_nodeapi (&$node, $op, $a3, $a4)
{
if ($op != "insert") return;
if ($node->type != "metric_setup") return;
$child = new StdClass();
$child->type = 'tool_metrics';
$child->uid = $node->uid;
$child->title = v($node, "field_label") . " for " .
v($node, "field_manager_tool");
$child->field_owner = mkcck("uid", v($node, "field_owner"));
$child->field_metric_tool = mkcck("value", v($node, "field_manager_tool"));
$child->field_url = mkcck("value", v($node, "field_url"));
node_save($child);
}
function v ($node, $field)
{
$x = $node->$field;
$y = $x[0];
return $y['value'];
}
function mkcck ($key, $value)
{
$x = array();
$y = array();
array_push($x, $y);
$y[$key] = $value;
return $y;
}
The v() and mkcck() stuff was my attempt to decode and then encode, respectively, the data in the "CCK" way, again per what I saw when I did a var_dump on CCK nodes.
This doesn't quite seem to work.
Is there a clean, documented way to create and save CCK content? Seems like this should be fairly straightforward and/or often-asked. Poking around the forum only lead me to this one thread.
Thanks!
you might wanna have a look
you might wanna have a look into the node_factory module http://drupal.org/project/node_factory, does pretty much what you are after.
and perhaps you want to include some code for the operation 'update', 'insert' is only called for the initial creation of a node.