Home

Panda

  • Home - Latest posts
  • Portfolio - My Drupal work
  • Gallery - My photos
  • Multimedia - My videos
  • Ponderings - My blog archive
  • Contact - Email me
Home » Ponderings » Node-specific blocks

Node-specific blocks

Pondered at 1:40am on the 26th June, 2010

I've found myself using a particular Views technique a lot recently and so decided to post my first Drupal tutorial about it.

Imagine the following scenario:
You have a website with a photo gallery made up of Album nodes (with each Album node displaying multiple photos). You want to show a description of the album in a sidebar block for each album you create. You therefore create a new block, write the description in the body field, then set the block to display on all Album pages. Easy, right?
What you realise though, is that the same description is now being shown for each album. You not only have to create a different block for each of your albums, but also setup the blocks to display only on the album pages to which they refer.
If only there was an easier way...

There is! And here's how you do it.
First, you need to add a new field to your Album content type called Description (this'll be where you enter the description for each Album). As we don't want the description being displayed in the node itself, but rather in a block in the sidebar, we set it to hidden on the Display Fields tab.

Now, create a view called Album_Description of the type Node, then add a block display to this view. I'll let you decide on the exact settings for the view, but I recommend using an unformatted fields style, displaying 1 item. Add the new Description field to the view, then setup some filters to only show published nodes of type Album.

Now comes the tricky part; you only want to show the description for the album you're currently viewing. To do this, add the Node: Nid argument to the view. Now normally arguments are used with page views and you'd specify the node ID in the URL (so the view would only show fields belonging to that node). However, because this is a block we can't specify the node ID in the URL, the block has to get it itself.
To allow it to do this, we provide it with a default argument, specifically, the Node ID from URL. What this means is that for each page the block is displayed on, it'll see if the URL (not the path alias) is of the form 'node/[nid]' and if so, will use that node ID as the argument.
For example, if the current page is not a node (i.e. the contact form page), there won't be a node ID so the block won't display anything. If the current page is a node, but not an Album node, it'll try to use the node ID, but since our filter prevents it from displaying anything other than Album nodes, it won't show a thing. Finally, if the current page is an Album node, it'll use the node ID and display the description field from that node!

Save the view, add the newly-created block to the sidebar of your choice, then navigate to an album node that you've entered a description for. You'll see the description displayed in your new block and, best of all, when you navigate to a different album, it'll display the different description instead. No more having to create different blocks. Yay!

Feel free to leave a comment below if this tutorial helped you, or if you have any questions about it. Enjoy creating your new, node-specific blocks!

  • drupal

Great tutorial! Perhaps worth

Submitted by Andy Carter at 6:27am on the 30th June, 2010

Great tutorial! Perhaps worth mentioning that if you're using the Context module then you can set the view to react only when the correct content type is being shown. While this doesn't have a big effect on the way things work (some performance benefits maybe) it would make the block's usage clearer from the admin perspective. This could be useful if you end up with a site with a lot of blocks.

  • reply

Many thanks for this! "To

Submitted by Richard@Home at 1:31am on the 3rd July, 2010

Many thanks for this!

"To allow it to do this, we provide it with a default argument, specifically, the Node ID from URL."

really helped me out :-)

  • reply

Post new comment

The content of this field is kept private and will not be shown publicly.

Tags

drupal holiday miscellaneous movie new zealand personal trailer

MicroBlog

10:43am 4th Sep 2010
Difference between #Facebook's Share & Like buttons: http://is.gd/eTZ9A Good read, especially for us web developers who don't use FB !webdev
2:26pm 3rd Sep 2010
@freemjd: Have patience ;)
Posted in reply to freemjd
1:43pm 3rd Sep 2010
@freemjd: Not at all, our mission is simply to introduce people to Him :)
Posted in reply to freemjd
MicroBlog
Copyright © 2010 - Peter Anderson