You are here

function fz152_form_alter in FZ152 7

Same name and namespace in other branches
  1. 8 fz152.module \fz152_form_alter()

Implements hook_form_alter().

File

./fz152.module, line 85
Main file for hooks and custom functions.

Code

function fz152_form_alter(&$form, &$form_state, $form_id) {
  if (variable_get_value('fz152_enable')) {
    $modules_info = fz152_info();
    $active_forms = array();
    $pattern_for_forms = '';

    // Parse all active forms.
    foreach ($modules_info as $k => $v) {
      $form_callback = $v['form callback'];
      $active_forms = array_merge($active_forms, $form_callback());
    }

    // Implode all form id's in a single string. Which will be used for regex.
    foreach ($active_forms as $k => $v) {
      $pattern_for_forms .= $v['form_id'] . PHP_EOL;
    }
    $matches = fz152_form_id_matches($form_id, $pattern_for_forms);
    if (!empty($matches)) {
      $matched_form_id = $matches[0];

      // Find checkbox weight by matched form id with available in array.
      // Because we have support for wildcards, this is necessary.
      $checkbox_weight = NULL;
      foreach ($active_forms as $k => $v) {
        $current_form_id = str_replace('*', '.*', $v['form_id']);
        $pattern = "/{$current_form_id}/";
        if (preg_match($pattern, $matched_form_id)) {
          $checkbox_weight = $v['weight'];
          break;
        }
      }

      // Finally we add checkbox.
      $is_checkbox = variable_get_value('fz152_is_checkbox');
      if ($is_checkbox) {
        $form['fz152_agreement'] = array(
          '#type' => 'checkbox',
          '#required' => TRUE,
          '#title' => variable_get_value('fz152_checkbox_title'),
          // HTML5 support.
          '#attributes' => array(
            'required' => 'required',
          ),
        );
      }
      else {
        $form['fz152_agreement'] = array(
          '#name' => 'fz152-agreement',
          '#type' => 'item',
          '#markup' => variable_get_value('fz152_checkbox_title'),
        );
      }
      if ($checkbox_weight) {
        $form['fz152_agreement']['#weight'] = $checkbox_weight;
      }
    }
  }
}