I'm building a module form and I'd like to insert a math captcha programmatically into the form if the site administrators haven't set an option in the captcha configuration forms. That is, if the captcha module is installed and another option hasn't been set through the captcha configuration form, display a math captcha.

It seems to me this kind of interface would be in the API, but that appears to be just for modules registering new captcha types. Is there a way for me to insert a captcha through code? On a quasi-related note, is there a way for my module to register the form with the captcha module so it displays in the captcha configuration menu?

Comments

jrosen’s picture

I am looking to accomplish something similar... subscribing.

soxofaan’s picture

Version: 6.x-1.0-rc2 » 6.x-2.x-dev
Status: Active » Fixed

I'd like to insert a math captcha programmatically into the form

In the CAPTCHA 6.x-2.x branch (e.g. 6.x-2.0-beta5), CAPTCHAs are Drupal Form API form elements, and you can add a CAPTCHA challenge to a form like you add textfields and checkboxes:

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

However, I would recommend to use the standard CAPTCHA admin interface instead, as this will be less confusing for the users of your module. e.g. how can they disable/change the type of a CAPTCHA if you hardcoded it in a module?

This brings us to your next question:

On a quasi-related note, is there a way for my module to register the form with the captcha module so it displays in the captcha configuration menu?

this is possible (and recommended over the solution above) with a call to the following function

/**
 * Helper function for adding/updating a CAPTCHA point.
 *
 * @param $form_id the form ID to configure.
 * @param captcha_type the setting for the given form_id,
 *   can be 'none' to disable CAPTCHA, 'default' to use the default challenge type
 *   or something of the form 'image_captcha/Image'. If capcha_type is NULL, the entry for the
 *   CAPTCHA type will be removed.
 * @return nothing
 */
function captcha_set_form_id_setting($form_id, $captcha_type) {
...

This function resides in captcha.inc, so you'll typically have to load it e.g. with
module_load_include('inc', 'captcha');

hope this helps.

barrett’s picture

Looks like exactly what I need. Many thanks, soxofaan.

Status: Fixed » Closed (fixed)

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

merzikain’s picture

This is for anyone looking for how to use a captcha type other than Math and haven't been able to figure it out.

I wanted to create a recaptcha field on my form so I set #captcha_type to "recaptcha/reCAPTCHA"

If you want to use something else and don't know what it should be, the easiest way to figure it out is to go to /admin/config/people/captcha and use something like firebug (or just hunt through the page source) to see the options for the Default challenge type field on that admin page. The value of each option is an available setting for #captcha_type.

Not exactly rocket science but also not completely obvious to some so hopefully this helps someone out there.