1

I need to add some AJAX functionality to a node form. Using hook_form_alter I'm overriding form elements. In this particular case I need to add a callback on the 'language' element provided by the translation module (Core, Content Translation).

However, when selecting a different option in the 'language' listbox, the page refreshes. What could trigger this behaviour?

This is the code I'm using:

function foo_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'bar_node_form') {
    $form['#prefix'] = '<div id="bar-form-wrapper">';
    $form['#suffix'] = '</div>';

    $form['language']['#ajax'] = array(
      'callback' => 'bar_form_callback',
      'wrapper' => 'bar-form-wrapper',
    );

    if (isset($form_state['values'])) {
      drupal_set_message('ajax callback');
    }
  }
}

function bar_form_callback($form, $form_state) {
  return $form;
}
1
  • Did you find a solution for this??, I'm facing the same problem... – Pablo May 9 '12 at 13:31
1

What exactly are you trying to do? Is the page refreshing or the entire $form? What you're asking it to do is by attaching #ajax to the select is that when it changes a value resubmit the entire $form, which would probably include any submit handlers. Typically #ajax would be directed at a form element rather than the entire form.

5
  • The complete page is refreshing. – Bart Dec 14 '11 at 22:12
  • So what are you trying to do actually? The callback function should just return the part(s) of the form that you want to update. Why are you returning the entire form? – Malks Dec 14 '11 at 22:37
  • I need to change properties of a few form elements. – Bart Dec 15 '11 at 7:00
  • Even if I replace/return one element in the callback the page reloads. – Bart Dec 15 '11 at 10:26
  • I'm out of ideas. If you get me your whole module I'm happy to try and give it a quick debug. – Malks Dec 15 '11 at 23:35
1

since the callback is acting as a form submit, your callback (_submit) should have the same naming convention as your form id.

"bar_node_form" is your form id so:

'callback' => 'bar_node_form_submit',
-1

When you specify 'callback' => 'bar_form_callback', it's not referring to a PHP callback per se. Callback in this case refers to a hook_menu() callback, in other words, the URL that JavaScript will call when the AJAX trigger is fired.

You need to define a bar_form_callback path in your custom_module_menu() function. That menu callback then call function bar_form_callback().

D.O. FAPI reference.

10
  • As explained in my question, adding AJAX to the form is what I intend to do. "What could trigger this behaviour?" is a question on what causes the page to reload (yes, a hard reload). The page reloads when I select a language in the select box. – Bart Dec 14 '11 at 18:43
  • Hopefully it's just that you're missing the hook_menu() callback. – amateur barista Dec 14 '11 at 19:12
  • I don't think you need a hook_menu() defined for the #ajax callbacks. – Malks Dec 14 '11 at 20:32
  • I think you do. Else, how is Drupal going to find your function? I doubt there's some automagical behavior for that. Also check your Firebug. When you click on the languages button, what is the XHR (AJAX) callback path? Probably is thenameofyoursite.com/bar_form_callback. – amateur barista Dec 14 '11 at 20:55
  • Also, check your PHP error log and the Drupal watchdog. It might have been throwing an error. "Hard" reload is not supposed to be the normal behavior, so one can only assume there's an error involved. I think again your error is that you're missing the AJAX callback (the hook_menu implementation). – amateur barista Dec 14 '11 at 20:56

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.