Code: Flexinode listing with related nodes

EffieRover - February 2, 2006 - 03:31

I couldn't figure out how else to do this, so I hacked it together. Someone more familiar with Drupal can take a dump on the code, but it was quick and it works. All changes in node.module:

Add this in function node_menu

  // -- Loy says: custom display for all flexinode listings of a tid --
  $items[] = array('path' => 'rpg-product-list', 'title' => t('RPG Product List'),
    'callback' => 'rpgproducts_page',
    'access' => user_access('access content'),
    'type' => MENU_CALLBACK);

Add this anywhere in node.module; variables are set at the top. Vocabulary_id is the id number of the vocabulary that you will use for browsing by category.

/**
* Loy says: Generate a listing of flexinode-1's for home or current tid.
* Also generate site listing of child tid's
*/
function rpgproducts_page() {

  $flexinode_id = 'flexinode-1';
  $list_no = 15;
  $vocabulary_id = 10;

  $content_header = '<table><tr valign="top"><td>';
  $content_separator = '</td><td style="width:2em;">&nbsp;</td><td style="width:16em;">';
  $content_footer = '</td></tr></table>';

  // Loy says: set the node content (main stuff)
  if (is_numeric(arg(1))) {
    $tid = arg(1);
    $result = db_query("SELECT name, description FROM {term_data} WHERE tid='$tid'");
    while ($term = db_fetch_object($result)) {
      $name .= $term->name;
      $description .= $term->description;
    }
    if (isset($description)) {
      $description = '<div class="term-description">'.$description.'</div>';
    }
    drupal_set_title($title = $name);
    $current->tid = $tid;
    $breadcrumbs = array(array('path' => $_GET['q']));
    while ($parents = taxonomy_get_parents($current->tid)) {
      $current = array_shift($parents);
      $breadcrumbs[] = array('path' => 'rpg-product-list/'. $current->tid, 'title' => $current->name);
    }
    $breadcrumbs = array_reverse($breadcrumbs);
    menu_set_location($breadcrumbs);
    $sql = "SELECT DISTINCT(n.nid) FROM {node} n INNER JOIN {term_node} tn ON n.nid=tn.nid WHERE tn.tid=$tid AND n.type='$flexinode_id' AND n.status=1 ORDER BY n.created DESC LIMIT 0,$list_no";
  } else {
    $sql = "SELECT n.nid FROM {node} n WHERE n.status=1 AND n.type='$flexinode_id' ORDER BY n.created DESC LIMIT 0,$list_no";
  }
  $node_content = '';
  $result = db_query($sql);
  while ($node = db_fetch_object($result)) {
    $node_content .= node_view(node_load(array('nid' => $node->nid)), 1);
  }

  // Loy says: set the category browse structure: next-level children
  if (is_numeric(arg(1))) {
    $result = db_query("SELECT d.tid, d.name, d.description FROM {term_data} d INNER JOIN {term_hierarchy} h USING (tid) WHERE d.vid = $vocabulary_id AND h.parent=$tid ORDER BY d.weight ASC, d.name ASC");
  } else {
    $result = db_query("SELECT d.tid, d.name, d.description FROM {term_data} d INNER JOIN {term_hierarchy} h USING (tid) WHERE d.vid = $vocabulary_id AND h.parent=0 ORDER BY d.weight ASC, d.name ASC");
  }
  $category_content = '';
  while ($category = db_fetch_object($result)) {
    $category->count = taxonomy_term_count_nodes($category->tid, $flexinode_id);
    if ($category->count > 0) {
      $category_content .= '<li class="subterm"><b>' . l($category->name, 'rpg-product-list/'. $category->tid, array('title' => $category->description)) .'</b> ('. $category->count .')';
    }
  }
  if ($category_content != '') {
    $category_content = '<div class="subterm-container"><h2 class="title">Browse by Category</h2><ul class="subterm">' . $category_content . '</ul></div>';
  }

  // Loy says: pull other stuff now
  $other_content = '';
  if (is_numeric(arg(1))) {
    $sql = "SELECT DISTINCT(n.nid), n.title, n.teaser, n.type FROM {node} n INNER JOIN {term_node} tn ON n.nid=tn.nid WHERE tn.tid=$tid AND n.type<>'$flexinode_id' AND n.type<>'forum' AND n.status=1 ORDER BY n.created DESC LIMIT 0,$list_no";
  } else {
    $sql = "SELECT n.nid, n.title, n.teaser, n.type FROM {node} n WHERE n.status=1 AND n.type<>'$flexinode_id' AND n.type<>'forum' ORDER BY n.created DESC LIMIT 0,$list_no";
  }
  $result = db_query($sql);
  while ($node = db_fetch_object($result)) {
    // $other_content .= node_view(node_load(array('nid' => $node->nid)), 1);
    $other_content .= '<li class="subterm"><b>' . l($node->title, 'node/'. $node->nid, array('title' => $node->teaser)) . '</b> (' . ucfirst($node->type) . ')';
  }
  if ($other_content != '') {
    $other_content = '<div class="subterm-container"><h2 class="title">Related Articles</h2><ul class="subterm">' . $other_content . '</ul></div>';
  }

  // Loy says: if node_content is empty, move the browsing categories over
  if ($node_content == '') {
    $node_content = $category_content;
    $category_content = '';
  }
  $output .= $content_header . $node_content . $content_separator . $description . $category_content . $other_content . $content_footer;

  print theme('page', $output, $list_no);
}

It creates a list of the last X flexinodes as the main content; if main content is empty in this $tid, then place the child categories there instead. As a sidebar, it creates a description of the main $tid, a list of child categories (unless promoted) and a list of related nodes of type != the flexinode type and != forum.

Can be seen at http://testing.rpgcomplete.com

There is no reason to add

merlinofchaos - February 2, 2006 - 04:49

There is no reason to add this in node.module -- put this in something like rpg.module (i.e, something completely yours) instead, and save yourself upgrade headaches. =)

-- Merlin

[Point the finger: Assign Blame!]
[Read my writing: ehalseymiles.com]

The odd thing is that every

EffieRover - February 2, 2006 - 14:28

The odd thing is that every time I try to add a new module file, I keep hitting memory ceilings on some of the admin pages. Of course, I have just installed the patch for the path alias problem, so perhaps that's moot.

If I were to create a separate module file, what function would I put the menu declaration in? Can I just make one up?

The menu declaration would

merlinofchaos - February 2, 2006 - 18:05

The menu declaration would go in hook_menu (i.e,

<?php
function YOURMODULENAME_menu($may_cache) {
 
$items = array();
  if (
$may_cache) {
   
$items[] = ...
  }
  return
$items;
}
?>

You can usually solve the memory limitation by increasing the PHP memory limit. There are quite a few posts about that topic, so do a google search on Drupal PHP memory limit and you should find it.

-- Merlin

[Point the finger: Assign Blame!]
[Read my writing: ehalseymiles.com]

 
 

Drupal is a registered trademark of Dries Buytaert.