You are here

function term_level_widget_validate in Term Level Field 7

Extracts values form term level field and sets the value for saving.

1 string reference to 'term_level_widget_validate'
term_level_field_widget_form in ./term_level.module
Implements hook_field_widget_form().

File

./term_level.module, line 326
Field type for referencing terms with a level to an entity.

Code

function term_level_widget_validate($element, &$form_state) {
  $instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
  $values = array();
  foreach (element_children($element) as $key) {
    if (isset($element[$key]['terms']) && is_array($element[$key]['terms'])) {
      foreach (element_children($element[$key]['terms']) as $tid) {
        $level = $element[$key]['terms'][$tid]['#value'];
        if (!is_null($level) && $level != "none") {
          $values[] = array(
            'tid' => $tid,
            'level' => $level,
          );
        }
        elseif (!empty($instance['widget']['settings']['validate_empty_terms']) && is_null($level)) {
          $term = $element[$key]['#terms'][$tid];
          form_error($element[$key]['terms'][$tid], t('%term has no level specified. Please select one or remove it from the table.', array(
            '%term' => $term->name,
          )));
        }
      }
    }
  }

  // Use own required validation here, as the default validation does not work
  // here (tag_cloud_term_options always exists).
  if ($element['#required'] && !count($values)) {
    form_error($element, t('!name field is required.', array(
      '!name' => $element['#title'],
    )));
  }
  form_set_value($element, $values, $form_state);
}