Hello, I'm still a noob with Drupal, I was wondering how can I get Drupal to auto generate a chosen category list on certain pages? As I'd like to make index pages of the content on my site, is there a way to automate this process instead of manually coding the links individually? I would like the list to be on the content area of the page and not in the left, right or top navigation areas. Any help would be greatly appreciated. Thank you

Comments

jaredeckersley’s picture

Hello seraphangel,

I have finished a module that does this. I just placed my request for a cvs account, but will post the code here until I can upload it proper.

This module is coded against 4.7 Drupal and the Category module. I am using the form_alter hook to include a set of radio options "Enabled" and "Disabled" on the Category sections. When enabled, the navigation tree is appended to that nodes body. This will display all navigational elements, even if those elements have been marked as "hidden" from the main navigation menu via the Category module. The reasoning for this is that we have massive amounts of nodes and need a convenient way for users to get to the content without cluttering the main nav with all the content.

The following features are provided:

  • navigational style listings per node - appended to the nodes body
  • add/delete/modify functions
  • db install script

I hope this helps :)

- Jared Eckersley

File gem_cat_index.module

<?php
/**
  gem_cat_index provides a navigational tree listings per node. 
  This module depends on the Category module.

  Copyright (C) 2007  Jared Eckersley <jared@jaredeckersley.com>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
// $Id: gem_cat_index.module,v 1.1 2007/02/20 20:25:12 cvs Exp $

/**
 * Implementation of hook_help().
 * This is just adding some text to the module admin section.
 */
function gem_cat_index_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('Enables navigation listings in the node body.');
  }
}


/**
 * hook_form_alter call.
 * This injects a checkbox into the category form array.
 * The checkbox is stored in $form['category']['index'].
 */
function gem_cat_index_form_alter($form_id, &$form) {
  $type = $form['type']['#value'] .'_node_form';

  if ($form_id == $type) {
    if (is_array($form['category'])) {
      $form['category']['index'] = array(
        '#type' => 'radios',
        '#title' => t('Navigation Layout'),
        '#description' => t('Should this node have a navigation layout attached to it?'),
        '#default_value' => 0,
        '#options' => array(0 => t('Disabled'), 1 => t('Enabled')),
      );
    }
  }

}

/**
 * hook_nodeapi call
 * 
 */

function gem_cat_index_nodeapi(&$node, $op, $teaser, $page) {
  switch ($op) {
    case 'load':
      $object = db_fetch_object(db_query('SELECT nid FROM {gem_cat_index} WHERE nid = %d', $node->nid));
      return array('gem_cat_index' => $object->nid);
      break;
    case 'insert':
      if ($node->category['index'] == 1) {
        db_query('INSERT INTO {gem_cat_index} (nid) VALUES (%d)', $node->nid);
      }
    break;
    case 'update':
      db_query('DELETE FROM {gem_cat_index} WHERE nid = %d', $node->nid);
      if ($node->category['index'] == 1) {
        db_query('INSERT INTO {gem_cat_index} (nid) VALUES (%d)', $node->nid);
      }
    break;
    case 'delete':
      db_query('DELETE FROM {gem_cat_index} WHERE nid = %d', $node->nid);
    break;
    case 'view':
      if ($node->gem_cat_index) {
        $tree          = _gem_cat_index($node->gem_cat_index);
        $node->body   .= $tree;
        $node->teaser .= $tree;
      }
    break;
  }
}

/**
 * Handle all the calls to helper function.
 * 
 * @param $nid
 *   The parent node id.
 * 
 * @param return
 *   a fully formatted navagational menu.
 */
function _gem_cat_index($nid) {
  global $_menu;

  // The category module maintains a mapping 
  // in category_menu_map table of the global $_menu 
  // array index key that corresponds to the node id.
  $mid  = category_menu_get_mapping($nid);

  // grab all the child elements from $_menu array.
  $menu = recurse_children($mid);

  // manually add the parent array to our menu array
  $menu[$mid] = $_menu['items'][$mid];

  // return HTML formated navigational tree.
  return gem_cat_index_get_tree($mid,$menu);
}

/**
 * Format an array into a linked navigational tree
 * 
 * @param $pid
 *   This is the initial starting point, but through 
 *   recursive calls, all children get passed in as
 *   a parent.
 * 
 * @param $menu
 *   This is the menu array to be formatted.
 * 
 * @return
 *   HTML formatted navigational tree.
 */
function gem_cat_index_get_tree($pid = 1, $menu = NULL) {

  if (isset($menu[$pid]) && $menu[$pid]['children']) {
    foreach ($menu[$pid]['children'] as $mid) {
      if ($mid > 0) {
        $children = isset($menu[$mid]['children']) ? $menu[$mid]['children'] : NULL;
        $menu_tree .= theme('menu_item', $mid, gem_cat_index_get_tree($mid, $menu), count($children) == 0);
      }
    }
  }
  
  if ($menu_tree) {
     $output = '<ul class="tree">'. $menu_tree .'</ul>';
  }

  return $output;
}

/**
 * Build a multidimensional array of all the children
 * of the given parent.
 * 
 * @param $parent
 *   This is the initial starting point, but through 
 *   recursive calls, all children get passed in as
 *   a parent.
 * 
 * @return
 *   An array that matches the format of the global
 *   $_menu['visible'] array.
 */
function recurse_children($parent) {
  global $_menu;

  $children = array();

  if ($_menu['items'][$parent]['children']) {
    foreach ($_menu['items'][$parent]['children'] as $child) {
   	  if ($child > 0) {
            $children[$child] = array('title'=>$_menu['items'][$child]['title'],
                                                  'path'=>$_menu['items'][$child]['path'],
                                                  'children'=>$_menu['items'][$child]['children'],
                                                  'type'=>$_menu['items'][$child]['type'],
                                                  'pid'=>$parent,
                                                  'id'=>$child);
      }
    }
  }

  foreach ($children as $child) {
    $children += recurse_children($child['id']);
  }

  return $children;
}

?>

File gem_cat_index.install

<?php
/**
  gem_cat_index provides a navigational tree listings per node. 
  This module depends on the Category module.

  Copyright (C) 2007  Jared Eckersley <jared@jaredeckersley.com>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
// $Id: gem_cat_index.install,v 1.1 2007/02/20 20:25:11 cvs Exp $

/**
 * Implementation of hook_install()
 *
 * This will automatically install the database tables for the release module for both the MySQL and PostgreSQL databases.
 */

function gem_cat_index_install() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("CREATE TABLE {gem_cat_index} (
                nid int(10) unsigned NOT NULL default '0',
                PRIMARY KEY (nid)
               ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
    break;
    case 'pgsql':
      db_query("CREATE TABLE {gem_cat_index} (
        nid int(10) unsigned NOT NULL default '0',
        PRIMARY KEY (nid),)
      );");
    break;
  }
}

?>