Adding a related content view in Drupal 7

Update

The most gracious commenter “nordic material” provided working instructions for performing this task using Views 3 in D7. I just went through the directions and they work perfectly. Below is a recap. Thanks again Nordic Material!

  1. Create view (block)
  2. Add fields (title, a small image, date, whatever you like). Or teasers, what works as well.
  3. “Arguments” is from now on called “Contextual filters”, so go there.
  4. Add the filter: “Content: Has taxonomy term ID”
  5. Configuration on this filter: -> When the filter value is NOT in the URL: -> Provide default value -> Type: Taxonomy Term ID from URL -> under that, check the box “Load default filter from node page, that’s good for related taxonomy blocks.” -> now check the boxes of the taxonomies you want involved here
  6. Now you need to add the second filter in order to exclude the current node from the list. Click [+] button of the contextual filters section.
  7. Select Content: Nid
  8. Set the ‘when argument is not present’ to ‘Provide default argument’
  9. Set the ‘Default argument type’ to ‘Content ID from URL’
  10. Go to “MORE” , and check the “Exclude” box.
  11. Add a block to your view and embed the block on your node pages
  12. Save your view

Note: The instructions below are outdated and applicable only to Views 2/D7. See the instructions above for latest method.

The way nodes are structured in Drupal 7 is significantly different than in D6. All node attributes are now fields, including taxonomy. I dig it,  but it means one will need to port lots of taxonomy related code from D6.

My task du jour was to setup a “Related Terms” block using views. There is a decent documentation article for D6here. I needed to make this work for D7. Listed below is the code. One key difference, however, is the D6 example is able to reference all  terms via the “$node->taxonomy” obj. where as in D7 you can only reference explicitly defined fields “$node->field_taxonomy_name”.

** Disclaimer ** The Relevant Content module does a fine job of this so this is more of an academic exercise. Most of this is copypasta from Drupal.org. I just modified for D7. I will also contribute this code on Drupal.org documentation

 

Creating a related content block view in Drupal 7

Creates a related content views block to list content with the same taxonomy of the current node:

  1. Create View
  2. Argument “Taxonomy: Term ID”
  3. Argument Configuration:
    • Provide default argument
    • PHP Code:Add this code:
       $node = node_load(arg(1)); if($node) { //Be sure to rename ->field_taxonomy_name to your field name foreach($node->field_taxonomy_name[LANGUAGE_NONE] as $term) { $terms[] = $term['tid']; } return implode('+',$terms); } else { return; } 
    • Check ON: Allow multiple terms per argument.
    • Check ON: Reduce duplicates
  4. Add Display: Block
  5. Add Field: Title
  6. Optional — Add Filter: Content Type (whatever type you’d like to filter)
  7. Now you need to add the second argument in order to exclude the current node from the list. Click [+] button of the arguments section.
  8. Select Node: Nid
  9. Set the ‘Action to take if argument is not present’ to ‘Provide default argument’
  10. Set the ‘Default argument type’ to ‘Node ID from URL’
  11. Check ‘Exclude the argument’
  12. Add a block to your view and embed the block on your node pages
  13. Save your view
  14. PROFIT!
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s