You are here

function _term_reference_tree_build_level in Taxonomy Term Reference Tree Widget 7

Same name and namespace in other branches
  1. 8 term_reference_tree.module \_term_reference_tree_build_level()
  2. 7.2 term_reference_tree.widget.inc \_term_reference_tree_build_level()

Builds a level in the term reference tree widget.

This function returns an element that has a number of checkbox_tree_item elements as children. It is meant to be called recursively when the widget is built.

Parameters

$element: The main checkbox_tree element.

$term: A taxonomy term object. $term->children should be an array of the term objects that are that term's children.

$form_state: The form state.

$value: The value of the element.

$max_choices: The maximum number of allowed selections.

Return value

A completed checkbox_tree_level element.

2 calls to _term_reference_tree_build_level()
term_reference_tree_process_checkbox_tree in ./term_reference_tree.widget.inc
Process the checkbox_tree widget.
_term_reference_tree_build_item in ./term_reference_tree.widget.inc
Builds a single item in the term reference tree widget.

File

./term_reference_tree.widget.inc, line 776

Code

function _term_reference_tree_build_level($element, $term, $form_state, $value, $max_choices, $parent_tids, $depth) {
  $must_expand = !$element['#start_minimized'] && !$element['#use_ajax'] || isset($term->tid) && in_array($term->tid, $element['#expanded']);
  $container = array(
    '#max_choices' => $max_choices,
    '#leaves_only' => isset($element['#leaves_only']) ? $element['#leaves_only'] : FALSE,
    '#start_minimized' => isset($element['#start_minimized']) ? $element['#start_minimized'] : FALSE,
    '#level_start_minimized' => $depth > 1 && !$must_expand,
    '#depth' => $depth,
  );

  // Adds checkbox_tree_level when children exists or ajax wrapper when children
  // are not loaded (has_children = TRUE but children is empty).
  if (!empty($term->children)) {
    $container['#type'] = 'checkbox_tree_level';
    foreach ($term->children as $child) {
      $container[$child->tid] = _term_reference_tree_build_item($element, $child, $form_state, $value, $max_choices, $parent_tids, $container, $depth);
    }
  }
  else {
    $id = drupal_html_id('term-reference-tree-widget-level-ajax-' . $term->tid);
    $container['#type'] = 'container';
    $container['#attributes']['id'] = $id;
    $container['#attributes']['class'][] = 'term-reference-tree-widget-level-ajax';
    $container['load_children_' . $term->tid] = array(
      '#type' => 'button',
      '#attributes' => array(
        'class' => array(
          'term-reference-tree-widget-level-ajax-button',
        ),
      ),
      '#value' => 'load children ' . $term->tid,
      '#limit_validation_errors' => array(),
      '#term' => $term,
      '#ajax' => array(
        'wrapper' => $id,
        'callback' => 'term_reference_tree_children_callback',
      ),
    );
  }
  return $container;
}