Programmically assign parent when creating a node from script in D6

that0n3guy - November 3, 2009 - 03:41
Project:Node Hierarchy
Version:6.x-1.2
Component:Documentation
Category:support request
Priority:normal
Assigned:Unassigned
Status:active
Description

Hey,

I am writing a module that takes some content from a parent node, manipulates it, then adds the manipulated content to child nodes.

There is a lot of documentation/tutorials on how to create a node/modify a node/add stuff to cck/etc... with a script... but didnt really see any thing for this module.

How would I go about setting the parent of a node from a script?

Normal nodes are something like this:

<?php
// Construct the new node object.
$node = new stdClass();

// Your script will probably pull this information from a database.
$node->title = "My imported node";
$node->body = "The body of my imported node.\n\nAdditional Information";
$node->type = 'story';   // Your specified content type
$node->created = time();

CCK something like this:

<?php
$node
->field_text_field[0]['value'] = "value 1";
$node->field_text_field[1]['value'] = "2nd value";
$node->field_nodereference[0]['nid'] = 58;
?>

(on a side note... If anyone is interested, I got this code from here: http://acquia.com/blog/migrating-drupal-way-part-i-creating-node ).

How would I do this w/ node hierarchy?

#1

that0n3guy - November 3, 2009 - 15:52

Soooo yeah, I just guessed and gave it a shot... and it worked :)

I decided to go w/ drupal_execute from this guide: http://thedrupalblog.com/programmatically-create-any-node-type-using-dru...

Here is what I came up with:

// create $form_state
$form_state = array();
$form_state['values'] = array(
    'title' => t('MyTitle ') .$i, // 'i' is just a counter, I have this in a loop.
    'body' => spin($string, false),
    'name' => $myuid, //this var is the user ID pulled from a database
    'parent' => $mynid,  //var is also pulled from a database, its the node id of the parent I want.
    'op' => t('Save'),
);

/* add CCK fields -- used later for cck fields
$form_state['values']['field_MYFIELD1'][0]['value'] = "blah";
$form_state['values']['field_MYFIELD2'][0]['value'] = "wee";
*/

// create $node
$node = (object) NULL;
$node->type = 'article';

// include node file, necessary for node generation
module_load_include('inc', 'node', 'node.pages');

// create node using drupal_execute
drupal_execute('article_node_form', $form_state, $node);

The 'parent' is what I used and set it as the nid.

Hope this helps someone :).

#2

andreiashu - November 4, 2009 - 16:05

Hi,

Just in case there is someone else that is using the 2.x branch (which is a lot better) here is what I came up with to create a new child when a node (of a certain type) is created.
If you know a better way of doing this please post a reply here.

/**
* hook_nodeapi
*/
function myModule_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  if($op == 'insert' && $node->type == REPLACE_THIS_WITH_YOUR_NODE_TYPE) {
    global $user;

    $nid = @$node->nid;

    $plid = _nodehierarchy_get_node_mlid($nid);
    $nodehierarchy_menu_links = _nodehierarchy_default_menu_link($nid, $plid);
    $nodehierarchy_menu_links['hidden'] = FALSE;

    $nodehierarchy_old_menu_links = $nodehierarchy_menu_links;
    $nodehierarchy_old_menu_links['enabled'] = TRUE;
    unset($nodehierarchy_old_menu_links['hidden']);
    unset($nodehierarchy_old_menu_links['link_path']);
    $nodehierarchy_old_menu_links['link_title'] = '';
    $nodehierarchy_old_menu_links['expanded'] = '';
    $nodehierarchy_old_menu_links['description'] = '';
    $nodehierarchy_old_menu_links['customized'] = '';

    // This is the child node
    $std_node = new stdClass();
    $std_node->title = 'The node\'s title';
    $std_node->type = 'page';
    $std_node->uid = $user->uid;
    $std_node->nodehierarchy_menu_links[] = $nodehierarchy_menu_links;
    $std_node->nodehierarchy_old_menu_links[] = $nodehierarchy_old_menu_links;
    node_save($std_node);
  }
}

#3

that0n3guy - November 6, 2009 - 15:15

The way I'm using this (w/ NH 1.2), I have a parent, which is created by users, and then they push a button (on a different page) and there parent is analysed and 100's of children are spit out.

Since I am also using batch api, I have to pass $nid (the parent nid) from previous functions.

function add_new_node($title, $myusername, $nid, $body, $descr, $refbox) {

/**
* Saving current user, then loading user w/ permissions to change author and pick parent nodes
* I have to do this because normal users don't have the permissions to set the author of a node.
* I want the children author to be the same as the parents author, even if someone else generates the children.
*
*/
global $user;
//save current user name
$current_user = $user -> name;
//load user with permissions to do everything on here
$user = user_load(array('name' => 'spincreator_donttouch'));
  $node = new StdClass();
  $node->type = 'auto_spun_article';
  $node->status = 1;
  $node->promote = 0;
  $node->format = 1;
  $node->title = $title;
  $node->name = $myusername; //used a username pulled from a query of the parent

// This is where I set the parent node id for the child... that simple
$node->parent = $nid;
  $node->field_spun_ref_box[0]['value'] = $refbox;
  $node->field_spun_desc[0]['value'] = $descr;
  $node->field_spun_article_body[0]['value'] = $body;
  $node = node_submit($node);
  if ($node->validated) {
    node_save($node);
  }
  //load old user back in
  $user = user_load(array('name' => $current_user));
}

I stopped using drupal_execute because a) it sometimes hates on cck fields b) its kinda funky with batch api... (plus I noticed it doesnt have a D7 section in the api http://api.drupal.org/api/function/drupal_execute/6 ... )

I didnt even look at the 2.x version yet.... I didn't realize it was changed so much. Hmmm.... maybe I should look at it instead.

#4

andreiashu - November 12, 2009 - 09:18

I didnt even look at the 2.x version yet.... I didn't realize it was changed so much. Hmmm.... maybe I should look at it instead.

Before starting playing with it have a look at this patch first:#623300: SQL errors in the latest 2.x dev from 2009-Nov-01

Cheers

 
 

Drupal is a registered trademark of Dries Buytaert.