« All about panels3 | Main | Redirect to a data use agreement before download »

January 12, 2010

Add a block view that leverages node references using views arguments

Here is a 'how to' that I am all to happy to undertake, if only because it means I have achieved something.

Overview:
I've got two content types, bibliography entries (biblio module), and research sites (CCK module). I have a node reference field in research sites which references the appropriate bibliographic entries.

My goal is to create a block which will appear in the right-hand column when a user is viewing a publication. The block will dynamically populate with "Related Research Sites". The block will not appear if there are no Research Site to Bibliography references OR if the use is viewing non-bibliography content type.

Modules:
Drupal 6.15
Biblio 6.x-1.9
CCK 6.x-2.6
Node Reference 6.x-2.6
Views 6.x-2.8

Assumptions:

The ability to create custom content and node references within CCK. The biblio module is installed and populated with (at least) a few publications.

In this case, I have a custom content type called Research Sites which contains the following Node reference to the Bibliography content type:

Label: Biblio Reference
Name: field_research_site_bib_ref
Type: Node reference

Steps:

1. Create a View (Administer > Site Building > Views > Add; /admin/build/views/add)
View Name: biblio_related_research_sites
View Type: Node

2. Configure the new view.
Add a Filter:
- Node: Published = Yes

Add a Field:
- Node: Nodereferrer: by field
- Using Field: field_research_site_bib_ref (Biblio Reference)

Note: I selected the node reference from Research Site to Bibliography. If do not select any fields here, all nodes that reference a particular publication will populate the block.

Add an Argument:
- Node: Nid
- Under "Action to take if argument is not present" select "Provide default argument" and choose "Node ID from URL"
- Everything else should be default.

Note: I got this crucial bit from the Geeks and God folks.

Edit Basic Information:
- Change Distinct from No to Yes.

Add a Block Display.

Save your edits.

Here is what my view looks like:

leaf graphic

(Right-click > view image for the fullsize image)


3. Administer the new block.

Go to Administer > Site Building > Blocks (admin/build/block) and drag the new block, "biblio_related_research_sites: Block", to the Right Sidebar area.

Configure the block:
- Block Title: Related Research Sites
- Under "Show block on specific pages" select "Show if the following PHP code returns TRUE (PHP-mode, experts only)."
- Enter the following code:


<?php
$match = FALSE;
$types = array('biblio' => 1);
if (arg(0) == 'node' && is_numeric(arg(1))) {
  $nid = arg(1);
  $node = node_load(array('nid' => $nid));
  $type = $node->type;
  if (isset($types[$type])) {
    $match = TRUE;
  }
}
return $match;
?>

Save Block.

4. Assuming you have a node reference within a research site to a publication, when you go to view the publication, a block should appear on the right listing the appropriate research site.

5. Here's the product with a Related Research Sites block in the right-hand column.

leaf graphic

(Right-click > view image for the fullsize image)


Alternative methods:

I am aware of at least 2 analogous solutions and I am sure others exist as well.

Using Panels 3 (specifically, Mini Panels) would no doubt get you here. I stayed away from this because I am new to Drupal and therefore believe the KISS method is best. The Panels 3 method fails this test because of the need to install the CTools and Panels modules. Besides, I'm pretty sure you need to know how to configure the View anyways...

A simpler option is to use the node referrer module. This module leverages the fact that you've already created a node reference from research site to bibliography and append the related research sites beneath the original publication. I dislike this for two reasons, 1) it is not as pretty because the styling is not inherited from the biblio module and 2) the user will have to scroll to the end of the publication in order to find the related research sites.

Posted by kkwaiser at January 12, 2010 09:56 AM

Comments

Login to leave a comment. Create a new account.