Search code examples
drupaldrupal-6drupal-modulesdrupal-fapi

Drupal 6 Form Api adding an item to an existing form


I want to add a new item into an existing form. I have the ID of the form and I know I need to use hook form_alter but not sure how to add it.

function modulename_form_alter(&$form, $form_state, $form_id) {
    switch ($form_id) {
        case 'form id goes here':
            // Need to do something here....         
        break;
    }
}

Solution

  • Because the &$form variable is a reference, whatever you do to it changes the original value. so just add it to $form;

    //After, need to do something here:
    $form['my_new_field'] = array(
    
     '#type' => 'select',
     //etc..
    
     );
    
    
    //You can also add a new validation here:
    
    $form['#validate'][]  = 'my_valiation_callback';
    

    See the drupal api ref for better details: