[Drupal] How to Alter form in Drupal 7

| | 1 min read

Altering a form means adding or change any forms in a module. In Drupal 7 form alter could be done using the function hook_form_alter()

Understanding hook_form_alter().

hook_form_alter(). - here the form altered and all the form under this module can get here using parameters $form and $form_state
click for more documentation.

The sample code for hook_form_alter :


function mymodule_form_alter (&$form, &$form_state, $form_id) {
  dpm($form_id);
  dpm($form);
  if($form_id == 'something')
  {
    $form['#submit'][] = '_custom_submit_function';
    /* Refer Form API of drupal 7 */
  }
}

More about form_alter() parameters

$form: array of form elements.
$form_state: array value. Current state of the form.
$form_id: String value. function name that generated the form.