By Ashford on
I am using the Workflow_ng module and I needed to use php to create a CCK node after the applicant paid their dues. While searching Drupal, I found bits and pieces of answers in different forums and web sites. Especially difficult to find was how to generate the values for the CCK fields that were using the options widget.
Here's a culmination of what I found that worked for Drupal 5.
I'll use the name 'abc' for the content_type in this example.
// Construct the new node object.
$node = new stdClass();
$node->title = "Your node title";
$node->body = "The body text of your node.";
$node->type = 'abc';
$node->created = time();
$node->changed = $node->created;
$node->status = 1; // Published?
$node->promote = 0; // Display on front page?
$node->sticky = 0; // Display top of page?
$node->format = 1; // Filtered HTML?
$node->uid = 1; // Content owner uid (author)?
$node->language = 'en';
/* **************
CCK date fields
format dates as shown in your content_type's data table
Mine were: 2008-12-25T00:00:00
************** */
$today = mktime(0,0,0, date("m"),date("d"),date("Y"));
$cck_today = date("Y-m-d") . "T00:00:00";
$node->field_abc_date_joined[0]['value'] = $cck_today;
/* **************
CCK reference fields
$node->['field_name'][0]['uid'] = 3;
$node->['field_name'][0]['nid'] = 125;
************** */
$node->field_abc_user_ref_uid[0]['uid'] = 3;
/* **************
CCK select fields using the options widget
$node['field_name']['key'] = 'your value';
or if it's multiple select: $node['field_name']['keys'] = array('your value').
************** */
$node->field_abc_intern_status['key'] = 'trainee';
$node->field_abc_certified_value['key'] = 'no';
/* **************
CCK regular text fields
$node['field_name'][0]['value'] = 'your value';
************** */
$node->field_abc_member_status[0]['value'] = 'volunteer';
node_submit($node);
node_save($node);
Comments
More: node_save($node) with CCK fields --using date widget
The date fields I was using in the previous example were text string dates. If you use the options widget to select the Month-Day_Year, use these lines...
$node->field_abc_date_joined[0][value][mon] = date("m");
$node->field_abc_date_joined[0][value][mday] = date("d");
$node->field_abc_date_joined[0][value][year] = date("Y");
* Example uses the current date for the values.
Very helpful Code
Hi Ashford ,
Thanks for this helpful Code. It helps me a lot.
Can you please help me to add image field in this code
re: help me to add image field in this CCK code
I have not tried this with the CCK imagefield. The first thing I would try is to follow the same sequence as the other text fields. As all the experts say, "Try it on a test site and not on your live site."
$node['your_imagefield_field_name'][0]['value'] = 'your value';
I hope this code syntax still works with Drupal 6 (and 7). I am getting ready to set up a new web site and I will need the Rules module to trigger some node updates.
Thanks Ashford for quick
Thanks Ashford for quick reply,
I didn't try this.........as i have already got the solution..........
Please share your solution
Please share your solution for the community!
Please share your solution
Thanks for all the helpful code!