I have an entity reference field, and I'd like to do some ajax so that when a value is selected for that field, some text is displayed in a div below the field. Since it's on a node edit form, I'm using a form alter hook:

 * Implementation of hook_form_FORM_ID_alter()
function mymodule_form_blog_node_form_alter(&$form, &$form_state, $form_id) {
  $form['field_video_reference']['#ajax'] = array(
    'callback' => 'mymodule_video_ajax_callback',
    'wrapper' => 'my-path-div',

  $form['my_path'] = array(
    '#markup' => t('My path'),
    '#prefix' => '<div id="my-path-div">',
    '#suffix' => '</div>',
    '#weight' => 6,

  if (!empty($form_state['node']->field_video_reference)) {
    $form['my_path']['#markup'] = 'Token path: http://www.mysite.org/node/' . $form_state['node']->field_video_reference['und'][0]['target_id'];

function mymodule_video_ajax_callback($form, $form_state) {
  return $form['my_path'];

The code works fine when a the form is displayed and the field already has a value. However, the most likely use will be when the node is first being created, so I want this to trigger when a value is first added. The problem is, my callback is not being triggered at all. I'm following the first example in the "Javascript in Drupal" chapter in "Drupal 7 module Development," so from what I see, I it should be called when the field value is changed. Does this not work for fields that are already using ajax to get a value in the first place (i.e. an autocomplete field like this one), or is there something else I'm missing?


UPDATE: In case it is an issue with doing this on an autocomplete field, I added another test field that is a select list and attached the ajax to it as above, but the form is still not rebuilt when I select a value.

1 Answer 1


I think the problem is that $form['field_video_reference'] is a container and you want to attach the ajax event to the actual text field element.

If you inspect the $form array you should see the correct place to put it. In needs to be at the same level as the #type => 'textfield' (or entity reference) property.

I think the path will be something like:

$form['field_video_reference'][LANGUAGE_NONE][0]['value']['#ajax'] = array(
    'callback' => 'mymodule_video_ajax_callback',
    'wrapper' => 'my-path-div',
  • Although I am not op, but i was looking for same thing from quite some time. I don't have words to thank you. It was helpful to me also. Thanks again.
    – Ali Nouman
    Mar 21, 2014 at 18:01
  • 1
    @felix-eve, I owe you a beer (or whatever your drink of choice is). The actual value turned out to be $form_state['values']['field_video_reference']['und'][0]['target_id'] (entity reference fields use target_id instead of value), and the ajax is firing now. However, I'm not getting the field value in my callback, but I'll open another issue for that.
    – wonder95
    Mar 24, 2014 at 18:58
  • Here's my followup question for anyone interested.
    – wonder95
    Mar 24, 2014 at 20:37

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.