Using Views To Create DMOZ-Style Sub-Categories

Photo of Greg Harvey
Mon, 2009-06-01 12:31By greg

This is a quick Drupal 6.x, Views 2.x blog tutorial about creating the classic DMOZ-style sub-categories plus node listings above/below. Something Drupal does not do out of the box, but is fairly easily achieved.

(Thanks to longwave in #drupaluk on IRC for suggesting this approach.)

You already have standard taxonomy listings (a list of nodes per term) which is already optionally provided using the Views module (so you can control the output without writing any code). There are code-based ways of achieving the following, but you don't actually need to write anything.

With the Views module (Views 2.x only, I'm afraid) create a new view of type "Term". Add a relationship of type "Taxonomy: Parent term". Add an argument of type "Taxonomy: Term ID". For the argument settings under "Action to take if argument is not present:" select the "Provide default argument" radio and select "PHP Code" as the type. In the resulting textarea simply type this:

return arg(2);
?>

Note, you don't actually want the tags.

Why? Because the taxonomy term page where our resulting block will appear is always taxonomy/term/%term so we can rely on arg(2) to return a valid term ID as long as we are on a term page (which we can do by making sure our block only appears on taxonomy/term/* in the block configuration settings).

Add a field, "Taxonomy: Term", link it to the term itself. Finally add another display of type Block and save. That's it. Go to blocks, position it where you want it, set the configuration for the block as I just described so it only appears on other term pages and you're done!

Or you can just import my code and work from there:

$view = new view;
$view->name = 'taxonomy_subcats';
$view->description = 'Shows a grid of sub-categories for a category as a block.';
$view->tag = '';
$view->view_php = '';
$view->base_table = 'term_data';
$view->is_cacheable = FALSE;
$view->api_version = 2;
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->override_option('relationships', array(
'parent' => array(
'label' => 'Parent',
'required' => 0,
'id' => 'parent',
'table' => 'term_hierarchy',
'field' => 'parent',
'relationship' => 'none',
),
));
$handler->override_option('fields', array(
'name' => array(
'label' => '',
'alter' => array(
'alter_text' => 0,
'text' => '',
'make_link' => 0,
'path' => '',
'alt' => '',
'prefix' => '',
'suffix' => '',
'help' => '',
'trim' => 0,
'max_length' => '',
'word_boundary' => 1,
'ellipsis' => 1,
'strip_tags' => 0,
'html' => 0,
),
'link_to_taxonomy' => 1,
'exclude' => 0,
'id' => 'name',
'table' => 'term_data',
'field' => 'name',
'relationship' => 'none',
),
));
$handler->override_option('arguments', array(
'tid' => array(
'default_action' => 'default',
'style_plugin' => 'default_summary',
'style_options' => array(),
'wildcard' => 'all',
'wildcard_substitution' => 'All',
'title' => '',
'default_argument_type' => 'php',
'default_argument' => '',
'validate_type' => 'none',
'validate_fail' => 'not found',
'break_phrase' => 0,
'not' => 0,
'id' => 'tid',
'table' => 'term_data',
'field' => 'tid',
'validate_user_argument_type' => 'uid',
'validate_user_roles' => array(
'2' => 0,
'3' => 0,
'4' => 0,
'6' => 0,
'5' => 0,
'8' => 0,
'7' => 0,
),
'relationship' => 'parent',
'default_options_div_prefix' => '',
'default_argument_user' => 0,
'default_argument_fixed' => '',
'default_argument_php' => 'return arg(2);',
'validate_argument_node_type' => array(
'webform' => 0,
'image' => 0,
'page' => 0,
'story' => 0,
'video' => 0,
),
'validate_argument_node_access' => 0,
'validate_argument_nid_type' => 'nid',
'validate_argument_vocabulary' => array(
'1' => 0,
'2' => 0,
'3' => 0,
'4' => 0,
'5' => 0,
),
'validate_argument_type' => 'tid',
'validate_argument_transform' => 0,
'validate_user_restrict_roles' => 0,
'validate_argument_php' => '',
),
));
$handler->override_option('access', array(
'type' => 'none',
));
$handler->override_option('empty', 'No sub-categories for this category.');
$handler->override_option('empty_format', '1');
$handler->override_option('items_per_page', 0);
$handler->override_option('style_plugin', 'grid');
$handler->override_option('style_options', array(
'grouping' => '',
'columns' => '2',
'alignment' => 'horizontal',
));
$handler = $view->new_display('block', 'Block', 'block_1');
$handler->override_option('block_description', '');
$handler->override_option('block_caching', -1);

?>

Have fun!