You are here

function select2_taxonomy_term_reference_validate in Select 2 7

Validate term references.

1 string reference to 'select2_taxonomy_term_reference_validate'
select2_field_widget_form_alter in ./select2.module
Implements hook_field_widget_form_alter().

File

./select2.module, line 903
Main file for Select2 module.

Code

function select2_taxonomy_term_reference_validate(&$element, &$form_state) {
  $values_to_set = array();
  $values = is_array($element['#value']) ? $element['#value'] : array();
  $v_mname = isset($element['#select2']['taxonomy_vocabulary']) ? $element['#select2']['taxonomy_vocabulary'] : '';
  $terms = array();
  if (isset($element['#required']) && $element['#required'] && empty($element['#value'])) {
    $values = array();
  }
  $element_id = isset($element['#id']) ? $element['#id'] : (isset($element['#attributes']['id']) ? $element['#attributes']['id'] : drupal_clean_css_identifier($element['#name']));
  $taxonomy_vocabulary = taxonomy_vocabulary_machine_name_load($v_mname);
  if ($values && $taxonomy_vocabulary) {
    $rebuild_options_vocs_list = select2_taxonomy_volabilaries_to_rebuild_options();
    $terms = taxonomy_term_load_multiple(FALSE, array(
      'vid' => $taxonomy_vocabulary->vid,
    ));
    foreach ($values as $key => $tid) {
      if (!$tid) {
        continue;
      }
      if (is_numeric($tid) && !strpos($tid, select2_get_system_decimal_point())) {

        // Check for valid tid.
        $num_tid = intval($tid);
        if (isset($terms[$num_tid])) {
          $values_to_set[] = $tid;
          continue;
        }
      }
      if ($form_state['rebuild_info']) {
        continue;
      }

      // Trying to add new term. Get max weight of terms in current voc for
      // adding new term at the end of list.
      $max_weight_select = db_select('taxonomy_term_data', 'td')
        ->condition('vid', $taxonomy_vocabulary->vid);
      $max_weight_select
        ->addExpression('MAX(td.weight)');
      $max_weight = $max_weight_select
        ->execute()
        ->fetchField();
      $max_weight = $max_weight ? $max_weight + 1 : 1;
      $new_term = new stdClass();
      $new_term->vid = $taxonomy_vocabulary->vid;
      $new_term->name = $tid;
      $new_term->weight = $max_weight;
      $save_result = taxonomy_term_save($new_term);
      if ($save_result == SAVED_NEW) {
        drupal_set_message(t('New taxonomy term "@term_name" saved in vocabulary "@vocabulary_name". You can edit it on <a href="@edit_term_link">this page</a>.', array(
          '@term_name' => $tid,
          '@vocabulary_name' => $taxonomy_vocabulary->name,
          '@edit_term_link' => base_path() . 'taxonomy/term/' . $new_term->tid . '/edit',
        )));
        $rebuild_options_vocs_list[] = $v_mname;
        $values_to_set[] = $new_term->tid;
        $new_data_obj = new stdClass();
        $new_data_obj->id = $new_term->tid;
        $new_data_obj->text = $new_term->name;
        $settings['data'][0] = $new_data_obj;
        _select2_set_element_settings($element_id, $settings);
      }
      else {
        drupal_set_message(t('Error while saving new term - the term skiped while subiting form.'), 'error');
        unset($values[$key]);
      }
    }
  }
  if (count($values_to_set)) {
    form_set_value($element, $values_to_set, $form_state);
    $element['#value'] = $values_to_set;
    $element['#validated'] = TRUE;
  }
}