Index: taxonomy_breadcrumb.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/taxonomy_breadcrumb/taxonomy_breadcrumb.module,v retrieving revision 1.7.2.2 diff -u -r1.7.2.2 taxonomy_breadcrumb.module --- taxonomy_breadcrumb.module 24 Apr 2009 02:51:41 -0000 1.7.2.2 +++ taxonomy_breadcrumb.module 27 Oct 2009 20:32:38 -0000 @@ -74,13 +74,16 @@ // if the node type IS NOT IN the node types list and the list IS NOT inclusive (e.g. exclusive) // THEN modify the breadcrumb trail. if ($in_list == variable_get('taxonomy_breadcrumb_include_nodes', FALSE) ) { + // Build the breadcrumb from the hierarchy of terms applying to the node + // from the "lightest" vocabulary based on weight. + $hierarchy = _taxonomy_breadcrumb_node_term_hierarchy($node->nid); + $breadcrumb = _taxonomy_breadcrumb_breadcrumb_generate($hierarchy); - // Extract lightest term from lightest vocabulary assosciated with node. - $term = _taxonomy_breadcrumb_node_get_lightest_term($node->nid); - $breadcrumb = _taxonomy_breadcrumb_generate_breadcrumb($term->tid); + // Optionally add the node title. if (variable_get('taxonomy_breadcrumb_include_node_title', FALSE)) { $breadcrumb[] = $node->title; } + drupal_set_breadcrumb($breadcrumb); } } Index: taxonomy_breadcrumb.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/taxonomy_breadcrumb/taxonomy_breadcrumb.inc,v retrieving revision 1.1.2.3 diff -u -r1.1.2.3 taxonomy_breadcrumb.inc --- taxonomy_breadcrumb.inc 31 May 2009 19:37:18 -0000 1.1.2.3 +++ taxonomy_breadcrumb.inc 27 Oct 2009 20:32:37 -0000 @@ -45,23 +45,6 @@ } /** - * Return lightest term for given node ($nid). - * Similar to taxonomy_node_get_terms, but only return the lightest term in the - * lightest vocab for the node. - */ -function _taxonomy_breadcrumb_node_get_lightest_term($nid) { - - // We only want the first row of the result. This is the lightest term of the - // lightest vocab. - $result = db_query(db_rewrite_sql('SELECT t.* FROM {term_node} r INNER JOIN {term_data} t ON r.tid = t.tid INNER JOIN {term_hierarchy} h ON t.tid = h.tid INNER JOIN {vocabulary} v ON t.vid = v.vid WHERE r.nid = %d ORDER BY v.weight, h.parent DESC, t.weight, t.name', 't', 'tid'), $nid); - - // Extract the first row of query. - $term = db_fetch_object($result); - - return $term; -} - -/** * Return the administrator defined vocabulary path for a given vocabulary * ($vid). If a path doesn't exist, NULL is returned. */ @@ -131,6 +114,111 @@ } /** + * Return the hierarchy of terms applying to the node. + * + * @param integer $nid + * The node id of the node to build the hierarchy for. + * @return array + * Array, in hierarchy order, of the terms applying to the node. + */ +function _taxonomy_breadcrumb_node_term_hierarchy($nid) { + $terms = array(); + $vid = 0; + // Retrieve hierarchy information for all vocabularies and terms applying to + // the node. + $sql = 'SELECT parent, t.tid, t.* + FROM {term_node} r + INNER JOIN {term_data} t ON r.tid = t.tid + INNER JOIN {term_hierarchy} h ON t.tid = h.tid + INNER JOIN {vocabulary} v ON t.vid = v.vid + WHERE r.nid = %d + ORDER BY v.weight, parent'; + $result = db_query(db_rewrite_sql($sql, 't', 'tid'), $nid); + while ($term = db_fetch_object($result)) { + $terms[$term->parent] = $term; + // Save the id of the "lightest" vocabulary. + if ($vid == 0) { + $vid = $term->vid; + } + // Include only terms from the "lightest" vocabulary based on weight. + if ($term->vid != $vid) { + break; + } + } + + // Sort the terms in hierarchy order. + return _taxonomy_breadcrumb_hierarchy_order($terms); +} + +/** + * Return terms in hierarchy order. + * + * @param array $terms + * Array of terms to arrange in hierarchy order. + * @return array + * Array of terms in hierarchy order. + */ +function _taxonomy_breadcrumb_hierarchy_order($terms) { + // Sort the terms in hierarchy order. + $parent_id = 0; + $hierarchy = array(); + for ($count = 0; $count < count($terms); $count++) { + // If this array element does not exist, then the taxonomy is "broken." + $hierarchy[] = $terms[$parent_id]; + $parent_id = $terms[$parent_id]->tid; + } + + return $hierarchy; +} + +/** + * Return breadcrumb based on the hierarchy of terms applying to the node. + * + * @param array $terms + * Array keyed by parent id of the terms applying to the node. + * @param boolean $is_term_page + * Indicates whether the current page is a taxonomy/term page. + * @return array + * Array of breadcrumb items. + */ +function _taxonomy_breadcrumb_breadcrumb_generate($terms, $is_term_page = FALSE) { + // HOME breadcrumb generation. + $home_text = variable_get('taxonomy_breadcrumb_home', ''); + if ($home_text != '') { + $breadcrumb[] = l(t($home_text), NULL); + } + // VOCABULARY breadcrumb generation. + $term = $terms[0]; + $vocabulary_path = _taxonomy_breadcrumb_get_vocabulary_path($term->vid); + if ($vocabulary_path != NULL) { + $vocabulary = taxonomy_vocabulary_load($term->vid); + $breadcrumb[] = l(_taxonomy_breadcrumb_tt("taxonomy:vocabulary:$term->tid:name", $vocabulary->name), $vocabulary_path); + } + + // TERM breadcrumb generation. + foreach ($terms as $term) { + $term_path = _taxonomy_breadcrumb_get_term_path($term->tid); + if ($term_path == NULL) { + $term_path = taxonomy_term_path($term); + } + // Do not create links to own self if we are on a taxonomy/term page. + if ($is_term_page && $term->tid == $tid) { + $breadcrumb[] = _taxonomy_breadcrumb_tt("taxonomy:term:$term->tid:name", $term->name); + } + else { + $breadcrumb[] = l(_taxonomy_breadcrumb_tt("taxonomy:term:$term->tid:name", $term->name), $term_path); + } + } + + // Remove current TERM from end of breadcrumb trail + if (!variable_get('taxonomy_breadcrumb_show_current_term', TRUE) && !is_null($breadcrumb)) { + array_pop($breadcrumb); + } + + return $breadcrumb; +} + +/** * Helper function for when i18ntaxonomy module is not installed. */