I'm working on a module that enhances a content type containing an entity reference field (which uses autocomplete). I'd like to populate other parts of my form with some of the data from the node that is referenced when the user fills out the reference field. I'm able to add ajax callbacks to other fields on the form, so I'm wondering if there is a conflict between autocomplete (which uses ajax) and my additional ajax callback.

Comments

Jaypan’s picture

Your explanation was rather general, so my rather general reply is that Drupal autocomplete and #ajax are able to work together. I've added additional autocomplete fields using #ajax in the past, and I've also created additional fields based on a value that was entered in an autocomplete.

ejustice’s picture

Thanks for your response. I just was trying to determine if there was some conflict between using both the autocomplete and the ajax functions for an individual form field.

bjalford’s picture

did you manage to get an ajax call working with the entity ref field? This works for the fied_job_type which is a select term reference, but as soon as I update the code to point to my autocomplete entity reference field it doesn't work.

function test_form_job_node_form_alter(&$form, &$form_state, $form_id) {
  $form['field_address']['und']['0']['value']['#description'] = t("test");

  $form['field_job_type']['#ajax'] ='callback' => 'anyjobportal_simplest_callback',
      'wrapper' => 'replace_textfield_div',
     );

  // This entire form element will be replaced whenever 'changethis' is updated.
  $form['field_address']['#prefix'] = '<div id="replace_textfield_div">';
  $form['field_address']['#suffix'] = '</div>';

  // An AJAX request calls the form builder function for every change.
  // We can change how we build the form based on $form_state.
  if (isset($form_state['values']['field_job_type'])) {
  	unset($form_state['input']['field_address']);
    $form['field_address']['und']['0']['value']['#default_value'] = t("how does this work?");
  }
  dpm($form_state);
  dpm($form);
}

function anyjobportal_simplest_callback($form, $form_state) {
  return $form['field_address'];
}
vegantriathlete’s picture