You are here

class OverviewForm in Events Log Track 8

Same name and namespace in other branches
  1. 8.2 src/OverviewForm.php \Drupal\event_log_track\OverviewForm

Configure user settings for this site.

Hierarchy

Expanded class hierarchy of OverviewForm

File

src/OverviewForm.php, line 16

Namespace

Drupal\event_log_track
View source
class OverviewForm extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'event_log_track_filter';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['filters'] = array(
      '#type' => 'details',
      '#title' => $this
        ->t('Filters'),
      '#description' => $this
        ->t('Filter the events.'),
      '#open' => TRUE,
    );
    $handlers = event_log_track_get_event_handlers();
    $options = array();
    foreach ($handlers as $type => $handler) {
      $options[$type] = $handler['title'];
    }
    $form['filters']['type'] = array(
      '#type' => 'select',
      '#title' => $this
        ->t('Type'),
      '#description' => $this
        ->t('Event type'),
      '#options' => array(
        '' => 'Select a type',
      ) + $options,
      '#ajax' => array(
        'callback' => '::formGetAjaxOperation',
        'event' => 'change',
      ),
    );
    $form['filters']['operation'] = EventLogStorage::formGetOperations(empty($form_state
      ->getUserInput()['type']) ? '' : $form_state
      ->getUserInput()['type']);
    $form['filters']['user'] = array(
      '#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'] = array(
      '#type' => 'textfield',
      '#size' => 5,
      '#title' => $this
        ->t('ID'),
      '#description' => $this
        ->t('The id of the events (numeric).'),
    );
    $form['filters']['ip'] = array(
      '#type' => 'textfield',
      '#size' => 20,
      '#title' => $this
        ->t('IP'),
      '#description' => $this
        ->t('The ip address of the visitor.'),
    );
    $form['filters']['name'] = array(
      '#type' => 'textfield',
      '#size' => 10,
      '#title' => $this
        ->t('Name'),
      '#description' => $this
        ->t('The name or machine name.'),
    );
    $form['filters']['path'] = array(
      '#type' => 'textfield',
      '#size' => 30,
      '#title' => $this
        ->t('Path'),
      '#description' => $this
        ->t('keyword in the path.'),
    );
    $form['filters']['keyword'] = array(
      '#type' => 'textfield',
      '#size' => 10,
      '#title' => $this
        ->t('Description'),
      '#description' => $this
        ->t('Keyword in the description.'),
    );
    $form['filters']['submit'] = array(
      '#type' => 'submit',
      '#value' => $this
        ->t('Submit'),
    );
    if (!empty($form_state
      ->getUserInput())) {
      $form['filters']['reset'] = array(
        '#type' => 'submit',
        '#value' => $this
          ->t('Reset'),
        '#limit_validation_errors' => array(),
        '#submit' => array(
          '::resetForm',
        ),
      );
    }
    $header = array(
      array(
        'data' => $this
          ->t('Updated'),
        'field' => 'created',
        'sort' => 'desc',
      ),
      array(
        'data' => $this
          ->t('Type'),
        'field' => 'type',
      ),
      array(
        'data' => $this
          ->t('Operation'),
        'field' => 'operation',
      ),
      array(
        'data' => $this
          ->t('Path'),
        'field' => 'path',
      ),
      array(
        'data' => $this
          ->t('Description'),
        'field' => 'description',
      ),
      array(
        'data' => $this
          ->t('User'),
        'field' => 'uid',
      ),
      array(
        'data' => $this
          ->t('IP'),
        'field' => 'ip',
      ),
      array(
        'data' => $this
          ->t('ID'),
        'field' => 'ref_numeric',
      ),
      array(
        'data' => $this
          ->t('Name'),
        'field' => 'ref_char',
      ),
    );
    $formData = !empty($form_state
      ->getUserInput()) ? $form_state
      ->getUserInput() : array();
    $limit = 20;
    $result = EventLogStorage::getSearchData($formData, $header, $limit);
    $rows = array();
    foreach ($result as $record) {
      if (!empty($record->uid)) {
        $account = User::load($record->uid);
        $userLink = $this
          ->l($account
          ->getUsername(), Url::fromUri('internal:/user/' . $account
          ->id()));
      }
      else {
        $account = NULL;
      }
      $rows[] = array(
        array(
          'data' => Html::escape(date("Y-m-d H:i:s", $record->created)),
        ),
        array(
          'data' => Html::escape($record->type),
        ),
        array(
          'data' => Html::escape($record->operation),
        ),
        array(
          'data' => Html::escape($record->path),
        ),
        array(
          'data' => strip_tags($record->description),
        ),
        array(
          'data' => empty($account) ? '' : $userLink,
        ),
        array(
          'data' => Html::escape($record->ip),
        ),
        array(
          'data' => Html::escape($record->ref_numeric),
        ),
        array(
          'data' => Html::escape($record->ref_char),
        ),
      );
    }

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

    // Finally add the pager.
    $build['pager'] = array(
      '#type' => 'pager',
    );
    $form['results'] = $build;
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $form_state
      ->disableRedirect();
    $form_state
      ->setRebuild();
  }

  /**
   * Resets all the states of the form.
   *
   * This method is called when the "Reset" button is triggered. Clears
   * user inputs and the form state.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   */
  public function resetForm(array &$form, FormStateInterface $form_state) {
    $form_state
      ->setRedirect('<current>');
    $form_state
      ->setValues([]);
  }

  /**
   * Ajax callback for the operations options.
   */
  public function formGetAjaxOperation(array &$form, FormStateInterface $form_state) {
    $ajax_response = new AjaxResponse();
    $element = EventLogStorage::formGetOperations($form_state
      ->getValue('type'));
    $ajax_response
      ->addCommand(new HtmlCommand('#operation-dropdown-replace', $element));
    return $ajax_response;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 87
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
OverviewForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
OverviewForm::formGetAjaxOperation public function Ajax callback for the operations options.
OverviewForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
OverviewForm::resetForm public function Resets all the states of the form.
OverviewForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.