Programmatically create a node with CCK fields?

Events happening in the community are now at Drupal community events on www.drupal.org.
ubuntu's picture

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.

Comments

here is your solution :)

node_factory module

clemens.tolboom's picture

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

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

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ử...

Content Construction Kit (CCK)

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: