0

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.

2 Answers 2

1

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');

  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.