I am trying to Ajaxify a node form element (field_unterbringung). When the user changes the value of an entity reference field (unterbringung) the markup should change from "old markup" to "new markup". But nothing happens. The ajax callback is not even called and drupal_set_message of "ajax callback is not showing up" is not showing up.

function customisations_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == "patientprofile_node_form") {
      $form['field_unterbringung']['und'][0]['value']['#prefix'] = '<div id="markup_wrapper">';
      $form['field_unterbringung']['und'][0]['value']['#suffix'] = '</div">';
      $form['field_unterbringung']['und'][0]['value']['#ajax']['callback'] = 'ajax_callback';
      $form['field_unterbringung']['und'][0]['value']['#ajax']['wrapper'] = 'markup_wrapper';
      $form['field_unterbringung']['und'][0]['value']['#ajax']['event'] = 'change';
      $form['field_unterbringung']['und'][0]['value']['#markup'] = "old markup";
      drupal_set_message('<pre>'. print_r($form['field_unterbringung'],TRUE) .'</pre>');

corrected callback:

function ajax_callback($form, &$form_state) {
  drupal_set_message("ajax callback firing", 'status');
  watchdog('customisations', 'ajax callback firing');
  return $form['field_unterbringung']['und'][0]['value'];


Turns out that when I add the ajax property without [0]['value'] the Ajax indicator shows up.

$form['field_unterbringung']['und']['#ajax'] = array(
    'callback' => 'customisations_ajax_callback',
    'wrapper' => 'markup_wrapper',
    'method' => 'replace',
  'event' => 'change',
  • You need to set the #markup in the form builder function, not the AJAX callback, and return the actual thing you're replacing from the callback (return $form['field_unterbringung']['und'][0]['value'];), not the whole form – Clive Apr 12 at 14:37
  • Thanks for the tip Clive. I made the correction. I think the ajax_callback function is alsonot even called properly (no "ajax callback firing" wd entry and no message when I change the value of the entity reference value). I tried select list and radio button widgets – user24957 Apr 12 at 15:09
  • When you write form builder function, is the hook form ALTER not enough or must the AJAX be added already in the hook form function? Most of the the examples show how to create form fields, but as the form is quite complex I added them through UI – user24957 Apr 13 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.