0

I am creating a widget under taxonomy_term_reference field and trying to use the same autocomplete used by taxonomy module, autocomplete is working fine but data is not getting saved.

    /**
     * Implements hook_field_widget_info().
     */
    function test_field_widget_info() {
      return array(
        'test_widget' => array(
          'label' => t('test Engine'),
          'field types' => array('taxonomy_term_reference'),
          'settings' => array(
            'size' => 60,
            'autocomplete_path' => 'taxonomy/autocomplete',
          ),
          'behaviors' => array(
            'multiple values' => FIELD_BEHAVIOR_CUSTOM,
          ),
        ),
      );
    }

    /**
     * Implements hook_field_widget_form().
     */
    function test_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
      if ($instance['widget']['type'] == 'test_widget') {
        $form_field = array();
        $tags = array();
        foreach ($items as $item) {
          $tags[$item['tid']] = isset($item['taxonomy_term']) ? $item['taxonomy_term'] : taxonomy_term_load($item['tid']);
        }
        $form_field['autocomplete'] = array(
          '#type' => 'textfield',
          '#title' => 'Tags',
          '#default_value' => taxonomy_implode_tags($tags),
          '#autocomplete_path' => 'taxonomy/autocomplete' . '/' . $field['field_name'], // Trying to use the same autocomplete path from taxonomy module.
          '#size' => $instance['widget']['settings']['size'],
          '#maxlength' => 1024,
          '#element_validate' => array('test_validate'),
          '#attributes' => array(
            'class' => array($element['#field_name']),
          ),
        );

        $form_field['search'] = array(
          '#type' => 'button',
          '#value' => t('Search Tags'),
          '#ajax' => array(
            'callback' => '_test_callback',
          ),
          '#attributes' => array(
            'reference_class' => $element['#field_name'],
          ),
          '#name' => $element['#field_name'],
        );
        $element += $form_field;
        return array('tid' => $element);
      }
    }
function test_validate($element, &$form_state, $form) {
  $value = array();
  if ($tags = $element['#value']) {
    // Collect candidate vocabularies.
    $field = field_widget_field($element, $form_state);

    $vocabularies = array();
    foreach ($field['settings']['allowed_values'] as $tree) {
      if ($vocabulary = taxonomy_vocabulary_machine_name_load($tree['vocabulary'])) {
        $vocabularies[$vocabulary->vid] = $vocabulary;
      }
    }
  }
  $typed_terms = drupal_explode_tags($tags);
  foreach ($typed_terms as $typed_term) {
    // See if the term exists in the chosen vocabulary and return the tid;
    // otherwise, create a new 'autocreate' term for insert/update.
    if ($possibilities = taxonomy_term_load_multiple(array(), array('name' => trim($typed_term), 'vid' => array_keys($vocabularies)))) {
      $term = array_pop($possibilities);
    }
    else {
      $vocabulary = reset($vocabularies);
      $term = array(
        'tid' => 'autocreate',
        'vid' => $vocabulary->vid,
        'name' => $typed_term,
        'vocabulary_machine_name' => $vocabulary->machine_name,
      );
    }
    $value[] = (array)$term;
  }
  form_set_value($element, $value, $form_state);
}
1
  • have you checked what data is sent to the server? Oct 19, 2016 at 11:44

1 Answer 1

0

Try adding the following line below form_set_value().

$element['#value'] = 'new_value';

P.S. Borrowed from Alter taxonomy autocomplete value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.