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?
Comments
Comment #1
that0n3guy commentedSoooo 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:
The 'parent' is what I used and set it as the nid.
Hope this helps someone :).
Comment #2
andreiashu commentedHi,
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.
Comment #3
that0n3guy commentedThe 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.
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.
Comment #4
andreiashu commentedBefore 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
Comment #5
dhope commentedHi all,
I'm also attempting to create child nodes but it doesn't want to work. Here's the code snippet:
The node gets created correctly except no parent is assigned.
Manually assigning the parent works fine.
$node->parent is set correctly prior to the call to node_save but turns up empty afterwards.
Guess I could work around it by writing to nodehierarchy directly but then I'd run into problems with URL alias which is supposed to assign an alias based on the parent name.
Tearing my hair out already here. Any views why this may not work?
EDIT: never mind, it needed the user_load piece.
Comment #6
spacegoat1701 commentedI'm using the latest 2.x dev, and I have tried andreiashu's pattern, but I can't get it to work.
Specifically, here's my code ($par_nid is a node ID loaded from the database):
Comment #7
zeezhao commentedHi. Please has anyone been able to do this with latest 2.x? I could not get it to work too.
I tried both methods i.e #2 and #3
The breadcrumb is set though in the child. But the child is not linked to parent node, and hence does not show up under parent. Looks like there is still one more setting to do...
edit: Also was running it as admin, so permissions should be fine...
also tried:
where $pid is nid of parent. Created breadcrumbs ok, but parent still not set....
Comment #8
dca123 commented@zeezhao
Can you post the code you have working? I'm having the same problem as well using #2 except the breadcrumb doesn't even appear...
Comment #9
dca123 commentedas well, @zeezhao
Can you confirm that the children don't exist by going to the parent node and clicking on the 'Children' tab? I've had it happen where no default view has been set to view the children on the same node display as the parent.
Comment #10
dca123 commentedI finally got this working on 2.x
Code below is based off of andreiashu's. Is tested and works...
Comment #11
zeezhao commentedThanks for this! Will try it out.
I looked into what I was doing last night and these are the additional issues...:
1. The way I did it before actually created the children under the new parent, and was visible from the "Children" tab. but not showing up in the view. So had to do:
$node->nh_children_view_display = 'default';
2. Now after doing that, because the new parent was cloned in code, for some reason its view was pointing to view of node from which it was cloned, but still had correct children under its "Children" tab! So some settings were not right on the clone.
Hope you stuff clears it up for me. Thanks.
Comment #12
rvarkonyi commentedMake sure you add this:
Should work.
Comment #13
zeezhao commentedThanks for all your help and comments. Got it to work eventually... As I was also cloning existing nodes, had to add some extra steps:
For cloned child:
For cloned parent, since it was also cloned from another parent node, and was not going to have any parent itself, I had to do:
Comment #14
achtonComment #15
skaughti can't say it enough
thankyou thankyou thankyou
i've spent alot of time trying to make this happen!! cheers.
Comment #16
dreamingX commented#13 works in D7 too!
Thank you! this saved me hours...
Comment #17
jbylsma commentedMade the title more ambiguous to hopefully steer future searchers.
Comment #19
chichilatte commentedSorry to reopen the issue, but it might be useful to point out that (in D6.2, NH 6.21 at least) if you do the node hierarchy changes after node_save(), the path alias doesn't take into account the hierarchy. Based on comment#10, just putting the nodehierarchy_menu_links array into the $node before saving worked perfectly for me...
Comment #20
ksemihin commentedPDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'link_title' cannot be null: INSERT INTO {menu_links}
What is wrong ???
Comment #21
emanaton commentedFor anyone stumbling on this and hoping against hope that someone bothered to put in the D7 solution, today is your lucky day!