Adding dynamic form elements using AHAH in Apply for for role Module

In Drupal AHAH is the best practice for adding removing from elements dynamically  I am not going to explain the basics if anyone want to learn what is AHAH go to http://drupal.org/node/331941. I have been using apply for role module in the registration form. I got a task form my client to add some fields dynamically in the registration form based on the role user is applying for.

Since i didn’t find any ready made solution i decided to do it my self. First thing that pops up in my mind is to use AHAH to populate fields. To use AHAH you first need to install the AHAH Helper module.

After installing the module you need to edit the module file. Modify the code in apply_for_role.module(6.x version). I know this is not the best approach but due to time constraints i was left with no choice. I would recommend to create a new module and override the specific hook  function apply_for_role_user.

1
2
3
4
5
6
switch ($op) {
    case 'register':
      // Admin created account aren't processed by the module.
      if (user_access('administer users')) {
        break;
      }

Write the following code after the these lines. Modify the div and other elements according to your design.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//Calling the form alter function to override it  
//Calling the form alter function to override it  
function apply_for_role_form_alter(&$form, $form_state, $form_id)
{
    //Registering the ahah function for this form
    ahah_helper_register(&$form, $form_state);
 
    if (variable_get('apply_for_role_register', 0)) {
        $filter_roles = array();
        foreach (variable_get('users_apply_roles', array()) as $rid => $role) {
            //In my case i am skipping few roles
            if ($rid > 2) {
                $filter_roles[$rid] = $role;
            }
        }
        if (count($filter_roles)) {
            $default         = !empty($form_state['values']['rid']) ? $form_state['values']['rid'] : array();
            //Add wrapping div to populate new form element in it. 	  
            $form['wrapper'] = array(
                '#prefix' => '<div id="ahah_wrapper">',
                '#suffix' => '</div>',
                '#weight' => -26 //determines where you want this field to populate. 		
            );
            if ((variable_get('apply_for_role_multiple', 0) == 0) && (variable_get('apply_for_role_register', 0) == 1)) {
                $filter_roles[0] = t('--');
                ksort($filter_roles);
            }
            $form['wrapper']['rid'] = array(
                '#type' => variable_get('apply_for_role_multiple', 0) ? 'checkboxes' : 'select',
                '#title' => variable_get('apply_for_role_multiple', 0) ? t('Which of these most closely describes you?') : t('Select a role'),
                '#options' => $filter_roles,
                '#default_value' => $default,
                '#required' => (variable_get('apply_for_role_register', 0) == 2) ? TRUE : FALSE,
                //Adding ahah event
                '#ahah' => array(
                    'path' => ahah_helper_path(array(
                        'wrapper'
                    )),
                    'wrapper' => 'ahah_wrapper'
                )
 
            );
 
            if ($form_state['values']['rid']) {
                //Adding dynamic form field     
                $form['wrapper']['security_number'] = array(
                    '#type' => 'textfield',
                    '#title' => t('Security number'),
                    '#description' => 'This is optional. A verified security number will enable us to provide you with additional content',
                    '#maxlength' => 20
                );
 
            }
        }
    }

About Sallah Ud Din Sarwar

I am a multi skilled Software Engineer with over 4 year’s industry experience in designing and developing Object oriented solutions in Multi layered & N-Tired. I have substantial success to my credit. I have consistently handled complex problems to the satisfaction of my clients by working in stressful situations. I enjoy learning and applying new technologies in a dynamic and forward thinking professional environment.

Check Also

Crack Python Interviews: Essential Questions Answered for Success

Mastering Python Interviews: Top Questions Answered! Introduction: Are you ready to ace your Python interview? …

Leave a Reply