I have created a content type named "main menu". I have few select lists which depend on one another. I wanted to display a different list when different checkboxes are selected. I figured out that I need to use Ajax to edit the form in order to achieve that but can anyone tell me how to use Ajax with hook_form_alter?

The form id of the form I am supposed to edit is "main_menu_node_form". When I try to access this form using hook_form_alter(), and add the #ajax property of the form fields, it seems that I am not able to do that since the fields are already rendered before the handle is passed to hook_form_alter(). Can anyone please help me solve this?

Note : I have created the content type using hook_install().

function main_menu_content_type_form_main_menu_node_form_alter(&$form, &$form_state, $form_id) {

  $form['field_mlnk'] = array(
    '#ajax' => array(
      'callback' => 'main_menu_content_type_form_main_menu_node_form_alter_callback',
      'wrapper' => 'link-div',
      'method' => 'replace',
      'effect' => 'fade',
  • 1
    It would be helpful to see the code of the form you are trying to alter, and the code you are using to alter it.
    – apaderno
    Commented Apr 1, 2012 at 13:41
  • @kiamlaluno I have created a content type main_menu using hook_install function. I wanted to display fields while adding content according to the selection made in lists. Should I add the ajax properties in hook_install or in Hook_form_alter. Commented Apr 1, 2012 at 21:43
  • Still, it would be useful to see the code you are using for hook_form_alter() (as you wrote, "When I try to access this form using form alter […[…]").
    – apaderno
    Commented Apr 1, 2012 at 22:25
  • @kiamlaluno I have added the code snippet to my question. Kindly see that and tell me whether the code does work or not. Commented Apr 1, 2012 at 22:32
  • It would be perfect if you would show also the code for main_menu_content_type_form_main_menu_node_form_alter_callback(); if that function contains code that is wrong, users cannot report any changes you should do to your code to make it work.
    – apaderno
    Commented Apr 1, 2012 at 22:41

1 Answer 1


I would be able to give you some detailed information about what you are doing wrong with hook_form_alter if you included some actual code from your custom module. However, since you are not giving any specifics, you can watch the video: How to build beautiful forms in Drupal 7 (note that I entered the URL so that it starts at the moment where the ajax is described). About 5 minutes of watching this will probably give you the answer to your question (since it describes exactly how to use ajax with hook_form_alter), but if you are really impatient, the actual code is shown somewhere around here.

If you want to see a live, well-tested and documented example of a form being altered using ajax, there is one in the Examples module. You can find the actual hook here and the callback function here.

I hope this helps. If it doesn't, give us some more background about your code and we will be able to tell you exactly what you are doing wrong.

  • @F123K I wanted to alter the existing fields in the form rather than adding new field to the form. So please tell me how that could be done. Commented Apr 1, 2012 at 22:03
  • Post edited with a sample hook and the callback function.
    – F1234k
    Commented Apr 2, 2012 at 11:20

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.