Creating hierarchical structure in Drupal content pages

leave a comment »

In a typical website structure , it is often found that there is  a main content type and several other content-types  linked to that content in a parent-child relationship. So you may want to store a unique identifier in the main content type and make this accessible to all its children content-types. You may also want  the urls of children to be defined with a proper structure linking them to the parent.

A typical example would be  movie page being your main content and synopsis of the movie as child page.

You can do this in drupal by using a module called node-hierarchy. Now if you enable this module you can go the page and set its’ parents/children. But my problem was a little complicated. I had a data set of around 8K pages i.e.  almost 8000 nodes and I wanted to create 3 child nodes of each node using a script. It is not possible to do this manually.

Here’s the code that I came up with:


//get all the nodes that you wish to be the parent nodes

foreach($nodes as $nid){

$plid = _nodehierarchy_get_node_mlid($nid,true); //parent node

//create a new node
$node = new StdClass();//creating a bare node
$node->type = 'whatever';
$node->status = 1;//give it a published staus
$node->title = "whatever";
$node->created = time();
$node->comment = 1;
$node->promote = 0;
$node->sticky = 0;
$node->uid = 1;// the user id who owns the node
$node->language = 'und';


$nodehierarchy_menu_links = _nodehierarchy_default_menu_link($nid, $plid);
$nodehierarchy_menu_links['pnid']=$nid; // <----- Give the menu link the parent id
$nodehierarchy_menu_links['hidden'] = FALSE;
$nodehierarchy_menu_links['enabled'] = TRUE;

$nodehierarchy_old_menu_links = $nodehierarchy_menu_links;
$nodehierarchy_old_menu_links['enabled'] = TRUE;
$nodehierarchy_old_menu_links['link_title'] = '';
$nodehierarchy_old_menu_links['expanded'] = '';
$nodehierarchy_old_menu_links['description'] = '';
$nodehierarchy_old_menu_links['customized'] = '';

$node->nodehierarchy_menu_links[] = $nodehierarchy_menu_links;
$node->nodehierarchy_old_menu_links[] = $nodehierarchy_old_menu_links;

_nodehierarchy_save_node($node); // <---- Let nodeheirarchy do it's thing
$newnodeid = $node->nid;



Now if you go to your parent node page, you should be able to see the children as well . You can also add a piece of code that will create the url_alias as you wish to be for each child node.

For example:


$path = array ();
$path['source'] = 'node/'.$newnodeid;
$path['alias'] = '/parenturl/content-type/childurl';



Written by rationalspace

April 8, 2013 at 11:39 am

Posted in Drupal

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: