Index: modules/taxonomy/taxonomy.module =================================================================== RCS file: /cvs/drupal/drupal/modules/taxonomy/taxonomy.module,v retrieving revision 1.436 diff -u -p -r1.436 taxonomy.module --- modules/taxonomy/taxonomy.module 2 Nov 2008 17:46:47 -0000 1.436 +++ modules/taxonomy/taxonomy.module 3 Nov 2008 13:16:14 -0000 @@ -1050,6 +1050,92 @@ function taxonomy_terms_load($str_tids) } /** + * Load multiple taxonomy terms based on certain conditions. + * + * @param $tids + * An array of taxonomy term IDs. + * @param $conditions + * An array of conditions to add to the query. + * @param $reset + * Whether the reset the internal cache. + * + * @return + * An array of term objects, indexed by tid. + */ +function taxonomy_term_multiple_load($tids = array(), $conditions = array(), $reset = FALSE) { + static $term_cache = array(); + + if ($reset) { + $term_cache = array(); + } + + $terms = array(); + + // Load any available terms from the cache. + if (!empty($tids)) { + foreach ($tids as $key => $tid) { + if (isset($term_cache[$tid])) { + $terms[$tid] = $term_cache[$tid]; + unset($tids[$key]); + } + } + } + + // If any terms loaded from the cache don't match a condition, remove them. + + if (!empty($conditions)) { + foreach ($terms as $tid => $term) { + foreach ($conditions as $key => $value) { + if ($term->$key != $value) { + unset($terms[$tid]); + } + } + } + } + + // Fetch any remaining terms from the database. + $query = db_select('term_data', 't'); + $term_data_fields = drupal_schema_fields_sql('term_data'); + foreach ($term_data_fields as $field) { + $query->addField('t', $field, $field); + } + + // If the $tids array is populated, add those to the query. + if (!empty($tids)) { + $query->condition('t.tid', $tids, 'IN'); + } + + // If the conditions array is populated, add that to the query. + if (!empty($conditions)) { + foreach ($conditions as $field => $value) { + $query->conditions('t.' . $field, $value); + } + } + + $result = $query->execute(); + + foreach ($result as $record) { + $terms[$record->tid] = $record; + } + + // Invoke hook_taxonomy_multiple_load() on the terms array. + foreach (module_implements('taxonomy_term_multiple_load') as $module) { + $function = $module . '_taxonomy_term_multiple_load'; + call_user_func($function, &$terms); + } + + // Invoke hook_taxonomy_term_load() on each individual term. + foreach (module_implements('taxonomy_term_load') as $module) { + $function = $module . '_taxonomy_term_load'; + array_walk(&$terms, $function); + } + + $term_cache += $terms; + + return $terms; +} + +/** * Return the term object matching a term ID. * * @param $tid @@ -1062,12 +1148,7 @@ function taxonomy_term_load($tid, $reset if (!is_numeric($tid)) { return FALSE; } - static $terms = array(); - if (!isset($terms[$tid]) || $reset) { - $terms[$tid] = taxonomy_get_term_data($tid, $reset); - module_invoke_all('taxonomy_term_load', $terms[$tid]); - } - return $terms[$tid]; + return array_shift(taxonomy_term_multiple_load(array($tid), NULL, $reset)); } /**