You are here

function _term_reference_tree_build_item in Taxonomy Term Reference Tree Widget 7.2

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

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 688

Code

function _term_reference_tree_build_item(&$element, &$term, &$form_state, &$value, $max_choices, $parent_tids, $parent, $depth) {
  $start_minimized = FALSE;
  if (array_key_exists('#start_minimized', $element)) {
    $start_minimized = $element['#start_minimized'];
  }
  $leaves_only = FALSE;
  if (array_key_exists('#leaves_only', $element)) {
    $leaves_only = $element['#leaves_only'];
  }
  $t = null;
  if (module_exists('locale')) {
    $t = taxonomy_term_load($term->tid);
    $term_name = entity_label('taxonomy_term', $t);
  }
  else {
    $term_name = $term->name;
  }
  $container = array(
    '#type' => 'checkbox_tree_item',
    '#max_choices' => $max_choices,
    '#leaves_only' => $leaves_only,
    '#term_name' => $term_name,
    '#level_start_minimized' => FALSE,
    '#depth' => $depth,
  );
  if (!$element['#leaves_only'] || count($term->children) == 0) {
    $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 ($element['#token_display'] != '' && module_exists('token')) {
      if (!$t) {
        $t = taxonomy_term_load($term->tid);
      }
      $e['#title'] = token_replace($element['#token_display'], array(
        'term' => $t,
      ), array(
        'clear' => TRUE,
      ));
    }
    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'];
    }
  }
  else {
    $e = array(
      '#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;
}