Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

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) {
  dpm($form);
  dpm($form_state);

  $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',
    ),
  );
}
share|improve this question
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. –  kiamlaluno Apr 1 '12 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. –  Drupal Developer Apr 1 '12 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 […[…]"). –  kiamlaluno Apr 1 '12 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. –  Drupal Developer Apr 1 '12 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. –  kiamlaluno Apr 1 '12 at 22:41

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer
    
@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. –  Drupal Developer Apr 1 '12 at 22:03
    
Post edited with a sample hook and the callback function. –  F1234k Apr 2 '12 at 11:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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