You are here

function event_log_track_form_submit in Events Log Track 8

Same name and namespace in other branches
  1. 8.2 event_log_track.module \event_log_track_form_submit()

Form submission callback.

1 string reference to 'event_log_track_form_submit'
event_log_track_form_alter in ./event_log_track.module
Implements hook_form_alter().

File

./event_log_track.module, line 126
Track the logs of form submissions or other actions that performed by user.

Code

function event_log_track_form_submit(&$form, FormStateInterface $form_state) {
  if (!empty($form_state->__event_log_track_logged)) {

    // Some forms are submitted twice, for instance the node_form.
    // We will only call the submit callback once.
    return;
  }
  $form_state->__event_log_track_logged = TRUE;

  // Get form id.
  $form_id = $form['#form_id'];

  // Dispatch the submission to the correct event handler.
  $handlers = event_log_track_get_event_handlers();
  foreach ($handlers as $type => $handler) {
    $dispatch = FALSE;
    if (!empty($handler['form_ids']) && in_array($form_id, $handler['form_ids'])) {
      $dispatch = TRUE;
    }
    elseif (!empty($handler['form_ids_regexp'])) {
      foreach ($handler['form_ids_regexp'] as $regexp) {
        if (preg_match($regexp, $form_id)) {
          $dispatch = TRUE;
          break;
        }
      }
    }
    if ($dispatch) {

      // Dispatch!
      $function = $handler['form_submit_callback'];
      if (function_exists($function)) {
        $log = $function($form, $form_state, $form_id);
        if (!empty($log)) {

          // Log the event.
          $log['type'] = $type;
          event_log_track_insert($log);
        }
      }
    }
  }
}