ga_push_form_validate.module in GA Push 8
Same filename and directory in other branches
Drupal Module: GA Push (form validate).
File
modules/form_validate/ga_push_form_validate.moduleView source
<?php
/**
* @file
* Drupal Module: GA Push (form validate).
*/
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_form_alter().
*/
function ga_push_form_validate_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$gafv_config = \Drupal::config('ga_push_form_validate.settings');
if ($gafv_config
->get('show_form_ids') && \Drupal::currentUser()
->hasPermission('admin ga push')) {
$form['ga_push_validate'] = [
'#markup' => t('FORM ID: @form_id', [
'@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, FormStateInterface $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, FormStateInterface $form_state) {
$errors = $form_state
->getErrors();
if (is_array($errors)) {
foreach ($errors as $key => $error) {
// @TODO: filter the error output.
$push = [
'eventCategory' => (string) t('Form validate error'),
'eventAction' => $form['form_id']['#value'] . ' - ' . $key,
'eventLabel' => (string) $error,
'eventValue' => 1,
];
ga_push_add_event($push);
}
}
}
/**
* Returns all logged forms.
*/
function ga_push_form_validate_get_logged_forms() {
$gafv_config = \Drupal::config('ga_push_form_validate.settings');
$forms = $gafv_config
->get('form_lists');
return array_map('trim', explode("\n", $forms));
}
Functions
Name | 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. |