You are here

function term_reference_tree_process_checkbox_tree in Taxonomy Term Reference Tree Widget 7

Same name and namespace in other branches
  1. 7.2 term_reference_tree.widget.inc \term_reference_tree_process_checkbox_tree()

Process the checkbox_tree widget.

This function processes the checkbox_tree widget.

Parameters

$element: The element to be drawn.$element['#field_name']

$form_state: The form state.

Return value

The processed element.

1 string reference to 'term_reference_tree_process_checkbox_tree'
term_reference_tree_element_info in ./term_reference_tree.module
Implements hook_element_info().

File

./term_reference_tree.widget.inc, line 294

Code

function term_reference_tree_process_checkbox_tree($element, $form_state) {
  if (is_array($form_state)) {
    $allowed = !empty($element['#filter_view']) ? _term_reference_tree_get_allowed_values($element['#filter_view']) : '';
    $default = !empty($element['#default_value']) && is_array($element['#default_value']) ? $element['#default_value'] : array();
    $opened = !empty($form_state['children']) ? $form_state['children'] : array();
    $max_depth = !empty($element['#max_depth']) ? $element['#max_depth'] : NULL;

    // Build the terms hierarchy.
    $default_parents = _term_reference_tree_taxonomy_get_parents_all($element['#vocabulary']->vid, $default);
    $element['#expanded'] = $opened + $default_parents;
    $element['#options_tree'] = _term_reference_tree_get_term_hierarchy($element['#parent_tid'], $element['#vocabulary']->vid, $allowed, $element['#expanded'], $element['#use_ajax'], $max_depth);

    // Flatten the hierarchy.
    $terms_flat = _term_reference_tree_hierarchy_flatten($element['#options_tree']);

    // Give other modules a chance to alter the terms.
    drupal_alter('term_reference_tree_process_terms', $terms_flat, $element);

    // Resolve labels.
    _term_reference_tree_taxonomy_resolve_labels($terms_flat, $element['#token_display']);
    $max_choices = !empty($element['#max_choices']) ? $element['#max_choices'] : 1;
    if ($max_choices != '-1') {
      drupal_add_js(array(
        'term_reference_tree' => array(
          'trees' => array(
            $element['#id'] => array(
              'max_choices' => $element['#max_choices'],
            ),
          ),
        ),
      ), 'setting');
    }
    if ($max_choices == 1 && !$element['#required']) {
      array_unshift($element['#options_tree'], (object) array(
        'tid' => '',
        'name' => t('N/A'),
        'depth' => 0,
        'vocabulary_machine_name' => $element['#vocabulary']->machine_name,
        'has_children' => FALSE,
      ));
    }
    if ($max_choices != 1) {
      $element['#tree'] = TRUE;
    }
    if (!empty($element['#select_parents'])) {
      $element['#attributes']['class'][] = 'select-parents';
    }
    $tree = new stdClass();
    $tree->children = $element['#options_tree'];
    $tree->has_children = TRUE;
    $element[] = _term_reference_tree_build_level($element, $tree, $form_state, $default, $max_choices, array(), 1);

    // Add a track list element?
    if (!empty($element['#track_list'])) {
      $element[] = array(
        '#type' => 'checkbox_tree_track_list',
        '#max_choices' => $max_choices,
      );
    }
  }
  return $element;
}