---
title: "Creating Tree Data Structure in Drupal · Appnovation"
url: "https://www.appnovation.com/insights/creating-tree-data-structure-drupal"
description: "How to build hierarchical tree node structures in Drupal without taxonomy, avoiding UUID and Features export problems."
---

Article

# Creating Tree Data Structure in Drupal

[![](https://cdn.sanity.io/images/ey850001/production/2d107293dd3dd11fd0c7f11462129ad68346a665-68x68.jpg?w=68&auto=format) youfei youfei Marketing Drupal Developer](/people/youfei-youfei.md) May 04, 2016  
 3 min read 

![Creating Tree Data Structure in Drupal](https://cdn.sanity.io/images/ey850001/production/de67566591761a0a8bdc3f45edc652db685cc4c3-1400x400.jpg?w=1400&auto=format) 

On this page

## On this page

Sometimes you don’t want to use taxonomy for a building node structures of a tree, as it creates huge problems when you're trying to export them as features and involves things like UUID, which is not perfect yet.

There is a project on Drupal.org utilizing tree structure with eneity reference module, however it is currently in lack of maintenance, maybe a bit difficult to use.

[https://www.drupal.org/project/tree (opens in new window)](https://www.drupal.org/project/tree)

This issue is explaining how this module is working.

[https://www.drupal.org/node/1862008 (opens in new window)](https://www.drupal.org/node/1862008)

However, a more manageable way may be creating a table in database for tree-structure management, and then associate the node id to each items in the table, in that method, the whole tree-structure is manageable by the Features module.

There are some different ways of implementing the data structure:

1.  Adjacency List
2.  Materialized Path
3.  Nested Set
4.  Closure Table

Each method has its pros and cons, depending on how often the data is saved and loaded. This form can conclude some characteristics of each method of implementation:

Read Speed

Write Speed

Space efficiency

Difficulty of implementation

Adjacency List

Low

High

High

Low

Materialized Path

Low

High

High

Medium

Nested Set

High

Low

Low

High

Closure Table

High

High

Very Low

High

More detailed analysis will be way off topic, for smaller projects with small metrics, each of them is a reasonably good method to use.

Some detailed comparison of those methods can be found here:

[http://www.slideshare.net/billkarwin/models-for-hierarchical-data (opens in new window)](http://www.slideshare.net/billkarwin/models-for-hierarchical-data)

## Adjacency List

We will start with the easiest implementation: the adjacency list.

The idea is simple: have a field in the database to store the parent node.

First, we have to implement hook\_schema() in the .install file in a newly created custom module. Let’s name the module mytree.

function mytree\_schema(){

$schema\[‘mytree\_table’\] = array(

'description' => 'The table for storing a custom tree structure.',

'fields' => array(

'tid' => array(

'description' => 'The identifier for a tree node.’,

'type' => 'serial',

'unsigned' => TRUE,

'not null' => TRUE,

),

'pid' => array(

'description' => 'The parent tree node id.’,

'unsigned' => TRUE,

'not null' => TRUE,

),

‘name’ => array(

'description' => 'The name of the node.',

'type' => 'varchar',

'length' => 255,

'not null' => TRUE,

'default' => '',

),

'nid' => array(

'description' => 'The drupal node id which the tree node is associated to.',

'type' => 'int',

'unsigned' => TRUE,

),

'weight' => array(

'description' => 'The weight of entry, affecting the display orders.',

'type' => 'int',

'not null' => TRUE,

'size' => 'normal',

'default' => 0,

),

),

'primary key' => array('tid'),

);

return $schema;

}

Note the nid will be storing node id of any Drupal nodes, in this way the existing nodes can be structured with our custom module.

When you have the schema implemented, enable your custom module, so that you can verify the table in phpmyadmin by searching for the table name : mytree.

Next step is to create some apis for the data structure, say a CURD (create,update,read,delete) cycle.

function mytree\_data\_create($pid,$name,$nid,$weight)){

$record = array(

‘pid’ => $pid,

‘name’ => $name,

‘nid’ => $nid,

‘weight’ => $weighg,

);

drupal\_write\_record(‘mytree\_table’, $record);

}

function mytree\_data\_update($tid,$pid,$name,$nid,$weight){

$record = array(

‘tid’ => $tid,

‘pid’ => $pid,

‘name’ => $name,

‘nid’ => $nid,

‘weight’ => $weight,

);

if($tid != NULL){

db\_merge(‘mytree\_table’)->key(array('tid' => $tid))->fields($record)->execute();

}else{

db\_merge(‘mytree\_table’)->key(array('name' => $name))->fields($record)->execute();

}

}

function mytree\_data\_read(){

$query = db\_select(‘mytree\_table’, 't');

$result = $query->fields('t')

->orderBy('weight')

->execute()

->fetchAll();

return $result;

}

function mytree\_data\_delete($tid){

db\_delete(‘mytree\_table’)->condition('tid',$tid)->execute();

}

These 4 are not all API functions you might going to need, write some more based on your needs retrieve items by name, id...etc

Some other interesting ways of implementing tree structure will be coming soon!

[![](https://cdn.sanity.io/images/ey850001/production/2d107293dd3dd11fd0c7f11462129ad68346a665-68x68.jpg?w=68&auto=format) youfei youfei Marketing Drupal Developer · Appnovation](/people/youfei-youfei.md) 

## Keep reading

[![Abstract graphic of white lines spiralling around a green-outlined triangle on a dark background](https://cdn.sanity.io/images/ey850001/production/e561f763d9608b75872d9b71d0674353c36a06fb-1201x630.png?w=640&auto=format)](/insights/2020-11-finding-your-way-build-your-migration-roadmap-drupal-9.md)

 Article · 4 min Nov 05, 2020 

### [Finding Your Way: Build Your Migration Roadmap to Drupal 9](/insights/2020-11-finding-your-way-build-your-migration-roadmap-drupal-9.md)

As we move toward 2021 and continue to reckon with COVID-19, digital-first companies need a plan to meet continuously shifting customer needs. Drupal 9 will help you do it.

[Nathan Gervais](/people/nathan-gervais.md) Article 

[![Abstract graphic of white lines spiralling around a green-outlined triangle on a dark background](https://cdn.sanity.io/images/ey850001/production/e561f763d9608b75872d9b71d0674353c36a06fb-1201x630.png?w=640&auto=format)](/insights/2020-11-your-tech-stack-standing-way-your-success.md)

 Article · 4 min Nov 19, 2020 

### [Is Your Tech Stack Standing In The Way Of Your Success?](/insights/2020-11-your-tech-stack-standing-way-your-success.md)

Your business potential and technology needs (should) go hand in hand. You can plan business outcomes all you want, but if they’re not mapped strategically to the technology you need to actually make them happen, you’ll fail. Easy as that.

[Nathan Gervais](/people/nathan-gervais.md) Article 

[![Abstract graphic of white lines spiralling around a green-outlined triangle on a dark background](https://cdn.sanity.io/images/ey850001/production/e561f763d9608b75872d9b71d0674353c36a06fb-1201x630.png?w=640&auto=format)](/insights/2020-10-move-drupal-9-and-exceed-cxpectations.md)

 Article · 5 min Oct 20, 2020 

### [Move to Drupal 9 and Exceed CXpectations](/insights/2020-10-move-drupal-9-and-exceed-cxpectations.md)

As we move toward 2021 and continue to reckon with COVID-19, digital-first companies need a plan to meet continuously shifting customer needs. Drupal 9 will help you do it.

[Nathan Gervais](/people/nathan-gervais.md) Article 

## Get the thinking,  
*before everyone else.*

One considered email a month. New essays, field notes from live engagements, and the occasional strong opinion. No noise.
