You are here

function boost_captcha_form_alter in Boost Captcha 6

Same name and namespace in other branches
  1. 7 boost_captcha.module \boost_captcha_form_alter()

Implementation of hook_form_alter().

File

./boost_captcha.module, line 48

Code

function boost_captcha_form_alter(&$form, $form_state, $form_id) {
  static $form_id_regexp;
  $boost_captcha_filter_forms = variable_get('boost_captcha_filter_forms', '');

  // Clean up the form filter and save statically as this maybe called for
  // multiple forms.
  if (!isset($form_id_regexp)) {
    $form_id_regexp = '/^(' . preg_replace(array(
      '/(\\r\\n?|\\n)/',
      '/\\\\\\*/',
    ), array(
      '|',
      '.*',
    ), preg_quote($boost_captcha_filter_forms, '/')) . ')$/';
  }
  $is_form_excluded = FALSE;
  $filter_by_form = variable_get('boost_captcha_filter_by_form', 0);

  // Check form level filtering if some patterns are set
  if ($boost_captcha_filter_forms) {
    if ($filter_by_form > 0) {
      $form_match = preg_match($form_id_regexp, $form_id);

      // If $filter_by_form is 1 then listed forms are excluded from any filtering
      // and if 2 then filtering is applied only on listed forms.
      if ($filter_by_form == 1) {
        $is_form_excluded = $form_match;
      }
      else {
        $is_form_excluded = !$form_match;
      }
    }
  }
  else {
    if ($filter_by_form == 1) {
      $is_form_excluded = FALSE;
    }
    else {
      $is_form_excluded = TRUE;
    }
  }

  // If form is not excluded then mark the form as one where boost captcha logic
  // has to be applied
  if (!$is_form_excluded) {
    if (!isset($form['#prefix'])) {
      $form['#prefix'] = '';
    }
    if (!isset($form['#suffix'])) {
      $form['#suffix'] = '';
    }
    $form['#prefix'] = '<div class="boost-captcha-process-form">' . $form['#prefix'];
    $form['#suffix'] = $form['#suffix'] . '</div>';
  }
}