I looking for the best way to implement a block that lists all terms of a certain vocabulary. Each term should link to page that lists all nodes associated with that term. Any help would be greatly appreciated. Thanks!
See here for a great tutorial to achieve exactly what you want
http://chrisshattuck.com/blog/how-add-block-menu-tags-or-other-taxonomy-terms-drupal-site
The easiest way to approach this would probably be to use Views, and simply create a new view of the type "term". Here's a quick example which assumes that you have some basic familiarity with the Views UI:
- Visit Views > Add (build/views/add), give your new view a name, and select Term from the "View type" radio buttons.
- On the next page, start by adding a Taxonomy: Vocabulary filter and selecting your vocabulary in the filter settings.
- Add a Taxonomy: Term field and enable the Link this field to its taxonomy term page option in the field settings. You might also want to remove the field's label, since this is just a simple listing.
- You probably want your display to display all terms in your vocabulary, so change the "Items to display" 0 (unlimited). By default, new views only display 10 items at a time.
- Check out the Live preview below to see if it's outputting what you need.
- Add a new Block display using the dropdown on the left side of the Views UI.
- Give your new block a name in the "Block settings" area. This is the description that will appear on Drupal's block admin page.
- Save your view and visit admin/build/block to place and configure your block.
It's worth noting that Views does indeed have some overhead, but in my experience, its flexibility and ease-of-use far outweigh the relatively minor performance hit.
If you'd like to avoid using Views, you could write a pretty simple custom module using hook_block() and adapting http://drupal.org/node/247472. If you'd like, I can edit this answer with an example module based on that.
(Posting this as another answer, since this is a different approach than my first answer.)
As I mentioned above, here's another approach involving a custom module based on the code at http://drupal.org/node/247472. You could also just drop that code into a custom block with the "PHP" input format selected, but that's generally considered to be bad practice.
Add a new folder in sites/all/modules called vocabulary_block. Customize and add the following two files:
vocabulary_block.module
<?php
/**
* @file
* Exposes a block with a simple list of terms from [vocabulary].
* Each term is linked to its respective term page.
*/
/**
* Lists terms for a specific vocabulary without descriptions.
* Each term links to the corresponding /taxonomy/term/tid listing page.
*/
function vocabulary_block_get_terms($vid) {
$items = array();
$terms = taxonomy_get_tree($vid, 0, -1, 1);
foreach($terms as $term) {
$items[]= l($term->name, "taxonomy/term/$term->tid");
}
if(count($items)) {
return theme('item_list', $items);
}
}
/**
* Implementation of hook_block().
*/
function vocabulary_block_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$blocks[0]['info'] = t('List of [vocabulary] terms');
return $blocks;
case 'view':
if ($delta == 0) {
$vid = 43;
$block['subject'] = t('[Vocabulary]');
$block['content'] = vocabulary_block_get_terms($vid);
}
return $block;
}
}
vocabulary_block.info
name = Vocabulary Block
description = Exposes a block with a simple list of terms from [vocabulary]. Each term is linked to its respective term page.
; Core version (required)
core = 6.x
; Package name (see http://drupal.org/node/101009 for a list of names)
package = Taxonomy
; Module dependencies
dependencies[] = taxonomy
Notes
Be sure to change
$vid = 43;
to reflect the ID of the vocabulary that you'd like to load. You can find the VID by visiting admin/content/taxonomy and looking at the destination of the edit vocabulary link for your vocabulary. The VID will be the last fragment of that URL: admin/content/taxonomy/edit/vocabulary/[vid].I wouldn't normally hard-code the $vid into the module itself. However, setting up the necessary Drupal variable and administration form (to allow users to select a vocabulary from the Drupal interface) would be overkill for this answer.
For your own documentation purposes, don't forget to search/replace [vocabulary] in those two files and use your own vocabulary's name instead.
This method may not necessarily be more performant than the Views method I described earlier, especially once you start considering caching, optimization, etc. Since performance is a priority, I recommend thoroughly testing a variety of different methods on this page and choosing whichever one is fastest for you.