You are here

function _term_reference_tree_build_item in Taxonomy Term Reference Tree Widget 7

Same name and namespace in other branches
  1. 8 term_reference_tree.module \_term_reference_tree_build_item()
  2. 7.2 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.

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_item element, which contains a checkbox and possibly a checkbox_tree_level element as well.

1 call to _term_reference_tree_build_item()
_term_reference_tree_build_level in ./term_reference_tree.widget.inc
Builds a level in the term reference tree widget.

File

./term_reference_tree.widget.inc, line 689

Code

function _term_reference_tree_build_item($element, $term, $form_state, $value, $max_choices, $parent_tids, $parent, $depth) {
  $term_name = $term->name;
  if (!empty($element['#token_display']) && module_exists('token') && !empty($term->term_reference_tree_token)) {
    $term_name = check_plain($term->term_reference_tree_token);
  }
  elseif (!empty($term->term_reference_tree_label)) {
    $term_name = check_plain($term->term_reference_tree_label);
  }
  $container = array(
    '#type' => 'checkbox_tree_item',
    '#max_choices' => $max_choices,
    '#leaves_only' => isset($element['#leaves_only']) ? $element['#leaves_only'] : FALSE,
    '#term_name' => $term_name,
    '#level_start_minimized' => FALSE,
    '#depth' => $depth,
    '#has_children' => FALSE,
  );
  if (!$element['#leaves_only'] || empty($term->children)) {
    $e = array(
      '#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 ($e['#type'] == 'radio') {
      $parents_for_id = array_merge($element['#parents'], array(
        $term->tid,
      ));
      $e['#id'] = drupal_html_id('edit-' . implode('-', $parents_for_id));
      $e['#parents'] = $element['#parents'];
    }
    $context = array(
      'element' => $element,
      'term' => $term,
      'form_state' => $form_state,
    );
    drupal_alter('term_reference_tree_element', $e, $context);
  }
  else {
    $e = array(
      '#type' => 'checkbox_tree_label',
      '#value' => $term_name,
    );
  }
  $container[$term->tid] = $e;
  $max_depth_reached = !empty($element['#max_depth']) && $depth >= $element['#max_depth'];
  if ($term->has_children && !$max_depth_reached) {
    $container['#has_children'] = TRUE;
    $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;
}