Node-specific blocks

A node-specific block

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!

An album description
Oops...
Hide the Description field
View settings
Argument settings

2 Comments

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.

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 :-)

Add new comment