2

I have created a reset button by adding a customized module (Reset) for webform. But this button gets displayed all over the site. I just want to display this button on a single webform.

Below is my Reset module code:

<?php
/*
 *
 *  creating  hook_form_alter()
 *
 */
function custom_example_form_alter(&$form,&$form_state,$form_id) {
            $form['webform_client_form_34'] = array(
            '#weight' => 1001,    
            '#markup' =>'<input class="form-button" type="reset" value="Reset"/>',
            );
            return $form;
}

How should the if condition look like that I should be adding to it ?

3

Find the form id first.

Assume your form id is 50 than,

function <module>_form_alter(&$form, $form_state, $form_id) {
  if (strpos($form_id, 'webform_client_form_50') === 0) {
    $form['actions']['reset'] = array(
      '#type' => 'button',
      '#value' => t('Reset'),
      '#weight' => 100,
      '#validate' => array(),
      '#attributes' => array('onclick' => 'this.form.reset(); return false;'),
    );
  }
}

OR

 if ($form_id == 'my_webform_client_form_id') {

Your code

}

Another method :-

In Drupal 7, you can either use hook_form_alter() or hook_form__alter(), which ever you prefer. Just make sure you get the naming and parameters right. Drupal 6 only supports hook_form_alter() however.

When you create these functions, also remember that Drupal may not always pick up on them until you've flushed the cache.

function mymodule_form_webform_client_form_44_alter(&$form, &$form_state) {
// code here;
}

Please remember to clear your cache also and try to disable/enable your module also

2

If you're using hook_form_alter() to do this, you need to have a if statement that looks at the form id that corresponds with your node. For example....

if ($form_id == 'webform_client_form_34') {
  $form['webform_client_form_34'] = array(
    '#weight' => 1001,    
    '#markup' =>'<input class="form-button" type="reset" value="Reset"/>',
  );
}
1
  • Not working ,the button still displaying all over other locations throughout the site . – Rajender Rajan May 15 '15 at 7:09
2

To further complete/complement Shawn's answer (and the comment like "not working"), make sure you use the correct value for the $form_id.

To actually find this correct value, there are a few options, like:

  • insert a line like dpm($form_id); in front of the if statement.

  • use the Get Form ID module to easily find out form id of any form in Drupal, for which a video demo is available also. Here is a quote about it from its project page:

    By hovering over any form you will see a contextual link available. By expanding the link you will see the form ID. Click on the link and you will see a modal window with copyable form id and hook_form_FORM_ID_alter() hook suggestion.

  • Use the improved and rebranded version of the (deprecated) Get Form ID module, which is the Devel form debug module, and for which a video demo is available. Here is a quote about it from its project page:

    By hovering over any form you will see a contextual link available. By expanding the link you will see the form ID. Click on the link and you will see a modal window with copyable form id and hook_form_FORM_ID_alter() hook suggestion.

After finding out the (for sure) correct value, I'd be curious if combining it with Shawn's answer you can get it to work ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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