Is there a way to include not only the immediate parent of a book node but also all of the ancestors, i.e. the path from the first book node (with id=0) to the current node? If not, would it be straightforward to do that using a new module?

Comments

natuk’s picture

I managed to do this in the end by using nid for the book page and converting it to the path with a function in the theme file which looks like this (template as produced by views with "glossary" being my theme name and "alphabetical" my view name):

/**
 * Function to build path to nid.
 */
function mytheme_views_handle_field_alphabetical_node_nid($fields, $field, $data) {
  $info = $fields[$field['fullname']];

  if ($field['handler'] && function_exists($field['handler'])) {
    return $field['handler']($info, $field, $data->$field['queryname'], $data);
  }

  if ($info['handler'] && is_string($info['handler']) && function_exists($info['handler'])) {
    $id = $data->nid;
    while ($id > 0) {
      $parent = findParent($id);
      $path[$parent->nid] = $parent->title;
      $id = $parent->nid;
    }
    $path = array_reverse($path,TRUE);
    foreach ($path as $nid => $value) {
      if (!empty($path[$nid])){
        $result .= ' »<br/>'.l("$value", "node/$nid");      
      }
    }
    return $result;
  }
  
  return check_plain($data->$field['queryname']);

}

//helper function to find the parent of a book node
function findParent($nid = NULL){
  if (!isset($nid)){
    $nid = 0;
  }
	
  $query = 'SELECT nn.nid, nn.title FROM {node} n INNER JOIN {book} b ON n.vid = b.vid INNER JOIN {node} nn ON b.parent = nn.nid WHERE n.status = 1 AND n.nid = %d';
  $result = db_query(db_rewrite_sql($query), $nid);
  while ($child = db_fetch_object($result)){
    $myNode = $child;
  }		
  return $myNode;
}
esmerel’s picture

Status: Active » Closed (fixed)

No activity for 6 months