9

I am on D7.

I have a content type that is webform enabled.

Requirement I need to implement captcha for all the webforms for nodes of this content type.

Problem In D7, you are supposed to give the form_id in the settings page to get captcha in the form, however I will have loads of forms with all different form_ids.

I am using the Captcha module.

Is there any way to implement my requirement?

9

You could add the following code to your template.php

if (strstr($form_id, 'webform_client_form')) {
        $form['my_captcha_element'] = array(
            '#type' => 'captcha',
            '#captcha_type' => 'image_captcha/Image',
        );
    }

this should be placed inside the yourthemename_form_alter

  • Thanks. I just got this solution, got it working, came back to answer my own question and saw that you already have. :) Thanks for the help nonetheless. – SGhosh Jan 17 '13 at 7:17
  • To have your custom code respect the permissions set for those people who can skip CAPTCHA add the following to your logic: if ((strstr($form_id, 'webform_client_form')) && !(user_access('skip CAPTCHA'))) { $form['my_captcha_element'] = array( '#type' => 'captcha', '#captcha_type' => 'default', ); } – kbrinner Apr 17 '15 at 21:49
1

Alternatively you can put this code in a custom module:

/**
* Implementation of hook_form_alter().
*/
function mymodule_form_alter(&$form, $form_state, $form_id) {
  if (preg_match("/^webform_client_form_[0-9]+$/",$form_id) && user_is_logged_in() == FALSE) {
    $form['my_captcha_element'] = array(
      '#type' => 'captcha',
    );
  }
}

Source: https://drupal.org/node/255795

0

To have your custom code respect the permissions set at admin/people/permissions for those people who can skip CAPTCHA, add the following to your logic:

if ((strstr($form_id, 'webform_client_form')) && !(user_access('skip CAPTCHA'))) {
    $form['my_captcha_element'] = array(
      '#type' => 'captcha',
      '#captcha_type' => 'default',
    );
  }

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.