function shs_term_get_children in Simple hierarchical select 7
Function to get the list of children of a term.
The structure is stored in the database cache as well as in drupal_static(). Cache has the following structure: <code> [$parent] => array(), </code>
Parameters
mixed $identifier: Either a vocabulary ID or an array with the following keys:
- field_name: Name of field which sends the request.
int $parent: ID of parent term.
array $settings: Additional settings (for example "language", etc.,).
bool $reset: If TRUE, rebuild the cache for the given $vid and $parent.
Return value
array List of child terms keyed by term id.
2 calls to shs_term_get_children()
- shs_field_widget_validate in ./
shs.module - Validation handler for widgets of type "taxonomy_shs".
- shs_json_term_get_children in ./
shs.module - JSON callback to get the list of children of a term.
File
- ./
shs.module, line 847 - Provides an additional widget for term fields to create hierarchical selects.
Code
function shs_term_get_children($identifier, $parent = 0, array $settings = array(), $reset = FALSE) {
global $language;
$langcode = $language->language;
if (empty($settings['language']->language)) {
$settings['language'] = $language;
}
else {
$langcode = $settings['language']->language;
}
$vocabularies = array();
$vocabulary_cache_key = NULL;
if (is_numeric($identifier)) {
$vocabulary_cache_key = $identifier;
if (($vocabulary = taxonomy_vocabulary_load($identifier)) === FALSE) {
watchdog('Simple hierarchical select', 'Unknown vocabulary with ID !vid used to get terms.', array(
'!vid' => $identifier,
));
return array();
}
$vocabularies[$vocabulary->machine_name] = $identifier;
}
elseif (is_array($identifier) && !empty($identifier['field_name'])) {
$vocabulary_cache_key = $identifier['field_name'];
}
$terms =& drupal_static(__FUNCTION__, array());
if ($reset || $vocabulary_cache_key && empty($terms[$vocabulary_cache_key][$langcode][$parent])) {
// Initialize list.
$terms[$vocabulary_cache_key][$langcode][$parent] = array();
$cache_key = "shs:{$vocabulary_cache_key}";
// Get cached values.
$cache = cache_get($cache_key);
if ($reset || !$cache || $cache->expire && time() > $cache->expire || empty($cache->data[$langcode][$parent])) {
// Cache is empty or data has become outdated or the parent is not cached.
if ($cache) {
// Cache exists and is not yet expired but $parent is missing.
$terms[$vocabulary_cache_key] = $cache->data;
}
if ($reset) {
$terms[$vocabulary_cache_key][$langcode][$parent] = array();
}
if (!is_numeric($vocabulary_cache_key) && is_array($identifier)) {
// Get list of vocabularies from field configuration.
$field = field_info_field($identifier['field_name']);
if (!empty($field['settings']['handler_settings']['target_bundles'])) {
foreach ($field['settings']['handler_settings']['target_bundles'] as $vocabulary_name) {
if (($vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_name)) !== FALSE) {
$vocabularies[$vocabulary_name] = $vocabulary->vid;
}
}
}
}
foreach ($vocabularies as $vid) {
// Get term children (only first level).
// Only load entities if i18n_taxonomy or entity_translation is
// installed.
$load_entities = module_exists('i18n_taxonomy') || module_exists('entity_translation');
$tree = taxonomy_get_tree($vid, $parent, 1, $load_entities);
foreach ($tree as $term) {
$term_name = $term->name;
if (module_exists('i18n_taxonomy')) {
$term_name = i18n_taxonomy_term_name($term, $langcode);
}
elseif (module_exists('entity_translation')) {
$term_name = entity_label('taxonomy_term', $term);
}
$terms[$vocabulary_cache_key][$langcode][$parent][$term->tid] = $term_name;
}
}
// Set cached data.
cache_set($cache_key, $terms[$vocabulary_cache_key], 'cache', CACHE_PERMANENT);
}
else {
// Use cached data.
$terms[$vocabulary_cache_key] = $cache->data;
}
}
// Allow other module to modify the list of terms.
$alter_options = array(
'vid' => $vocabulary_cache_key,
'parent' => $parent,
'settings' => $settings,
);
drupal_alter('shs_term_get_children', $terms, $alter_options);
return empty($terms[$vocabulary_cache_key][$langcode][$parent]) ? array() : $terms[$vocabulary_cache_key][$langcode][$parent];
}