You are here

function _term_reference_tree_widget_validate 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_widget_validate()

Validates the term reference tree widgets.

This function sets the value of the tree widgets into a form that Drupal can understand, and also checks if the field is required and has been left empty.

Parameters

$element: The element to be validated.

$form_state: The state of the form.

Return value

The validated element.

1 string reference to '_term_reference_tree_widget_validate'
term_reference_tree_field_widget_form in ./term_reference_tree.widget.inc
Implements hook_field_widget_form().

File

./term_reference_tree.widget.inc, line 583

Code

function _term_reference_tree_widget_validate(&$element, &$form_state) {
  $items = _term_reference_tree_flatten($element, $form_state);
  $value = array();
  if ($element['#max_choices'] != 1) {
    foreach ($items as $child) {
      if (!empty($child['#value'])) {
        array_push($value, array(
          $element['#value_key'] => $child['#value'],
        ));

        // If the element is leaves only and select parents is on, then automatically
        // add all the parents of each selected value.
        if ($element['#select_parents'] && $element['#leaves_only']) {
          foreach ($child['#parent_values'] as $parent_tid) {
            if (!in_array(array(
              $element['#value_key'] => $parent_tid,
            ), $value)) {
              array_push($value, array(
                $element['#value_key'] => $parent_tid,
              ));
            }
          }
        }
      }
    }
  }
  else {

    // If it's a tree of radio buttons, they all have the same value,
    // so we can just grab the value of the first one.
    if (count($items) > 0) {
      $child = reset($items);
      if (!empty($child['#value'])) {
        array_push($value, array(
          $element['#value_key'] => $child['#value'],
        ));
      }
    }
  }
  if ($element['#required'] && empty($value)) {

    // The title is already check_plained so it's appropriate to use !.
    form_error($element, t('!name field is required.', array(
      '!name' => $element['#title'],
    )));
  }
  form_set_value($element, $value, $form_state);
  return $element;
}