background image
HomeRecent PostsDrupalSearchTagsRSSContactAboutAccount

Creating parent and child relationships between nodes using the CCK node reference module

Eric.London's picture

This article will show you how to establish parent and child relationships between nodes. To start, you'll need to install and enable the CCK module, and node reference (which comes with CCK). Next, you'll need to create two node types (admin/content/types/add). For my example, I created two node types: Organization (parent) and Department (child), both consisting of a title and body (of course, you can add as many fields as you'd like).

Next, you'll want to add a new field to the parent node (organization) by editing the content type (admin/content/node-type/organization) and clicking on the "Manage fields" tab. Enter the information to add a new reference field on this screen. Add a label (ex: "Department Reference"), enter a field name (ex: field_ref_department), choose "Node reference" as the field type, and choose "Autocomplete text field" as the form element as shown below.

node reference add field 1

On the next screen you can choose the field options. For this example, I changed "Number of values" to "Unlimited" (to allow a one to many relationship), selected "Department" in the list of content types that can be referenced, and saved these settings.

node reference add field 2

Now, if you create a new parent node (Organization: node/add/organization), you'll have the option of adding pre-existing child (Department) nodes. As you start typing the title of the child nodes in the node reference auto-complete field, it will automatically populate the field with nodes that match. Of course this will only work if you have already created child nodes. At this point adding relationships is a two step process.

node reference add parent node 1

To improve usability, you can install the Popups API (http://drupal.org/project/popups) and Add and Reference (http://drupal.org/project/popups_reference) modules which allow you to add child nodes without leaving the node/add/PARENT-NODE screen. After installing and configuring these modules, you'll be able to add child nodes directly from the node/add/PARENT-NODE screen by clicking on the "Add new: Add Child Node link", which spawns a lightbox window to add a child node.

node reference add parent node 2

Here is a screenshot of a parent node view after adding child relationships:

node reference parent node view

Modules are unmaintained

Unfortunately, both the "Popups: Add and Reference" and "Popups API" are unmaintained at the moment.

Great blog Eric. Just in case

Great blog Eric. Just in case anyone has had the same problems I've had with Popups API, there is a better solution.

Noderelationships module:
http://drupal.org/project/noderelationships

It adds a bunch of enhancements to nodereference fields. Including:
- Search and reference (single and multiple value selection)
- Create and reference
- Translate and reference

It works great with filefield and imagefield, where PopupsAPI gave me issues.

Node Relationships module

The Node Relationships module has a lot of potential and I was so excited to try it out. But it was a serious jerk to install and the JQuery version it requires clashed severely with Lightbox2 which I need for my image gallery. I even tried a suggested patch (http://drupal.org/node/411162) which allowed the popup functionality of Node Relationships to work but then disabled the cool functionality of the Lightbox2 feature. Lightbox2 technically worked afterward, but only displayed a popup window rather than producing the darker background effect that makes it worthwhile visually. After almost 3 hours trying to get this all to play nice together, I gave up and found the Popups modules, which installed super easily and work like a charm! Thanks so much for this blog. : ) I wish I had found it sooner!

Many thanks for the detailed

Many thanks for the detailed outline. I had it starred in my RSS for a few days and just today I needed to solve this issue. Great step-by-step with pictures!

Lots of options in Drupal 6, though 7 will be very different.

This is an old post, but it's ranked pretty high on Google, so I figure I should leave some info I've been looking into... though I still don't have an answer.

http://drupal.org/project/nodehierarchy
http://drupal.org/project/relativity
http://drupal.org/project/noderelationships
http://drupal.org/project/flexifield

http://drupal.org/project/popups_reference
http://drupal.org/project/nodereference_explorer

It's a lot to consider when trying to find a solution.

Eric.London's picture

Drupal 6.x

Thanks for the links. This post is not "old", just Drupal 6.x. [see my tags]