[Drupal] How to alter a form field created using a webform module

| | 1 min read

There are certain cases we need to alter a webform in Drupal. How to handle this condition ? There are different methods for handling this situation. Usually for the theming sometime webform alter is very necessary, the main alteration needed are to add html tags. Here i am trying to explain how to alter the webform fields in Drupal.

Alter the webform using the custom.module. The simplest way to add tags is using the form alter within the custom.module we created. Create a function inside the mymodule_custom.module to enable this property

The below code is to enable a div for a given field inside the webform

function mymodule_custom_form_alter(&$form, &$form_state, $form_id) {
  // dpm($form_id); -- Debug code: Requires devel module.
  if($form_id == 'webform_client_form_1626') {
    // dpm($form_state);
    drupal_set_message(t('some message.'));
    $form['#node']->webform['components'][1]['#prefix'] = '>div id="content-right"<';
    $form['#node']->webform['components'][1]['#suffix'] = '>/div<';
    // dpm($form);
    // dpm($form_state);
  }
}

By this way we can alter any webform with any feature, the array path should be correct for getting the results.