Skip to main content
Bounty Ended with 50 reputation awarded by frazras
added 205 characters in body
Source Link
David Thomas
  • 21.9k
  • 2
  • 33
  • 68

Here's another solution using a simple lightweight db_select query on taxonomy_term_data.

It provides two functions, one to get the next term, and one to get the previous.

The idea is to select the next ( or previous ) term in the same vocabulary with a weight greater ( or less than ) than current term, ordered by weight and limited to one result. A static cache is used so query only performed once per page load.

/**
 * Return next term id by weight
 * @param $term taxonomy term object
 * @return int term id or FALSE if none exists
 */
function EXAMPLE_next_term($term) {

  // Use static cache
  $next = &drupal_static(__FUNCTION__ . '_' . $term->tid, FALSE);
  
  if(!$next){
    $next = db_select('taxonomy_term_data', 'td')
      ->fields('td', array('tid'))
      ->condition('weight', $term->weight, '>')
      ->condition('vid', $term->vid)
      ->orderBy('weight')
      ->range(0,1)
      ->execute()
      ->fetchField();
  } 
   
  return $next;
    
}

/**
 * Return previous term id by weight
 * @param $term taxonomy term object
 * @return int term id or FALSE if none exists
 */
function EXAMPLE_previous_term($term) {

  // Use static cache
  $previous = &drupal_static(__FUNCTION__ . '_' . $term->tid, FALSE);

  if(!$previous){
  $previous = db_select('taxonomy_term_data', 'td')
    ->fields('td', array('tid'))
    ->condition('weight', $term->weight, '<')
    ->condition('vid', $term->vid)
    ->orderBy('weight')
    ->range(0,1)
    ->execute()
    ->fetchField();
  }
    
  return $previous;
  
}

Example usage:

if($tid = EXAMPLE_next_term($term)){
  $next_term = taxonomy_term_load($tid); // Load full term
  $link = l($next_term->name, 'taxonomy/term/'.$next_term->tid);
}

Here's another solution using a simple lightweight db_select query on taxonomy_term_data.

It provides two functions, one to get the next term, and one to get the previous.

The idea is to select the next ( or previous ) term in the same vocabulary with a weight greater ( or less than ) than current term, ordered by weight and limited to one result. A static cache is used so query only performed once per page load.

/**
 * Return next term id by weight
 * @return int term id or FALSE if none exists
 */
function EXAMPLE_next_term($term) {

  // Use static cache
  $next = &drupal_static(__FUNCTION__ . '_' . $term->tid, FALSE);
  
  if(!$next){
    $next = db_select('taxonomy_term_data', 'td')
      ->fields('td', array('tid'))
      ->condition('weight', $term->weight, '>')
      ->condition('vid', $term->vid)
      ->orderBy('weight')
      ->range(0,1)
      ->execute()
      ->fetchField();
  } 
   
  return $next;
    
}

/**
 * Return previous term id by weight
 * @return int term id or FALSE if none exists
 */
function EXAMPLE_previous_term($term) {

  // Use static cache
  $previous = &drupal_static(__FUNCTION__ . '_' . $term->tid, FALSE);

  if(!$previous){
  $previous = db_select('taxonomy_term_data', 'td')
    ->fields('td', array('tid'))
    ->condition('weight', $term->weight, '<')
    ->condition('vid', $term->vid)
    ->orderBy('weight')
    ->range(0,1)
    ->execute()
    ->fetchField();
  }
    
  return $previous;
  
}

Here's another solution using a simple lightweight db_select query on taxonomy_term_data.

It provides two functions, one to get the next term, and one to get the previous.

The idea is to select the next ( or previous ) term in the same vocabulary with a weight greater ( or less than ) than current term, ordered by weight and limited to one result. A static cache is used so query only performed once per page load.

/**
 * Return next term id by weight
 * @param $term taxonomy term object
 * @return int term id or FALSE if none exists
 */
function EXAMPLE_next_term($term) {

  // Use static cache
  $next = &drupal_static(__FUNCTION__ . '_' . $term->tid, FALSE);
  
  if(!$next){
    $next = db_select('taxonomy_term_data', 'td')
      ->fields('td', array('tid'))
      ->condition('weight', $term->weight, '>')
      ->condition('vid', $term->vid)
      ->orderBy('weight')
      ->range(0,1)
      ->execute()
      ->fetchField();
  } 
   
  return $next;
    
}

/**
 * Return previous term id by weight
 * @param $term taxonomy term object
 * @return int term id or FALSE if none exists
 */
function EXAMPLE_previous_term($term) {

  // Use static cache
  $previous = &drupal_static(__FUNCTION__ . '_' . $term->tid, FALSE);

  if(!$previous){
  $previous = db_select('taxonomy_term_data', 'td')
    ->fields('td', array('tid'))
    ->condition('weight', $term->weight, '<')
    ->condition('vid', $term->vid)
    ->orderBy('weight')
    ->range(0,1)
    ->execute()
    ->fetchField();
  }
    
  return $previous;
  
}

Example usage:

if($tid = EXAMPLE_next_term($term)){
  $next_term = taxonomy_term_load($tid); // Load full term
  $link = l($next_term->name, 'taxonomy/term/'.$next_term->tid);
}
Source Link
David Thomas
  • 21.9k
  • 2
  • 33
  • 68

Here's another solution using a simple lightweight db_select query on taxonomy_term_data.

It provides two functions, one to get the next term, and one to get the previous.

The idea is to select the next ( or previous ) term in the same vocabulary with a weight greater ( or less than ) than current term, ordered by weight and limited to one result. A static cache is used so query only performed once per page load.

/**
 * Return next term id by weight
 * @return int term id or FALSE if none exists
 */
function EXAMPLE_next_term($term) {

  // Use static cache
  $next = &drupal_static(__FUNCTION__ . '_' . $term->tid, FALSE);
  
  if(!$next){
    $next = db_select('taxonomy_term_data', 'td')
      ->fields('td', array('tid'))
      ->condition('weight', $term->weight, '>')
      ->condition('vid', $term->vid)
      ->orderBy('weight')
      ->range(0,1)
      ->execute()
      ->fetchField();
  } 
   
  return $next;
    
}

/**
 * Return previous term id by weight
 * @return int term id or FALSE if none exists
 */
function EXAMPLE_previous_term($term) {

  // Use static cache
  $previous = &drupal_static(__FUNCTION__ . '_' . $term->tid, FALSE);

  if(!$previous){
  $previous = db_select('taxonomy_term_data', 'td')
    ->fields('td', array('tid'))
    ->condition('weight', $term->weight, '<')
    ->condition('vid', $term->vid)
    ->orderBy('weight')
    ->range(0,1)
    ->execute()
    ->fetchField();
  }
    
  return $previous;
  
}