1

I'm trying to register an ajax listener to a checkbox on one of my standard node forms. I want to display certain regions of the form only in case, the field is checked. I can get the drupal ajax ecosystem to work, when I use it in a custom form, but I'm unable to make it work on my node forms.

First I hook into form_alter to check if I'm with the node type I want to add the listener for:

function interceptor_form_alter(&$form, &$form_state, $form_id) {
  if($form_id === 'film_node_form') {
    interceptor_dvd_listener($form, $form_state);
  }
}

Then I try to attach the ajax stuff to my checkbox, which name is field_dvd:

function interceptor_dvd_listener(&$form, &$form_state) {
  $form['field_dvd'] = array(
    '#title' => t('You want to display DVD informations too?'),
    '#type' => 'checkbox',
    '#ajax' => array(
      'callback' => 'interceptor_dvd_listener_callback',
      'wrapper' => 'checkboxes-div',
      'effect' => 'slide',
      'progress' => array('type' => 'none'),
    ),
  );
  return $form;
}

The function interceptor_dvd_listener_callback is never called. I try to print some debug information there, but nothing happens...

function interceptor_dvd_listener_callback($form, $form_state) {
  data_service_log_object($form);
}

UPDATE

After passing $form as reference to interceptor_dvd_listener the ajax callback function got called like expected. I edited my code above.

2
  • It seems right code. When you check the checkbox an AJAX call is made?
    – sanzante
    Commented Jul 28, 2014 at 17:26
  • what is data_service_log_object? a custom function? google doesn't give me anything but this question - maybe the problem is with that function, not the ajax call.
    – Geoff
    Commented Jul 28, 2014 at 20:32

1 Answer 1

1

This interceptor_dvd_listener_callback() should return value, in documentation:

After form processing is complete, ajax_form_callback() calls the function named by #ajax['callback'], which returns the form element that has been updated and needs to be returned to the browser, or alternatively, an array of custom Ajax commands.

Why don't you use this: https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#states

It is drupal 7 standard feature to hide or show some fields when some elements has some states.

1
  • Right. Drupal form states are a better solution for my case. Thanks! Commented Jul 29, 2014 at 8:54

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.