You are here

public function OverviewForm::buildForm in Admin Audit Trail 1.0.x

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/OverviewForm.php, line 102

Class

OverviewForm
Configure user settings for this site.

Namespace

Drupal\admin_audit_trail

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['filters'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Filters'),
    '#description' => $this
      ->t('Filter the events.'),
    '#open' => TRUE,
  ];
  $handlers = admin_audit_trail_get_event_handlers();
  $options = [];
  foreach ($handlers as $type => $handler) {
    $options[$type] = $handler['title'];
  }
  $form['filters']['type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Type'),
    '#description' => $this
      ->t('Event type'),
    '#options' => [
      '' => $this
        ->t('Select a type'),
    ] + $options,
    '#ajax' => [
      'callback' => '::formGetAjaxOperation',
      'event' => 'change',
    ],
  ];
  $form['filters']['operation'] = AdminAuditTrailStorage::formGetOperations(empty($form_state
    ->getUserInput()['type']) ? '' : $form_state
    ->getUserInput()['type']);
  $form['filters']['user'] = [
    '#type' => 'entity_autocomplete',
    '#target_type' => 'user',
    '#selection_settings' => [
      'include_anonymous' => FALSE,
    ],
    '#title' => $this
      ->t('User'),
    '#description' => $this
      ->t('The user that triggered this event.'),
    '#size' => 30,
    '#maxlength' => 60,
  ];
  $form['filters']['id'] = [
    '#type' => 'textfield',
    '#size' => 5,
    '#title' => $this
      ->t('ID'),
    '#description' => $this
      ->t('The id of the events (numeric).'),
  ];
  $form['filters']['ip'] = [
    '#type' => 'textfield',
    '#size' => 20,
    '#title' => $this
      ->t('IP'),
    '#description' => $this
      ->t('The ip address of the visitor.'),
  ];
  $form['filters']['name'] = [
    '#type' => 'textfield',
    '#size' => 10,
    '#title' => $this
      ->t('Name'),
    '#description' => $this
      ->t('The name or machine name.'),
  ];
  $form['filters']['path'] = [
    '#type' => 'textfield',
    '#size' => 30,
    '#title' => $this
      ->t('Path'),
    '#description' => $this
      ->t('keyword in the path.'),
  ];
  $form['filters']['keyword'] = [
    '#type' => 'textfield',
    '#size' => 10,
    '#title' => $this
      ->t('Description'),
    '#description' => $this
      ->t('Keyword in the description.'),
  ];
  $form['filters']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Submit'),
  ];
  $header = [
    [
      'data' => $this
        ->t('Updated'),
      'field' => 'created',
      'sort' => 'desc',
    ],
    [
      'data' => $this
        ->t('Type'),
      'field' => 'type',
    ],
    [
      'data' => $this
        ->t('Operation'),
      'field' => 'operation',
    ],
    [
      'data' => $this
        ->t('Path'),
      'field' => 'path',
    ],
    [
      'data' => $this
        ->t('Description'),
      'field' => 'description',
    ],
    [
      'data' => $this
        ->t('User'),
      'field' => 'uid',
    ],
    [
      'data' => $this
        ->t('IP'),
      'field' => 'ip',
    ],
    [
      'data' => $this
        ->t('ID'),
      'field' => 'ref_numeric',
    ],
    [
      'data' => $this
        ->t('Name'),
      'field' => 'ref_char',
    ],
  ];
  $this
    ->getFiltersFromUrl($form);
  $result = AdminAuditTrailStorage::getSearchData($this->filters, $header, 20);
  if (!empty($this->filters)) {
    $form['filters']['reset'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Reset'),
      '#limit_validation_errors' => [],
      '#submit' => [
        '::resetForm',
      ],
    ];
  }
  $rows = [];
  foreach ($result as $record) {
    $userLink = $this
      ->getUserData($record->uid);
    $rows[] = [
      [
        'data' => date("Y-m-d H:i:s", $record->created),
      ],
      [
        'data' => $record->type,
      ],
      [
        'data' => $record->operation,
      ],
      [
        'data' => $record->path,
      ],
      [
        'data' => strip_tags($record->description),
      ],
      [
        'data' => $userLink,
      ],
      [
        'data' => $record->ip,
      ],
      [
        'data' => $record->ref_numeric,
      ],
      [
        'data' => $record->ref_char,
      ],
    ];
  }

  // Generate the table.
  $build['config_table'] = [
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#empty' => $this
      ->t('No events found.'),
  ];

  // Finally add the pager.
  $build['pager'] = [
    '#type' => 'pager',
    '#parameters' => $this->filters,
  ];
  $form['results'] = $build;
  return $form;
}