You are here

ga_push_form_validate.module in GA Push 7

Same filename and directory in other branches
  1. 8 modules/form_validate/ga_push_form_validate.module

Drupal Module: GA Push (form validate).

File

modules/form_validate/ga_push_form_validate.module
View source
<?php

/**
 * @file
 * Drupal Module: GA Push (form validate).
 */

/**
 * Implements hook_menu().
 */
function ga_push_form_validate_menu() {
  $items['admin/config/system/ga-push/form-validate'] = array(
    'title' => 'Ga push: form validate',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'ga_push_form_validate_settings_form',
    ),
    'access arguments' => array(
      'admin ga push',
    ),
    'description' => 'Global configuration of ga push.',
    'file' => 'ga_push_form_validate.admin.inc',
    'type' => MENU_LOCAL_TASK,
    'weight' => 1,
  );
  return $items;
}

/**
 * Implements hook_form_alter().
 */
function ga_push_form_validate_form_alter(&$form, &$form_state, $form_id) {
  if (variable_get('ga_push_form_validate_show_form_ids', FALSE) && user_access('admin ga push')) {
    $form['ga_push_validate'] = array(
      '#markup' => t('FORM ID: @form_id', array(
        '@form_id' => $form_id,
      )),
    );
  }
  $logged_forms = ga_push_form_validate_get_logged_forms();
  $log = FALSE;
  foreach ($logged_forms as $value) {
    $pattern = '/' . $value . '/';
    if (preg_match($pattern, $form_id)) {
      $log = TRUE;
    }
  }
  if ($log) {
    $form['#after_build'][] = 'ga_push_form_validate_form_after_build';
  }
}

/**
 * Add the function on the last place to record all errors.
 */
function ga_push_form_validate_form_after_build($form, $form_state) {
  $form['#validate'][] = 'ga_push_form_validate_get_validate_errors';
  return $form;
}

/**
 * Push form errors to GA.
 */
function ga_push_form_validate_get_validate_errors($form, $form_state) {
  $errors = form_get_errors();
  if (is_array($errors)) {
    foreach ($errors as $key => $error) {

      // @TODO: filter the error output.
      $push = array(
        'eventCategory' => t('Form validate error'),
        'eventAction' => $form['form_id']['#value'] . ' - ' . $key,
        'eventLabel' => $error,
        'eventValue' => 1,
      );
      ga_push_add_event($push);
    }
  }
}

/**
 * Returns all logged forms.
 */
function ga_push_form_validate_get_logged_forms() {
  $forms = variable_get('ga_push_form_validate_forms', '');
  return array_map('trim', explode("\n", $forms));
}

Functions

Namesort descending Description
ga_push_form_validate_form_after_build Add the function on the last place to record all errors.
ga_push_form_validate_form_alter Implements hook_form_alter().
ga_push_form_validate_get_logged_forms Returns all logged forms.
ga_push_form_validate_get_validate_errors Push form errors to GA.
ga_push_form_validate_menu Implements hook_menu().