blog-banner

Add Placeholder, Alter or Hide Image Captcha Title

  • Capcha
  • Drupal 7
  • HTML5
  • Templates

To add a placeholder or alter the title, description, and hide of the image captcha, do as follows

Use hook_form_FORM_ID_alter() and Form API pre_render function.

E.g:

/**
* Implements hook_form_FORM_ID_alter()
*/
function MY_MODULE_OR_THEME_form_contact_site_form_alter(&$form, &$form_state, $form_id) {
     $form['#pre_render'][] = 'yourform_pre_render';
}

function yourform_pre_render($element) {
   if (isset($element['captcha']['captcha_widgets']['captcha_response']['#title'])) {
           // change title
           $element['captcha']['captcha_widgets']['captcha_response']['#title'] = t('Change title name');
            // placeholder add
           $element['captcha']['captcha_widgets']['captcha_response']['#attributes']['placeholder'] = $element['captcha']['captcha_widgets']['captcha_response']['#title'];
            // empty title
           $element['captcha']['captcha_widgets']['captcha_response']['#title'] = '';
           $element['my_captcha_element']['captcha_widgets']['captcha_response']['#description'] = "New Description";
   }
return $element;
}

 

You can also use hook_form_alter() function:

 

function MY_MODULE_OR_THEME_form_alter(&$form, &$form_state, $form_id) {  
     if($form_id == 'FORM_ID') {
       $form['#pre_render'][] = 'yourform_pre_render';
     }
}

// Explains pre_render. This is when you create a form;

function yourform($form, &$form_state) {
    $form['#pre_render'][] = 'yourform_pre_render';
    $form['name'] = array(
        '#title' => t('name'),
        '#type' => 'textfield',
        '#required' => TRUE,
     );
    
    $form['actions'] = array(
         '#type' => 'actions',
    );
    $form['actions']['submit'] = array(
         '#type' => 'submit',
         '#value' => t('save'),
    );
return $form;
}

function yourform_pre_render($element) {
   $element['captcha']['captcha_widgets']['captcha_response']['#title'] = t('Your name');
   return $element;
}

Get awesome tech content in your inbox