I tried adding the #ajax property to an autocomplete entity reference field on a Content Type creation form but I don't seem to be getting the callback when the field value is changed. I tried adding #ajax to some other simple fields on the same page and the callback worked, so it's specific to the entity reference field. (The reason I'm doing this is to catch and handle the situations where there are zero or > 1 matching references and put up a little form to allow the user to add the missing entity or choose between the multiple matching ones).


Version:7.x-1.0-rc1» 7.x-1.x-dev

Version:7.x-1.x-dev» 7.x-1.0-rc1

Sorry, I was right the first time: -rc1. I haven't tried -dev.

> I haven't tried -dev

You should try -dev then

I'm seeing the same thing using dev. Here's the code to add the ajax which works fine when pointing to another field but not the entity ref one.

['field_customer_ref']['und']['#ajax'] = array(
'callback' => 'test_ajax_test_callback',
'wrapper' => 'test_ajax_field',

Category:bug» support

Status:Active» Closed (cannot reproduce)

Version:7.x-1.0-rc1» 7.x-1.0
Status:Closed (cannot reproduce)» Active

I'm having the same problem described by lasimon.
I tried on a clean install and the problem persists.

I created an entity reference field in a basic page and set the entity selection to another content type.
Then in a module i added:

function a_module_callback($form, $form_state){
'The content';
a_module_form_page_node_form_alter(&$form, &$form_state, $form_id) {
$form['field_company']['#ajax'] = array(
'callback' => 'a_module_callback',
'wrapper' => 'container'
$form['another_select'] = array(
'#type' => 'select',
'#title' => 'Another select',
'#options' => array('1'=>'one', 2=>'two'),
'#ajax' => array(
'callback' => 'a_module_callback',
'wrapper' => 'container'
$form["body"]['#prefix'] = '<div id="container">';
$form["body"]['#suffix'] = '</div>';

The ajax doesn't work for the entity reference but it works for the another_select field.
Is this some kind of default behavior of entity reference?

Status:Active» Closed (works as designed)

Just noticed that the language identifier was missing.
Stupid error but it took some time to realize.

The warning remains. Pay attentions to the language.

Can you post the code with the fix?

Status:Closed (works as designed)» Active

I tried adding $form['field_company']['und'][0]['value']['#ajax'] but no luck

I get the ajax spinner but the callback isnt fired

bjalford try $form['field_company']['und']['#ajax']
or $form['field_company']['und'][0]['#ajax']
Depends on the name of the field. You can see it using the DPM module or firebug to inspect the text field.

couldnt get it to work. Could someone upload complete code for a field created through the UI?

Version:7.x-1.0» 7.x-1.x-dev

Hi, I am using the current dev-version. And I am not able to make ajax-callbacks run ...
My entity reference uses a views-list to select an user.
After selecting the user, I want to fill a text field in the current content type with the phonenumber of the selected user. I use a custom modul to add the ajax callback, but it doesn't work.
My code:

function ajax_refresh_fonnumber_field_widget_form_alter(&$element, &$form_state, $context) {
$element['#field_name']) && $element['#field_name'] == 'field_entity_reference_user') {
$element['und']['#ajax'] = array(
'callback' => 'ajax_refresh_fonnumber_auto_fill_callback',
ajax_refresh_fonnumber_auto_fill_callback(&$form, &$form_state) {
$commands = array();
$form_state['values']['ield_entity_reference_user'])) {
// get selected item/node from dropdown list
$uid = $form_state['values']['ield_entity_reference_user']['und'][0]['uid'];
// load node by uid
$user = user_load($uid);
$user) {
$user->field_fon['und'])) {
// fill text field
        // edit-field-fon-und-0-value represent the generated html id of the text field
$commands[] = ajax_command_invoke('#edit-field-fon-und-0-value', 'val', array($user->field_fon['und'][0]['value']));
  return array(
'#type' => 'ajax', '#commands' => $commands);

Is this code correct? Is there still a problem with ajax in entityreference?
Is there a way to debug ajax??


If you are using the Devel module you can put


at the top of your hook_form_alter() to see where to put the ['#ajax'] value.


Thank you for your hint. But as I am no programmer, maybe you could help me a bit more ...
I enabled devel modul and inserted your code. Now I get lot of debug information. For each field in my content type, there is an debug entry now. How do I find the correct place for the ['#ajax'] now?

#entity_type (String, 4 characters ) node
#bundle (String, 14 characters ) mycontenttype
#field_name (String, 23 characters ) field_user_reference
#language (String, 3 characters ) und
#field_parents (Array, 0 elements)
#columns (Array, 1 element)
    0 (String, 9 characters ) target_id
#title (String, 17 characters ) My Reference Field
#description (String, 0 characters )
#required (Boolean) FALSE
#delta (Integer) 0
#type (String, 6 characters ) select
#default_value (Array, 1 element)
    0 (String, 2 characters ) 82
#multiple (Boolean) FALSE
#options (Array, 3 elements)
    _none (String, 9 characters ) - Keine -
    82 (String, 45 characters ) - My User 1 ( 1st Function )
    42 (String, 76 characters ) - My User 2 ( 2nd Function )
#value_key (String, 9 characters ) target_id
#element_validate (Array, 1 element)
    0 (String, 29 characters ) options_field_widget_validate | (Callback) options_field_widget_validate();
#properties (Array, 4 elements)
    strip_tags (Boolean) TRUE
    optgroups (Boolean) TRUE
    empty_option (String, 11 characters ) option_none
    filter_xss (Boolean) FALSE

I didn't include the global entity-entry of this debug record.

Why are you using hook_field_widget_form_alter() instead of just hook_form_alter(). I don't think you would have much better luck implementing hook_form_alter() instead of the field_widget_form.

Hm, maybe I am wrong, but I want to fire the ajax directly after the value of the selectfield has been changed - inside the edit mode - without saving. I thought, using hook_form_alter() I need to save the form or to put an button in the form??

You are not implementing hook_field_widget_form_alter() correctly anyways

It should be something more like this:

function hook_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
// do something on element.
return array('value' => $element);

Where are you getting your example code from?

Hi haydeniv,
I got the example from this post:


Hoped, that it works, as there was no negativ feedback ....

Status:Active» Closed (works as designed)

You may want to post this question at http://drupal.stackexchange.com because your question is not really related to an entity reference field other than it happens to contain one. Every time we post here all of the people in this thread get notified.

What you are trying to accomplish is more like this question: http://drupal.stackexchange.com/questions/83909/autofilling-fields-based...

I can't think of any modules that will do what you want to do out of the box so a custom module is going to be your best bet. Getting into AJAX is pretty complicated stuff for a new programmer though. Putting this back to works as designed as the initial problem was taken care of.