1

I am working on drupal 7 image captcha module.I want to alter or hide image captcha title like 'What is in your code'.How can I do it by without hacking the module?

1 Answer 1

4

use hook_form_FORM_ID_alter() and Form API pre_render function. E.g:

// Explains pre_render. This is when you create a form; 
function feedbackForm($form, &$form_state) {

  $form['#pre_render'][] = 'feedbackForm_pre_render';

  $form['name'] = array(
    '#title' => t('Ваше имя'),
    '#type' => 'textfield',
    '#required' => TRUE,
  );

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

  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Отправить'),
  );

  return $form;
}

function feedbackForm_pre_render($element) {
  $element['captcha']['captcha_widgets']['captcha_response']['#title'] = t('Защитный код');
  return $element;
}

You can also use hook_form_alter() function:

function MY_MODULE_OR_THEME_form_alter(&$form, &$form_state, $form_id) {
  dpm($form);

  if($form_id == 'FORM_ID') {
    $form['#pre_render'][] = 'feedbackForm_pre_render';
  }
}
2
  • Hi,Thank you for quick reply.It worked like charm.Just asking how did you get rendered array like $element['captcha']['captcha_widgets']['captcha_response']['#title'].Earlier I tried to use module_load_include for image_captcha.module to change image_captcha_cpatcha().Could it be done on that way? Commented Dec 12, 2013 at 8:05
  • 1
    @user3094034 I don't think that you need to change the image_captcha.module or its functions for this task.. hook_form_alter is the suitable way to achieve this kind of functionality.. If you didn't understand how did I get a rendered $element array, you can install Devel module and use its dpm function within pre_render function, like dpm($element) in my case. it will show you all elements of $element array in readable format.
    – xurshid29
    Commented Dec 12, 2013 at 8:17

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.