You are here

function content_taxonomy_autocomplete_widget in Content Taxonomy 5

Same name and namespace in other branches
  1. 6.2 content_taxonomy_autocomplete.module \content_taxonomy_autocomplete_widget()
  2. 6 content_taxonomy_autocomplete.module \content_taxonomy_autocomplete_widget()

Implementation of hook_widget().

File

./content_taxonomy_autocomplete.module, line 63
Defines a widget type for content_taxonomy with autocomplete

Code

function content_taxonomy_autocomplete_widget($op, &$node, $field, &$node_field) {
  $vid = $field['vid'];
  $tid = $field['tid'];
  $depth = !empty($field['depth']) ? $field['depth'] : NULL;
  switch ($op) {
    case 'form':
      $form = array();
      $form[$field['field_name']] = array(
        '#tree' => TRUE,
        '#weight' => $field['widget']['weight'],
      );
      $form[$field['field_name']]['values'] = array(
        '#type' => 'textfield',
        '#title' => $field['widget']['label'],
        '#autocomplete_path' => 'content_taxonomy/autocomplete/' . $field['field_name'] . '/' . $field['multiple'],
        '#default_value' => isset($node_field[$field['tid']]) ? content_taxonomy_autocomplete_merge_tags($node_field[$field['tid']], $vid, $tid) : $field['widget']['default_value']['values'],
        '#description' => t($field['widget']['description']),
        '#required' => $field['required'],
      );
      return $form;
    case 'validate':
      if ($field['multiple'] == 'single' && count(content_taxonomy_autocomplete_split_tags($node_field['values'], $vid)) > 1) {
        form_set_error($field['field_name'] . '][value', t('You can provide only one value'));
      }
      if ($field['widget']['new_terms'] == 'deny') {
        $tids = content_taxonomy_autocomplete_tags_get_tids($node_field['values'], $vid, $field['tid']);
        if (is_array($tids['non_existing_terms'])) {
          form_set_error($field['field_name'], t('New tags are not allowed'));
        }
      }
      break;
    case 'process form values':
      $all_tids = content_taxonomy_autocomplete_tags_get_tids($node_field['values'], $vid, $field['tid']);
      $node_field['tids'] = $all_tids['existing_tids'];
      if ($field['widget']['new_terms'] == 'insert') {
        if (is_array($all_tids['non_existing_terms'])) {
          if ($tid && $depth == 1) {
            $new_tids = content_taxonomy_autocomplete_insert_tags($all_tids['non_existing_terms'], $tid);
          }
          else {
            $new_tids = content_taxonomy_autocomplete_insert_tags($all_tids['non_existing_terms']);
          }
          if (is_array($node_field['tids'])) {
            $node_field['tids'] += $new_tids;
          }
          else {
            $node_field['tids'] = $new_tids;
          }
        }
      }
      if (isset($field['save']) && $field['save'] != 'tag') {
        $keys = array();
        if (!$node_field['tids']) {
          $node_field['tids'] = array();
        }
        foreach ($node_field['tids'] as $key => $tid) {
          if ($tid != 0) {
            $keys[$key] = $tid;
          }
        }
        $node_field = content_transpose_array_rows_cols(array(
          'value' => $keys,
        ));
      }
      unset($node_field['values']);
      break;
  }
}