You are here

function trick_question_form_alter in Trick Question 7

Same name and namespace in other branches
  1. 6 trick_question.module \trick_question_form_alter()

Alter the forms and add our field

File

./trick_question.module, line 194
trick_question.module - Main file for trick question module

Code

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

  // Is user allowed to skip question then simply return
  if (user_access('skip trick question')) {
    return;
  }
  $explanation = variable_get('trick_question_explanation', t('Yes, this is a trick question and easy to answer.<br />We ask it because spam bots are too stupid to answer correctly, while humans are not.'));
  $explanation = $explanation == '<none>' ? '' : $explanation;

  // Get the standard forms
  $forms = variable_get('trick_question_forms', array());

  // Get the custom forms
  $ids = array();
  $form_ids = explode("\n", variable_get('trick_question_form_ids', ''));
  if (is_array($form_ids)) {
    foreach ($form_ids as $id) {
      if (trim($id)) {
        $ids[$id] = $id;
      }
    }
  }

  // Merge them
  $forms = array_merge($forms, $ids);
  if (array_search($form_id, $forms, TRUE)) {

    // The form in question is in our list
    // Add the trick question
    $form['trick_question'] = array(
      '#type' => 'textfield',
      '#title' => variable_get('trick_question_question', t('Error in the Trick Question setup. Contact the admin.')),
      '#size' => 20,
      '#description' => filter_xss_admin($explanation),
      '#weight' => variable_get('trick_question_weight', 0),
    );

    // Add the validation handler
    $form['#validate'][] = 'trick_question_form_validate';
  }
}