diggersworld
diggersworld

Reputation: 13080

Add custom code into Drupal 7 webform

I'm looking to add a custom captcha input into a Drupal Webform. The captcha I wish to use isn't available as a plugin so I want to hook the form as it is output and then add my captcha presentation script.

I already know how to catch a form with <theme>_form_alter(). What I'm not so sure of is how I can inject elements into the form at a certain point. If anyone knows of how to achieve this it would be useful.

Upvotes: 0

Views: 737

Answers (2)

jswitchback
jswitchback

Reputation: 141

You could add your script with a preventDefault on the form submit. Do your client-side validation then submit the form. Adding your javascript would be as follows:

/**
 * Implements hook_form_alter().
 */
function MODULENAME_form_alter(&$form, &$form_state, $form_id) {
  dpm($form_id);
  if ($form_id == 'YOUR_FORM_ID_HERE') { // Example Form ID: webform_client_form_23

    $form['#attached']['js'][] = drupal_get_path('module', 'MODULENAME') . 'path/to/example_file.js';

    // OR

    drupal_add_js(drupal_get_path('module', 'MODULENAME') . 'path/to/example_file.js');

  }
}

Upvotes: 1

Coder anonymous
Coder anonymous

Reputation: 927

You can do it with ease by overriding the default form creation. Create a file in your template folder for your webform say its webform-form-22.tpl.php.. Use the $form variable which is available by default. Place the form elements from the $form variable and place your custom captcha according to your requirement anywhere in the form, and validate in the hook custom_module_webform_submission_insert..

Upvotes: 0

Related Questions