Hi,I am Prashant Shrestha, a web developer based in Kathmandu. I specialise in Front end Drupal Development and I also do CSS. And I am passionate about Photography.
A Guide to CCK nodereference
Those who use drupal will probably agree that CCK(Content Construction Kit) is one of the most important modules that drupal has to offer. CCK allows us to create custom content types and add different kinds of fields to it. The power of CCK can be extended with the help of different contributed modules.
Who this article is for
This article is for those who have some experience with Drupal and are comfortable using CCK.
In this tutorial we will be exploring a very useful feature of CCK called Nodereference. The CCK nodeference helps to define a relationship among different content types. For this particular tutorial I have used two content types:
-
Artists
-
Album
The song content type is related to the artist content type and the album content type is related to the artist content type.
What you will need
- A Drupal 6 installation, you can download it here.
- CCK(Content Construction Kit) module
Unzip the CCK module that you downloaded to your modules directory inside sites/all/modules.
Enable the modules
The next thing that you need to do is enable content and noderereference in your modules page at http://example.com/admin/build/modules/list

Enable content which is the main cck module and also enable noderefernece,text, and option widgets.
Create your contents
First create a content type called artist. I haven't added any extra fields but you can add extra fields like artist image and so on.
Next create a content type called album. We want to relate this particular content type to the artist content type. An artist can have many albums and we want to display this relationship. The way we can do that is through nodereference. In the manage fields of the album content type add a nodereference type field. Since I want to reference this album content to the artist content I will select artist in the content type that can be references: select list.

In the settings page select the type of content that you want to reference in this case we want to reference the artist content type so we select artist.

Lets Add some content
Ok, time to add some content. I have added three artists Johnny Cash, Taylor Swift and Dixie chicks.
Now when I create a new album. I will get a select list which will display all the Artists that I have created.

I select Dixie Chick, and when I click on submit I get a link to the artist page in my album node.

In this way the album node Wide open spaces is now referenced to its respective artist node.
Enhance Node reference
The power of node reference can be felt when it is used with Views 2 Relationship. I will be writing a tutorial on that in the future. But there are ways to enhance its feature. I have found two modules that works great in conjunction with the CCK node reference.
Nodereferrer
It provides a counter part to the CCK node reference. It is sort of like a back reference. What is does it it lists all the nodes that has referenced that particular node. In our case it displays all the albums belonging to a particular artist.
To use nodereferrer download it here.
Place it in your modules directory and enable it.
Next edit your artist content type and in your manage fields add a node referrer type field.

Select the referring node and the field used to refer to the artist node.

Now u can see all the albums related to that particular artist in the artist page.

Using these you can create a Last FM type website.
Noderefcreate
Node Reference allows us to link a node to another node. But the catch is that the parent node has to exist first. Thats where Noderefcreate comes handy. Noderefcreate helps to bypass all that. With it we can reference a non-existent node.
So much for todays tutorial...Next time we will be discussing about the Views 2 relationship.
Related Posts
Subscribe
Get the latest updates
Subscribe via RSS
Subscribe via Email










16 Responses to "A Guide to CCK nodereference"
nice article.
Nice article to kick start the blog!! way to go bro!!
this is such a nice theme!! im all jealous now :P
Hey Bro......nice work.....Love the design of the blog.......
Thanks a lot bro.. :)
Good article man, well defined. Breaking the content with h3 looks pro. You will make a good blogger ;).
Let's start blogging for YIPL's site.
Thanks man....Ya we shud definitely start blogging for yipl's site...
Nice article.
i have a question an need help. When créate the "Wide open spaces" i want to show on the liste box the title of the reference and all of the album fields.
how can i write the return array(0=>('nid','') ...,
I dont quite follow..can you please elaborate..
Nice article.
"Next time we will be discussing about the Views 2 relationship" - where is this, please?
Good article, bro..
Although, in the second screenshot I think instead of album it should be - Artist.... :)
nice and clear, how would you reference an image field?
The way to do this with pure views is described on this drupal forums post
Thanks for the awesome tutorial on nodereference. There is a lot of information out there for Drupal 5, but not 6. This information cuts the mustard.
Thanks for the tutorial.
But I would like to know if there is the possibility to referrer a album to more than one artist.
Thanks
Post new comment