Programmatically create a node with CCK fields?

I used admin GUI to create a new content type "my_type", then I added CCK fields. Does anyone know how to create a node of "my_type" using php programmatically?

I used drupal_execute() to create node before, but it requires a form id. In that case, the content type was defined by a module, which contained a form for the content type. I don't know what form id to use for a type created using GUI with CCK fields.

Another method of creating node is to use node_save(). I haven't figured out how to use either method for content type with CCK fields. Any help is greatly appreciated.

Thanks.

Login or register to post comments

here is your solution :)

greggles's picture
greggles - Sat, 2008-03-29 15:26

http://www.civicactions.com/blog/cck_import_and_update

That is the "best" method of how to do it...

There is also a lot of great discussion in http://lists.drupal.org/pipermail/development/2008-March/029148.html


node_factory module

clemens.tolboom's picture
clemens.tolboom - Sun, 2008-04-06 14:47

I hope some common sense will land in this module. This is far from complete though.

See also http://drupal.org/node/231922 for an abstract of the discussion mentioned by greggles.


Multiple values field

jcisio's picture
jcisio - Thu, 2008-08-07 15:24

Hello,

I know that for a simple field, this works:

<?php
$node
->field_soft_license[0]['value'] = $lic;
?>

But if this is a multiple values field (selected by checkboxes), I try this:

<?php
foreach ($licenses as $lic) {
 
$node->field_soft_license['value'][$lic] = $lic;
}
?>

However it doesn't work :( I use node_save instead of drupal_execute as I think the latter has some problems with Batch API.

--
[vi] www.thongtincongnghe.com
Trang tin điện tử về CNTT, Viễn thông, Điện tử...


My bad, I change to <?php

jcisio's picture
jcisio - Thu, 2008-08-07 15:53

My bad, I change to

<?php
$node
->field_soft_license[$i++]['value'] = $lic;
?>
and it works! I just looked at the form and wroter the former code. In fact, it's more intuitive.

--
[vi] www.thongtincongnghe.com
Trang tin điện tử về CNTT, Viễn thông, Điện tử...