Drupal 6: how create a node with CCK fields programmatically

<?php 

require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
module_load_include('inc', 'node', 'node.pages');

$node = new stdClass();

//Set up default values, if required.
node_object_prepare($node);

//Specify the content type
$node->type = 'profile';

//Specify an author for the node
$node->uid = 1;

//Add the title
$node->title = 'Test';

//Add the CCK fields data
$node->field_name[0]['value'] = 'Name';
$node->field_surname[0]['value'] = 'Surname';

//Save the node object into the database.
node_save($node);

//Debug the created node
print '<pre>';
print_($node);
print '</pre>';

Tags: ,

  1. Andystox’s avatar

    Useful post!

    A question though, is there a way to programmatically create a node and have it automatically include all cck fields defined for that node type? I have a node type with a lot of checkboxes that can change from time to time and I don’t want to have to make code changes every time an administrator wants to add new fields?

    Cheers!

Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>