function _term_reference_tree_build_item in Taxonomy Term Reference Tree Widget 8
Same name and namespace in other branches
- 7.2 term_reference_tree.widget.inc \_term_reference_tree_build_item()
- 7 term_reference_tree.widget.inc \_term_reference_tree_build_item()
Builds a single item in the term reference tree widget.
This function returns an element with a checkbox for a single taxonomy term. If that term has children, it appends checkbox_tree_level element that contains the children. It is meant to be called recursively when the widget is built.
1 call to _term_reference_tree_build_item()
- _term_reference_tree_build_level in ./
term_reference_tree.module - Builds a level in the term reference tree widget.
File
- ./
term_reference_tree.module, line 334
Code
function _term_reference_tree_build_item($element, $term, $form_state, $value, $max_choices, $parent_tids, $parent, $depth) {
$leaves_only = FALSE;
$langcode = \Drupal::languageManager()
->getCurrentLanguage()
->getId();
$t = NULL;
if (\Drupal::moduleHandler()
->moduleExists('locale') && !empty($term->tid)) {
$t = \Drupal::entityTypeManager()
->getStorage('taxonomy_term')
->load($term->tid);
if ($t && $t
->hasTranslation($langcode)) {
$term_name = $t
->getTranslation($langcode)
->label();
}
}
if (empty($term_name)) {
$term_name = $term->name;
}
$container = [
'#type' => 'checkbox_tree_item',
'#max_choices' => $max_choices,
'#leaves_only' => $leaves_only,
'#term_name' => $term_name,
'#level_start_minimized' => FALSE,
'#select_parents' => $element['#select_parents'],
'#depth' => $depth,
];
if (!$element['#leaves_only'] || count($term->children) == 0) {
$e = [
'#type' => $max_choices == 1 ? 'radio' : 'checkbox',
'#title' => $term_name,
'#on_value' => $term->tid,
'#off_value' => 0,
'#return_value' => $term->tid,
'#parent_values' => $parent_tids,
'#default_value' => isset($value[$term->tid]) ? $term->tid : NULL,
'#attributes' => isset($element['#attributes']) ? $element['#attributes'] : NULL,
'#ajax' => isset($element['#ajax']) ? $element['#ajax'] : NULL,
];
if (is_array($e)) {
if ($e['#type'] == 'radio') {
$parents_for_id = array_merge($element['#parents'], [
$term->tid,
]);
$e['#id'] = Html::getId('edit-' . implode('-', $parents_for_id));
$e['#parents'] = array_merge($element['#parents'], [
'wiget',
]);
}
}
}
else {
$e = [
'#type' => 'checkbox_tree_label',
'#value' => $term_name,
];
}
$container[$term->tid] = $e;
if (($depth + 1 <= $element['#max_depth'] || !$element['#max_depth']) && property_exists($term, 'children') && count($term->children) > 0) {
$parents = $parent_tids;
$parents[] = $term->tid;
$container[$term->tid . '-children'] = _term_reference_tree_build_level($element, $term, $form_state, $value, $max_choices, $parents, $depth + 1);
$container['#level_start_minimized'] = $container[$term->tid . '-children']['#level_start_minimized'];
}
return $container;
}