0

I am using the Captcha module to protect my site against spam.

I have put everything else on my form inside a fieldset which is collapsible by default. Howeve, the captcha fieldset is the only thing being output last and outside of the fieldset. Using hook_form_alter and dsm($form) I cannot see the captcha fieldset so i'm guessing it's not part of the form??

after scouring through the code i have discovered it's created via a theme function (theme_captcha) which I can override inside my template.php.

I would like to know how can I bring this fieldset inside the form just above it? Look at the screenshot attached.

captcha placement image

Thanks

3 Answers 3

3
+100

The fact that you are unable to view the $form['captcha'] element is due to the form workflow: CAPTCHA is added after your hook_form_alter.

In hook_form_alter documentation, it is stated that the order is

  1. module_A_form_alter
  2. module_A_form_BASE_ID_alter
  3. module_A_form_FORM_ID_alter
  4. module_B_form_alter
  5. module_B_form_BASE_ID_alter
  6. module_B_form_FORM_ID_alter

etc.

And

The module order is determined by system weight, then by module name.

CAPTCHA module uses hook_form_alter to add CAPTCHA element on the fly. So simply alter your module's weight to place it after CAPTCHA module.

2
  • This is the correct answer. You just need to change your module's weight so that it runs after the CAPTCHA module rather than before. Then you'll be able to see it in your hook_form_alter(). Commented Aug 15, 2013 at 14:47
  • All this gives me is #type, #captcha_type, #description, #weight. Doesn't help me to style (ideally, get rid of) the fieldset or inner parts of the CAPTCHA
    – user11312
    Commented Sep 3, 2013 at 18:22
3

Use hook_form_alter to hook into the form and programmatically insert the captcha element or adjust the weight of the element or pur the element inside the fieldset.

function example_form_alter(&$form, &$form_state, $form_id) {
  if($form_id == 'YOUR_FORM_ID') {
    // 1. Put the captcha element inside the fieldset 

    // OR

    // 2. Associate the capthca element to the form 
    //$form['captcha'] = array(
    //'#type' => 'captcha',
    //'#captcha_type' => 'image_captcha/Image',
    //);

    // OR

    // 3. Adjust the #weight attribute in the form 
  }
}

References

FORM API

How to add a CAPTCHA programmatically

If you need more details please let me have the $form variable in txt format.

2

If you are creating the form youself, you should be able to do something as simple as this:

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

where 'YOURFIELDSET' is the Submit Your Book fieldset. (Just don't have the Captcha automatically add it itself.)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.