Hey,

My goal is to display only 5 results in a taxonomy list page.

I think this function dictates it:

from taxonomy.module

/**
 * Accepts the result of a pager_query() call, such as that performed by
 * taxonomy_select_nodes(), and formats each node along with a pager.
 */
function taxonomy_render_nodes($result) {
  $output = '';
  $has_rows = FALSE;
  while ($node = db_fetch_object($result)) {
    $output .= node_view(node_load($node->nid), 1);
    $has_rows = TRUE;
  }
  if ($has_rows) {
    $output .= theme('pager', NULL, variable_get('default_nodes_main', 10), 0);
  }
  else {
    $output .= '<p>'. t('There are currently no posts in this category.') .'</p>';
  }
  return $output;
}

Now I am tring to create a hook for it and run it in one of my own modules:

/**
 * Accepts the result of a pager_query() call, such as that performed by
 * taxonomy_select_nodes(), and formats each node along with a pager.
 */
function best_render_nodes($result) {
  $output = '';
  $has_rows = FALSE;
  if(!(arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2)))){
    $limit = 5;
  }
  $count = 0;
  while ($node = db_fetch_object($result)) {
    $output .= node_view(node_load($node->nid), 1);
    $has_rows = TRUE;
    $count++;
    if($limit && $count == $limit){
      break;
    }
  }
  if ($has_rows) {
    $output .= theme('pager', NULL, variable_get('default_nodes_main', 10), 0);
  }
  else {
    $output .= '<p>'. t('There are currently no posts in this category.') .'</p>';
  }
  return $output;
}

But drupal doesn't go through there. I tried clearing the cache. I tried putting it in template.php and renamed the function to function phptemplate_render_nodes($result)

I have used hooks before to customize core functions without touching them. But it is not working here.

(Also if there is some super easy solution, like changing a setting in the admin pages, then whoops! let me know if there is haha).

I am pretty curious why the hook here is not working though, I feel I am missing some fundamental understandings here.

Appreciate it,
Eric

Comments

EliseVanLooij’s picture

The hook is not working, because _render_nodes is not a hook, i.e. it is never called by module_invoke_all in the taxonomy module. Apart from that, _render_nodes is not the one you want to be overriding: as the documentation you copied states, it merely accepts the results from a pager_query() call. So what you could do is write your own pager_query() and pipe the result to taxonomy_render_nodes.

function best_show_five_nodes(){
  //define $query here
  $output = '<h2>Five nodes</h2>';
  $result = pager_query($query, 5);
  $output .= module_invoke('taxonomy', 'render_nodes', $result);
  return $output;
}
esend7881’s picture

Okay I see that.

But where in drupal's core is it really calling for 10 listed nodes to display on the taxonomy page? (I.e., it is calling node.tpl.php 10 times).

I would like to just reduce that number to 5 times---and keep track of which iteration we are on as well.

Once I know that, then I'll know how to make drupal trigger my own function best_show_five_nodes (I hope).

-Eric

EDIT:

Oh, wait.. is it this function?

function pager_query($query, $limit = 10, $element = 0, $count_query = NULL) {
  global $pager_page_array, $pager_total, $pager_total_items;
  $page = isset($_GET['page']) ? $_GET['page'] : '';

  // Substitute in query arguments.
  $args = func_get_args();
  $args = array_slice($args, 4);
  // Alternative syntax for '...'
  if (isset($args[0]) && is_array($args[0])) {
    $args = $args[0];
  }

  // Construct a count query if none was given.
  if (!isset($count_query)) {
    $count_query = preg_replace(array('/SELECT.*?FROM /As', '/ORDER BY .*/'), array('SELECT COUNT(*) FROM ', ''), $query);
  }

  // Convert comma-separated $page to an array, used by other functions.
  $pager_page_array = explode(',', $page);

  // We calculate the total of pages as ceil(items / limit).
  $pager_total_items[$element] = db_result(db_query($count_query, $args));
  $pager_total[$element] = ceil($pager_total_items[$element] / $limit);
  $pager_page_array[$element] = max(0, min((int)$pager_page_array[$element], ((int)$pager_total[$element]) - 1));
  return db_query_range($query, $args, $pager_page_array[$element] * $limit, $limit);
}

How do I make sure the taxonomy pages call this with a limit of 5? (at least I assume that is the limit holding the display to 10 nodes..)

esend7881’s picture

Bump...

nancydru’s picture