In the CAPTCHA branch 6.x-2.x, the CAPTCHA stuff was refactored to a real drupal form element, which makes things much cleaner, more in line with drupal standards and makes it easier for other modules to integrate with the CAPTCHA module.
This should be documented better somewhere (e.g. in a README file or a drupal.org handbook page).
For the time being we can iterate here a bit on this documentation.

Adding a CAPTCHA element to a form is as easy as adding a text field or option list (maybe even easier):

$form['my_captcha_element'] = array(
  '#type' => 'captcha',
);

This adds a default CAPTCHA element to the form.
The CAPTCHA type can be set explicitly too:

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

Comments

soxofaan’s picture

a5342346’s picture

Hi,

I'm building a standalone login page, with a custom-themeable LoginForm. I'm following instructions at,

"Theming the Drupal 6 User Login Form"
http://blog.aphexcreations.net/2009/04/theming-drupal-user-login-form.html

I've created an override "user-login.tpl.php"

	<?php $login_form = get_user_login_form(); ?>
	<?php print $login_form->form_start; ?>
	Username: <?php print $login_form->name; ?><br />
	Password: <?php print $login_form->pass; ?><br />
	<?php print $login_form->submit; ?>
	<?php print $login_form->form_end; ?>

as well as a standalone LoginPage (page-login.tpl.php), in which I output content, as usual, with


					<?php print $content; ?>

Which nicely outputs a functional login form -- but WITHOUT the display of my Drupal6 CAPTCHA module's reCaptcha element. The module's installed OK, and functions fine -- if/when display in the NOT-overriden, standard Login form.

What I'd like to do is include the reCaptcha element in MY form.

To do that, as a first step, I added the snippet from above to my theme's template.php as referenced in the link/tutorial above, i.e.,

      function get_user_login_form() {
      ...
      $form['#build_id'] = sprintf('form-%s', md5(uniqid(mt_rand(), TRUE)));
 +   $form['my_captcha_element'] = array(
 +     '#type' => 'captcha',
 +     '#captcha_type' => 'captcha/Math',
 +   )
      $form_state = array();
      ...

But, that's apparently not correct/sufficient -- I simply see my original/custom form, with no added (re)Captcha "Math" challenge visible, and nothing Captcha-related in the generated PageSource.

What's the right method to get the Captcha element added to "my" customizable form?

Thanks!

BenDJ

a5342346’s picture

got it figured out.

in addition to

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

the function in template.php needs

	$out->my_captcha_element = drupal_render($form['my_captcha_element']);

and then, the user-login.tpl.php needs,

	<?php print $login_form->my_captcha_element; ?>

AFter cache clear, the recaptcha element displays as expected.

a5342346’s picture

i spoke a little too soon ...

the code above _displays_ the (re)captcha on my custom login page just fine.

but, the (re)captcha is _not_ part of the login validation flow -- username/password, with NOT (re)captcha entry/submit -- is sufficient to login. i.e., the (re)captcha's displayed, but ignored.

i can, i suppose, manually code an 'is_valid' check on the captcha response, but how would/should that be done still using the (re)captcha module's hooks?

soxofaan’s picture

Does it work with the simple math CAPTCHA? (reCAPTCHA is a bit of a special case and the math CAPTCHA is easier for debugging)

a5342346’s picture

i'm taking a different approach -- using a custom module, and working with #redirect.

so, for now, n/a.

thanks then.

a5342346’s picture

Status: Active » Closed (fixed)
soxofaan’s picture

Status: Closed (fixed) » Active

Please don't close this thread as the original issue is not fixed yet: better documentation on how to add a CAPTCHA widget programmatically

a5342346’s picture

sry, my mistake. thought it was one of my threads, and i don't know of any other way to stop following an issue ...

joyseeker’s picture

I want to add the CAPTCHA to a Profile form. There's no "markup" component like in Webform, so where would I put the PHP from the initial description above?

soxofaan’s picture

at joyseeker in #11: you don't need any PHP discussed here
adding a CAPTCHA to a profile form is possible out of the box with the CAPTCHA module:
* enable the option "add CAPTCHA administration links to forms" on the CAPTCHA admin page
* go to the profile form you want to add a CAPTCHA to, and use the (collapsed) CAPTCHA administration fieldset over there.

joyseeker’s picture

to #12

Thanks so much for taking the time to answer me! Yes, I now have the CAPTCHA on my Profile form.

white_pawn’s picture

Any way we could change the reCAPTCHA theme before/after loading 'my_captcha_element'?

soxofaan’s picture

Simon Naude’s picture

<?php
$form['my_captcha_element'] = array(
  '#type' => 'captcha',
  '#captcha_type' => 'captcha/Math',
);
?>

I added this to my custom registration form. It seems to work. Thanks for the code.

ransomweaver’s picture

I have an exposed filter on a view that needs a captcha, but it appears I can't enable it from the Captcha settings. I actually have two exposed filters, and they both appear in drupalforfirebug as form>views_exposed_form.

Adding the form in hook_form_alter using

$form['contact_wrapper']['info_captcha_element'] = array(
'#type' => 'captcha',
'#weight' => 35,
);

places the captcha form where I want it, but the page loads initially displaying :

* Math question field is required.
* The answer you entered for the CAPTCHA was not correct.

And when I submit I get: The answer you entered for the CAPTCHA was not correct.

Anyone know what is up with this? The form is in an expanded fieldset.

soxofaan’s picture

@ransomweaver: indeed, I could reproduce the your issue (with the standard way to add CAPTCHA to a form, not programmatically in form_alter). I think it's best to open a separate issue/ticket for this one. It seems an exposed view filter form is automatically submitted (before you get the chance to fill it in) in order to show a default view.

ransomweaver’s picture

josepvalls’s picture

What about image_captcha
It's probably trivial, but I can't figure it out.

update: I found it.

$form['captcha'] = array(
  '#type' => 'captcha',
  '#captcha_type' => 'image_captcha/Image',
);
-enzo-’s picture

If you need add an image captcha for a specific content type like webform, you can use the following code

<?php
  //Add captcha to all webforms
  if (strstr($form_id, 'webform_client_form')) {
        $form['my_captcha_element'] = array(
            '#type' => 'captcha',
            '#captcha_type' => 'image_captcha/Image',
        );
    }
?>
cknoebel’s picture

#21 works great. I'd add that it should go into a custom module using hook_form_alter:

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  //Add captcha to all webforms
  if (strstr($form_id, 'webform_client_form')) {
        $form['my_captcha_element'] = array(
            '#type' => 'captcha',
            '#captcha_type' => 'image_captcha/Image',
        );
    }
}
typehost’s picture

In Drupal 7 and the theme I am working with:

in template.php ---> added:

function mythemename_preprocess_user_login(&$variables) {
  $variables['form'] = $variables[''];
+  $form['captcha'] = array(
+  '#type' => 'captcha',
+  '#captcha_type' => 'image_captcha/Image',
);
}

and in user-login.tpl.php:

<?php
+     print drupal_render($form['captcha']);
?>

This worked fine for the user login page, but for some reason captcha was still not displaying on the user registration page where most of the spam comes from - despite the fact that it had no custom template or modifications and should have worked through admin config options on the captcha module (user_register_form). I added a hook form alter function in template.php to correct that.

final code in template.php:

<?php

function mythemename_theme() {
  return array(
	'user_login' => array(
		'template' => 'user-login',
		'arguments' => array('form' => NULL),
    ),
	'user_register_form' => array(
		'arguments' => array('form' => NULL),
    ),
  );
}
//
function mythemename_preprocess_user_login(&$variables) {
  $variables['form'] = $variables[''];
  $form['captcha'] = array(
  '#type' => 'captcha',
  '#captcha_type' => 'image_captcha/Image',
);
}
function mythemename_preprocess_user_register_form(&$variables) {
  $variables['form'] = $variables[''];
  $form['captcha'] = array(
  '#type' => 'captcha',
  '#captcha_type' => 'image_captcha/Image',
);
}
function mythemename_form_alter(&$form, &$form_state, $form_id) {
  if (strstr($form_id, 'user_register_form')) {
        $form['catcha'] = array(
            '#type' => 'captcha',
            '#captcha_type' => 'image_captcha/Image',
        );
    }
}

Final code in user-login.tpl.php:

<?php
     print drupal_render($form['name']);
     print drupal_render($form['pass']);
     print drupal_render($form['links']);
     print drupal_render($form['captcha']);
     print drupal_render($form['form_build_id']);
     print drupal_render($form['form_id']); 
     print drupal_render($form['actions']);
?>
soxofaan’s picture

@typehost in #23: I'm afraid that adding CAPTCHA in the theme layer/phase is too late to work properly. Have you tried already posting a form with wrong CAPTCHA response?

jive01’s picture

For Hidden Captcha:


 $form['my_captcha_element'] = array(
    '#type' => 'captcha',
    '#captcha_type' => 'hidden_captcha/Hidden CAPTCHA',
  );

maximkashuba’s picture

Issue summary: View changes

For Draggable Captcha:

<?php
 $form['my_captcha_element'] = array(
      '#type' => 'captcha',
      '#captcha_type' => 'draggable_captcha/Draggable Captcha',
    );
?>
mnask’s picture

how can i find the value(i.e 3+4=) the user has to enter ?

Miri Meltzer’s picture

// Set captcha image for specific webform form.
module_load_include('inc', 'captcha');
$form_id = 'webform_client_form_' . $my_form_id();
$captcha_type = 'image_captcha/Image';
captcha_set_form_id_setting($form_id, $captcha_type);

wundo’s picture

Version: 6.x-2.x-dev » 8.x-1.x-dev
wundo’s picture

Adding a CAPTCHA programmatically nowadays it's just about exporting the config entity properly, marking this as fixed.

wundo’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.