You are here

function captcha_questions_form_alter in Captcha Questions 7

Same name and namespace in other branches
  1. 8 captcha_questions.module \captcha_questions_form_alter()

Implements hook_form_alter().

File

./captcha_questions.module, line 56
Module file for the Captcha questions module

Code

function captcha_questions_form_alter(&$form, &$form_state, $form_id) {

  // Only show captcha questions if user is anonymous.
  if (user_is_anonymous()) {

    // Fetching variables for form ids, question and description.
    $captcha_questions_form_ids = variable_get('captcha_questions_form_ids', array());
    $question = variable_get('captcha_questions_question', NULL);
    $description = variable_get('captcha_questions_description', t('Please answer the question.'));
    foreach ($captcha_questions_form_ids as $key => $value) {
      if ($value === $form_id) {

        // If we're on a multi page form, find current page.
        $form_page_count = isset($form['details']['page_num']['#value']) ? $form['details']['page_num']['#value'] : 1;

        // Set field to hidden unless on the first page.
        $form['captcha_questions_answer_given'] = array(
          '#type' => $form_page_count == 1 ? 'textfield' : 'hidden',
          '#description' => t("@description", array(
            '@description' => $description,
          )),
          '#title' => check_plain($question),
          '#size' => 60,
          '#required' => TRUE,
        );
        $form['#validate'][] = 'captcha_questions_form_validate';
      }
    }
    return $form;
  }
}