1.20.x taxonomy.module | taxonomy_vocabulary_load_multiple($machine_names = array()) |
Load multiple taxonomy vocabularies based on certain conditions.
This function should be used whenever you need to load more than one vocabulary from the database. Terms are loaded into memory and will not require database access if loaded again during the same page request.
Parameters
$machine_names: An array of taxonomy vocabulary machine names, or FALSE to load all vocabularies.
Return value
An array of vocabulary objects, indexed by machine name.:
See also
File
- modules/
taxonomy/ taxonomy.module, line 1238 - Enables the organization of content into categories.
Code
function taxonomy_vocabulary_load_multiple($machine_names = array()) {
$static = &backdrop_static(__FUNCTION__, array(
'vocabularies' => array(),
'machine_names' => NULL,
));
// Populate the list of machine names if FALSE is provided.
if ($machine_names === FALSE) {
if (isset($static['machine_names'])) {
$machine_names = $static['machine_names'];
}
else {
$machine_names = array();
$config_names = config_get_names_with_prefix('taxonomy.vocabulary.');
foreach ($config_names as $config_name) {
$machine_names[] = str_replace('taxonomy.vocabulary.', '', $config_name);
}
$static['machine_names'] = $machine_names;
}
}
// Read each machine name out of config.
$new_vocabularies = array();
foreach ($machine_names as $machine_name) {
if (!isset($static['vocabularies'][$machine_name])) {
$config = config('taxonomy.vocabulary.' . $machine_name);
$data = $config->get();
$new_vocabularies[$machine_name] = $data ? new TaxonomyVocabulary($data) : FALSE;
}
}
// Allow modules to load additional data into the new vocabularies.
foreach (module_implements('taxonomy_vocabulary_load') as $module) {
$function = $module . '_taxonomy_vocabulary_load';
$function($new_vocabularies);
}
// Add the new vocabularies to the list of statically cached vocabularies.
$static['vocabularies'] += $new_vocabularies;
// Return only the requested vocabularies.
$return = array();
foreach ($machine_names as $machine_name) {
if (isset($static['vocabularies'][$machine_name])) {
$return[$machine_name] = $static['vocabularies'][$machine_name];
}
else {
$return[$machine_name] = FALSE;
}
}
// Sort the returned vocabularies.
uasort($return, function($a, $b) {
if ($a->weight != $b->weight) {
return $a->weight < $b->weight ? -1 : 1;
}
});
return $return;
}