diff options
author | Dries Buytaert <dries@buytaert.net> | 2010-08-08 02:06:56 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2010-08-08 02:06:56 +0000 |
commit | a9c9ef2d3010ac742e82542e3b0d0864cb9a9d25 (patch) | |
tree | 599899411c398fe6cb7645c60bfa0f2c25df4095 /modules/taxonomy | |
parent | 2aee69c3ced832a5f5a1aac6046859432f170218 (diff) | |
download | brdo-a9c9ef2d3010ac742e82542e3b0d0864cb9a9d25.tar.gz brdo-a9c9ef2d3010ac742e82542e3b0d0864cb9a9d25.tar.bz2 |
- Patch #870528 by Dave Reid: taxonomy fixes.
Diffstat (limited to 'modules/taxonomy')
-rw-r--r-- | modules/taxonomy/taxonomy.module | 129 | ||||
-rw-r--r-- | modules/taxonomy/taxonomy.test | 2 | ||||
-rw-r--r-- | modules/taxonomy/taxonomy.tokens.inc | 3 |
3 files changed, 65 insertions, 69 deletions
diff --git a/modules/taxonomy/taxonomy.module b/modules/taxonomy/taxonomy.module index f279f1c44..9fbc4c76f 100644 --- a/modules/taxonomy/taxonomy.module +++ b/modules/taxonomy/taxonomy.module @@ -581,14 +581,13 @@ function taxonomy_term_delete($tid) { field_attach_delete('taxonomy_term', $term); module_invoke_all('taxonomy_term_delete', $term); + taxonomy_terms_static_reset(); } $tids = $orphans; } cache_clear_all(); - taxonomy_terms_static_reset(); - return SAVED_DELETED; } @@ -679,6 +678,8 @@ function taxonomy_term_is_page($term) { function taxonomy_terms_static_reset() { drupal_static_reset('taxonomy_term_count_nodes'); drupal_static_reset('taxonomy_get_tree'); + drupal_static_reset('taxonomy_get_tree:parents'); + drupal_static_reset('taxonomy_get_tree:terms'); drupal_static_reset('taxonomy_get_parents'); drupal_static_reset('taxonomy_get_parents_all'); drupal_static_reset('taxonomy_get_children'); @@ -707,35 +708,30 @@ function taxonomy_vocabulary_get_names() { } /** - * Find all parents of a given term ID. + * Finds all parents of a given term ID. + * + * @param $tid + * A taxonomy term ID. + * + * @return + * An array of term objects which are the parents of the term $tid. */ -function taxonomy_get_parents($tid, $key = 'tid') { - if ($tid) { - $tids = &drupal_static(__FUNCTION__, array()); - if (isset($tids[$key][$tid])) { - $parents = $tids[$key][$tid]; - } - else { - $query = db_select('taxonomy_term_data', 't'); - $query->join('taxonomy_term_hierarchy', 'h', 'h.parent = t.tid'); - $result = $query - ->addTag('translatable') - ->addTag('term_access') - ->fields('t') - ->condition('h.tid', $tid) - ->orderBy('weight') - ->orderBy('name') - ->execute(); - $parents = array(); - foreach ($result as $parent) { - $parents[$parent->$key] = $parent; - } - } - return $parents; - } - else { - return array(); +function taxonomy_get_parents($tid) { + $parents = &drupal_static(__FUNCTION__, array()); + + if ($tid && !isset($parents[$tid])) { + $query = db_select('taxonomy_term_data', 't'); + $query->join('taxonomy_term_hierarchy', 'h', 'h.parent = t.tid'); + $query->addField('t', 'tid'); + $query->condition('h.tid', $tid); + $query->addTag('term_access'); + $query->orderBy('t.weight'); + $query->orderBy('t.name'); + $tids = $query->execute()->fetchCol(); + $parents[$tid] = taxonomy_term_load_multiple($tids); } + + return isset($parents[$tid]) ? $parents[$tid] : array(); } /** @@ -764,35 +760,35 @@ function taxonomy_get_parents_all($tid) { } /** - * Find all children of a term ID. + * Finds all children of a term ID. + * + * @param $tid + * A taxonomy term ID. + * @param $vid + * An optional vocabulary ID to restrict the child search. + * + * @return + * An array of term objects which are the children of the term $tid. */ -function taxonomy_get_children($tid, $vid = 0, $key = 'tid') { - $tids = &drupal_static(__FUNCTION__, array()); - if (isset($tids[$vid][$tid])) { - $children = $tids[$vid][$tid]; - } - else { +function taxonomy_get_children($tid, $vid = 0) { + $children = &drupal_static(__FUNCTION__, array()); + + if ($tid && !isset($children[$tid])) { $query = db_select('taxonomy_term_data', 't'); $query->join('taxonomy_term_hierarchy', 'h', 'h.tid = t.tid'); - $query - ->addTag('translatable') - ->addTag('term_access') - ->fields('t') - ->condition('parent', $tid) - ->orderBy('weight') - ->orderBy('name'); + $query->addField('t', 'tid'); + $query->condition('h.parent', $tid); if ($vid) { $query->condition('t.vid', $vid); } - $result = $query->execute(); - - $children = array(); - foreach ($result as $term) { - $children[$term->$key] = $term; - } - $tids[$vid][$tid] = $children; + $query->addTag('term_access'); + $query->orderBy('t.weight'); + $query->orderBy('t.name'); + $tids = $query->execute()->fetchCol(); + $children[$tid] = taxonomy_term_load_multiple($tids); } - return $children; + + return isset($children[$tid]) ? $children[$tid] : array(); } /** @@ -815,8 +811,8 @@ function taxonomy_get_children($tid, $vid = 0, $key = 'tid') { */ function taxonomy_get_tree($vid, $parent = 0, $max_depth = NULL, $depth = -1) { $children = &drupal_static(__FUNCTION__, array()); - $parents = &drupal_static(__FUNCTION__ . 'parents', array()); - $terms = &drupal_static(__FUNCTION__ . 'terms', array()); + $parents = &drupal_static(__FUNCTION__ . ':parents', array()); + $terms = &drupal_static(__FUNCTION__ . ':terms', array()); $depth++; @@ -829,19 +825,18 @@ function taxonomy_get_tree($vid, $parent = 0, $max_depth = NULL, $depth = -1) { $query = db_select('taxonomy_term_data', 't'); $query->join('taxonomy_term_hierarchy', 'h', 'h.tid = t.tid'); - $result = $query - ->addTag('translatable') - ->addTag('term_access') - ->fields('t') - ->fields('h', array('parent')) - ->condition('t.vid', $vid) - ->orderBy('weight') - ->orderBy('name') - ->execute(); - foreach ($result as $term) { - $children[$vid][$term->parent][] = $term->tid; - $parents[$vid][$term->tid][] = $term->parent; - $terms[$vid][$term->tid] = $term; + $query->addField('t', 'tid'); + $query->addField('h', 'parent'); + $query->condition('t.vid', $vid); + $query->addTag('term_access'); + $query->orderBy('t.weight'); + $query->orderBy('t.name'); + $tid_parents = $query->execute()->fetchAllKeyed(); + $terms[$vid] = taxonomy_term_load_multiple(array_keys($tid_parents)); + + foreach ($tid_parents as $tid => $parent_tid) { + $children[$vid][$parent_tid][] = $tid; + $parents[$vid][$tid][] = $parent_tid; } } @@ -851,8 +846,6 @@ function taxonomy_get_tree($vid, $parent = 0, $max_depth = NULL, $depth = -1) { foreach ($children[$vid][$parent] as $child) { $term = clone $terms[$vid][$child]; $term->depth = $depth; - // The "parent" attribute is not useful, as it would show one parent only. - unset($term->parent); $term->parents = $parents[$vid][$child]; $tree[] = $term; if (!empty($children[$vid][$child])) { diff --git a/modules/taxonomy/taxonomy.test b/modules/taxonomy/taxonomy.test index a7c922b7f..87cfb4011 100644 --- a/modules/taxonomy/taxonomy.test +++ b/modules/taxonomy/taxonomy.test @@ -1028,6 +1028,8 @@ class TaxonomyTokenReplaceTestCase extends TaxonomyWebTestCase { $tests['[term:url]'] = url('taxonomy/term/' . $term2->tid, array('absolute' => TRUE)); $tests['[term:node-count]'] = 1; $tests['[term:parent:name]'] = check_plain($term1->name); + $tests['[term:parent:url]'] = url('taxonomy/term/' . $term1->tid, array('absolute' => TRUE)); + $tests['[term:parent:parent:name]'] = '[term:parent:parent:name]'; $tests['[term:vocabulary:name]'] = check_plain($this->vocabulary->name); // Test to make sure that we generated something for each token. diff --git a/modules/taxonomy/taxonomy.tokens.inc b/modules/taxonomy/taxonomy.tokens.inc index 21baca7e0..8183fd79b 100644 --- a/modules/taxonomy/taxonomy.tokens.inc +++ b/modules/taxonomy/taxonomy.tokens.inc @@ -111,7 +111,8 @@ function taxonomy_tokens($type, $tokens, array $data = array(), array $options = break; case 'url': - $replacements[$original] = url('taxonomy/term/' . $term->tid, array('absolute' => TRUE)); + $uri = entity_uri('taxonomy_term', $term); + $replacements[$original] = url($uri['path'], array_merge($uri['options'], array('absolute' => TRUE))); break; case 'node-count': |