Next Tutorial

Drupal 7 Tutorial Part 16: Drupal 7 Reports & Logs Explained

Programatically create node with cck node reference in drupal 6

Submitted by anilsagar on Thu, 07/29/2010 - 17:49

Hi Everyone,

I want to share with you guys how to create a node programatically using drupal_execute function.

There are so many articles which explains it. I googled for the solution to create a node with node reference and result is i didn't find any solution. After some research i found one solution for this. I want to share the same with you guys. Below code explains how to create node programatically.


global $user;
module_load_include('inc', 'node', 'node.pages'); // required
$create = array('type' => 'node_type');
$form_state = array();
$form_state['values']['name'] = $user->name;
$form_state['values']['title'] = $question; // node's title
$node_ref = db_result(db_query("SELECT title FROM {node} where nid = %d", $nid)) . " [nid:" . $nid ."]";
$form_state['values']['field_video_faq_video_ref'][0]['nid']['nid'] = $node_ref; // change the field id in place of field_video_faq_video_ref
$form_state['values']['op'] = t('Save'); // required value
drupal_execute('video_faq_node_form', $form_state, (object)$create);

Hope this helps for some one who is searching for solution.

Cheers,
Anil Sagar

1 comment

node reference programmatically

Submitted by Anonymous on Wed, 11/10/2010 - 20:55.

Hi Anil,

Can we assign a node reference on the basis of node->name, instead of using nid.

Thakur.

©2010 AnilSagar. All rights reserved. Drupal theme by Kiwi Themes.