1

There is a webform in my website, I added the captcha to this webform using this code:

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

This works fine. I need to insert a placeholder or default the text field of the image. I mean in the following picture :

enter image description here

the What code is in the image? should move inside the text field. How do I can do this?

Even though it is possible to make this done using javscript but I need to make this done using form alter.

Thanks

2

you can use this module too:

https://www.drupal.org/project/form_placeholder

it worked for me very nice

| improve this answer | |
  • kheili sepasgozaram – M a m a D Apr 20 '15 at 4:04
  • khahesh mikonam!! :))) – shekoufeh Apr 20 '15 at 4:16
1

Use an #after_build form element to drop the placeholder attribute into the form:

function THEME_form_alter(&$form, $form_state, $form_id) {
    if (strstr($form_id, 'webform_client_form')) {
        $form['my_captcha_element'] = array(
                '#type' => 'captcha',
                '#captcha_type' => 'image_captcha/Image',
        );
        $form['#after_build'][] = 'THEME_captcha_alter';
    }
}
function THEME_captcha_alter($form, &$form_state) {
  $form['my_captcha_element']['captcha_widgets']['captcha_response']['#attributes'] = array('placeholder' => 'My Placeholder');
  return $form;
}
| improve this answer | |
  • theme_captcha_alter doesn't exists at all! – M a m a D Apr 20 '15 at 4:38
  • I guess could have made that clearer. THEME_captcha_alter can be any arbitrary function name. The important thing is it can alter the captcha element after the captcha module is finished building it . – Shawn Conn Apr 20 '15 at 4:44
  • Ah, I forgot to include $form['#after_build'][] = 'THEME_captcha_alter'; statement – M a m a D Apr 20 '15 at 4:47

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.