4

I'm using the following contrib module: https://www.drupal.org/project/google_recaptcha

How do I add a recaptcha to my form programatically, since the form I'm using isn't listed under forms on the config page of the module?

The following doesn't seem to work:

$form['recaptcha'] = array(
  '#type' => 'recaptcha'
);
  • 1
    Not sure but you could try once . $form['captcha'] = array( '#type' => 'captcha', '#captcha_type' => 'recaptcha/reCAPTCHA', ); – DRUPWAY Sep 22 '16 at 9:50
6

The module doesn't implement hook_element_info, so it doesn't define a new element type. The code you're using looks like it's borrowed from the traditional captcha module, which does things a bit better and provides the element type.

Looking at the code, this is all you need in your form alter hook:

g_add_captcha($form, $form_id);
1

For Drupal 8, use the following (after installing the reCaptcha module):

$form['captcha'] = array(
  '#type' => 'captcha',
  '#captcha_type' => 'recaptcha/reCAPTCHA',
);

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.