We don't fully exploit our taxonomy REST api because there is no function that lets you build links with all the bells and whistles. this function lets you enter a term, an array of terms, a tid or an array of tids, the "AND" or "OR" operator, depth and feed, and you get a link to the appropriate taxonomy listing page.

/**
 * Taxonomy REST API: Build a path to a taxonomy listing page
 *
 * @param $terms
 *   a single numeric term id, or a single $term object
 *   or an array of numeric term ids or $term objects
 * @param $op
 *   a string "AND" or "OR"
 * @param $depth
 *   depth of the taxonomy tree that is to be returned. 0 equals the entire tree
 * @param $feed
 *   TRUE for RSS feed, FALSE for page
 */
function taxonomy_get_path($terms, $op = 'AND', $depth = 0, $feed = FALSE) {
  // prepare $tids associative array $tid => $term accounting for the possibility
  // that $terms is either a tid, a $term, an array of tids, or an array of $term objects
  if (is_array($terms) && count($array) > 0) {
    foreach ($terms as $term) {
      if (is_numeric($term)) {
        $term = taxonomy_get_term($term);
      }
      if (is_object($term)) {
        $tids[$term->tid] = $term;
      }
    }
  }
  else {
    if (is_numeric($terms)) {
      $tids[$terms] = taxonomy_get_term($terms);
    }
    elseif (is_object($terms)) {
      $tids[$terms->tid] = $terms;
    }
  }

  // build the $path
  $path = 'taxonomy/term';
  $op = strtolower($op);
  switch ($op) {
    case 'and':
      $path .= '/'. implode(',', array_keys($tids));
      break;

    case 'or':
      $path .= '/'. implode('+', array_keys($tids));
      break;
  }
  
  if ($depth > 0) {
    $path .= "/$depth";
  }

  if ($feed) {
    if ($depth > 0) {
      $path .= '/feed';
    }
    else {
      $path .= '0/feed';
    }
  }
  return $path;
}

Comments

robertdouglass’s picture

This has also been submitted as a proposed addition to core:
http://drupal.org/node/64664

Bèr Kessels’s picture

Status: Needs review » Fixed

Committed in a new helper module file.

Anonymous’s picture

Status: Fixed » Closed (fixed)