Hello Drupalers:

My site requires a content type that is basically an container, with a list of items - think of something like a playlist. Each list has a title, maybe a description, and then a random length list of "things." One list might have 1 item, another might have 30...

I was planning on writing my own custom module, but I would really like to use CCK. However, I can't quite figure out how to create this content type using CCK, or if it is even possible... any ideas?

Thanks!
Michael

Comments

gryste’s picture

One way is to have a plain text field with an HTML list<ul> or <ol> ...

  • Item 1
  • Item 2

Another option, which might offer more flexibility down the line, is to create two new node types with CCK. To use your playlist analogy, one would be 'album' and the other 'track'. You could associate all of the 'tracks' with an 'album' node. The album node could list (using the views module) all of the tracks associated with it.

In the future you could add additional fields to the 'track' node type, such as song length, rating and so on.

newdru’s picture

How do you actually associate the tracks with the album? Could you be specific stating steps?

I ask because i know "what" the solution needs to be theoretically. that's not the problem. the problem is implementing it within druapl. so how do i associated one content type with another in a cohesive manner?

Each of those tracks is going to have to point to an album node in a separate table. So how do i make that happen on the track form when i create the track?

thanks for any specifics you can provide..

gryste’s picture

Using CCK create 'node reference' field in the 'track' node. In that field you can choose the appropriate 'album' node when you create each track.

To get the list of each 'track' referencing that node, you can create a view that looks for nodes with a CCK 'node reference' field that points to the current 'album'. I'm not entirely sure of the details, but you can pass parameters through the URL to customize the view. So you don't need a separate view for each and every album. One view that received a parameters (album node id in this case) and then searches for all node that have a CCK field that references that node.

newdru’s picture

I want to believe you on this :-)..

but when you say things like "i'm not sure of the details" that doesn't lead me to believe that you've actually done it in which case you're not exactly sure if it works..

not trying to be an arse, but I tried node reference back in november in 4.7 and it never worked in views... it worked as far a allowing me to select the referred to cck from a referrer cck FORM.. but when it came time for views to pull the info it didn't seem to work... it's possible it's more stable now though..

which begs a different question.. the example in this thread uses 'album' so that might not be the best example for my next question but lets use it anyway..

Lets say you have 300 albums or maybe 1000. When it comes time to use the nodereference field is it going to create a select list of 1000 items on my form? I guess what i'm getting at is i wonder if nodereference is meant for smaller subsets of referred to items.. then again, if you weren't using node reference, you'd still be running up against the same problem so i guess that's more or less a universal issue (just thinking out loud)..

thanks for the replies..

gryste’s picture

I've gone through the process and written up everything I did, step-by-step.

(Drupal 5.x)

Overview

The description turned out to be pretty long so here's an overview of what we're doing.

  • Create an 'album' node type
  • Create a 'track' node type which can reference an album
  • Create a block (using Views) that will list all of the tracks that reference the current Album node
  • Enable that block on node pages

Detailed Instructions

  1. Create a new node-type call it 'Album'
  • Go to Administer > Content types > Add content type
  • Fill in Name 'Album'
  • Fill in Type 'album'. Note they don't need to be the same.
  • Press Save Content Type
  • As above create a new node-type. Name 'Track', Type: 'track'
  • In the Content Types list you should see both Album and Track.
  • Here's the most important point. We need to add a CCK field to 'Track' nodes that will allow us to link them to an album. This is called a 'node reference' field.
    • Click on 'Track', then 'Add Field'
    • Label: 'On album'
    • Field Type: Choose 'Node reference > Autocomplete Text Field'. You can choose select list, but if you have 100 albums that might get messy.
    • Click Create Field
    • We want to restrict the 'On album' field to reference only 'Albums'. After creating the field you will see more options for that field. Under 'Content types that can be referenced' pick 'Album'. Save Field Settings.
  • At this point you should have two node types, one of which can reference the other. Now we need to create the view:
    • Administer > Site building > Views > Add
    • Basic Information > Name: 'Album_tracks'
    • Create a block that will appear on the album pages
      • Choose 'Provide Block'
      • View Type: List View
      • Title: Tracks
      • Nodes per block: 20.
      • If you think you'll have lots of tracks, you can enable the 'more link'
    • Fields. Choose what will be displayed in the block.
      • Add field: Node title
      • If you later add more fields to the 'Track' nodes those will be available here.
    • Arguments. Here's how we choose only the tracks that refer to this album.
      • Add Argument: Choose 'Node reference On album (field_on_album)'
      • Under Argument Handling Code type in $args[0] = arg(1);. See Views documentation for more options.
    • Click Save
  • All we need now is to enable the block
    • Administer > Site Building > Blocks
    • Find 'album_tracks' and choose 'Content'
    • Save blocks
    • In the block list, click 'configure' next to album tracks.
    • Under 'Show block on specific pages' choose 'Show only on listed pages' and then type in 'node/*'. This way it will only show on node pages. Save Block.
  • Now create some content.
    • Create content > Album. Title 'Best of Drupal' and the body.
    • Create content > Track. Do this twice. Titles 'I love View' and 'CCK rocks'
  • Voila! If you visit 'I love View' or 'CCK rocks' you should see "On Album: Best of Drupal". Click on that it takes you to "Best of Drupal" where you will see both tracks listed
  • newdru’s picture

    thanks for typeing that in!!!

    i'll try it and get back to you soon...

    VenDG’s picture

    Thanks for this explanation. I was able to use it for my own 'child to parent', 'parent to child', node associations and it worked perfectly.

    Enwebb’s picture

    This was exactly what I was looking for and it works. However, when I enter 'node/*' under 'Show only on listed pages' I get the following error in my 'Create Content Page' (q=node/add):

    * warning: Invalid argument supplied for foreach() in C:\www\drupal51\modules\node\node.module on line 504.
    * warning: implode(): Bad arguments. in C:\www\drupal51\modules\node\node.module on line 508.
    * user warning: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 query: SELECT n.nid, n.vid, n.type, n.status, n.created, n.changed, n.comment, n.promote, n.sticky, r.timestamp AS revision_timestamp, r.title, r.body, r.teaser, r.log, r.format, u.uid, u.name, u.picture, u.data FROM node n INNER JOIN users u ON u.uid = n.uid INNER JOIN node_revisions r ON r.vid = n.vid WHERE in C:\www\drupal51\includes\database.mysql.inc on line 172.

    vanvemden’s picture

    great example

    gmclelland’s picture

    -gryste

    The write up you included is excellent for showing the referenced nodes below the content on the node id page, but what would you suggest for someone who needs to be able to add child nodes on the same form?

    For example,
    In your example you have Albums and Tracks. How could you add tracks to an album from the add a new album page?

    Is this possible?

    Thanks for your help.

    plus8web’s picture

    hi all,

    am new to drupal but loving it so far :)

    I'm also doing something with artists, albums and tracks but in drupal 6 so could follow this guide to a point but once it got to views it's all different.. If anyone else is using v6 too then the post below might also be useful.

    Between this guide and the link below I managed to get relational lists on the go - thanks for all the authors :)

    http://drupal.org/node/289738

    best regards,

    Bernie