You are here

public function DbLogController::overview in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/dblog/src/Controller/DbLogController.php \Drupal\dblog\Controller\DbLogController::overview()
  2. 10 core/modules/dblog/src/Controller/DbLogController.php \Drupal\dblog\Controller\DbLogController::overview()

Displays a listing of database log messages.

Messages are truncated at 56 chars. Full-length messages can be viewed on the message details page.

Return value

array A render array as expected by \Drupal\Core\Render\RendererInterface::render().

See also

Drupal\dblog\Form\DblogClearLogConfirmForm

Drupal\dblog\Controller\DbLogController::eventDetails()

1 string reference to 'DbLogController::overview'
dblog.routing.yml in core/modules/dblog/dblog.routing.yml
core/modules/dblog/dblog.routing.yml

File

core/modules/dblog/src/Controller/DbLogController.php, line 126

Class

DbLogController
Returns responses for dblog routes.

Namespace

Drupal\dblog\Controller

Code

public function overview() {
  $filter = $this
    ->buildFilterQuery();
  $rows = [];
  $classes = static::getLogLevelClassMap();
  $this->moduleHandler
    ->loadInclude('dblog', 'admin.inc');
  $build['dblog_filter_form'] = $this->formBuilder
    ->getForm('Drupal\\dblog\\Form\\DblogFilterForm');
  $header = [
    // Icon column.
    '',
    [
      'data' => $this
        ->t('Type'),
      'field' => 'w.type',
      'class' => [
        RESPONSIVE_PRIORITY_MEDIUM,
      ],
    ],
    [
      'data' => $this
        ->t('Date'),
      'field' => 'w.wid',
      'sort' => 'desc',
      'class' => [
        RESPONSIVE_PRIORITY_LOW,
      ],
    ],
    $this
      ->t('Message'),
    [
      'data' => $this
        ->t('User'),
      'field' => 'ufd.name',
      'class' => [
        RESPONSIVE_PRIORITY_MEDIUM,
      ],
    ],
    [
      'data' => $this
        ->t('Operations'),
      'class' => [
        RESPONSIVE_PRIORITY_LOW,
      ],
    ],
  ];
  $query = $this->database
    ->select('watchdog', 'w')
    ->extend('\\Drupal\\Core\\Database\\Query\\PagerSelectExtender')
    ->extend('\\Drupal\\Core\\Database\\Query\\TableSortExtender');
  $query
    ->fields('w', [
    'wid',
    'uid',
    'severity',
    'type',
    'timestamp',
    'message',
    'variables',
    'link',
  ]);
  $query
    ->leftJoin('users_field_data', 'ufd', 'w.uid = ufd.uid');
  if (!empty($filter['where'])) {
    $query
      ->where($filter['where'], $filter['args']);
  }
  $result = $query
    ->limit(50)
    ->orderByHeader($header)
    ->execute();
  foreach ($result as $dblog) {
    $message = $this
      ->formatMessage($dblog);
    if ($message && isset($dblog->wid)) {
      $title = Unicode::truncate(Html::decodeEntities(strip_tags($message)), 256, TRUE, TRUE);
      $log_text = Unicode::truncate($title, 56, TRUE, TRUE);

      // The link generator will escape any unsafe HTML entities in the final
      // text.
      $message = Link::fromTextAndUrl($log_text, new Url('dblog.event', [
        'event_id' => $dblog->wid,
      ], [
        'attributes' => [
          // Provide a title for the link for useful hover hints. The
          // Attribute object will escape any unsafe HTML entities in the
          // final text.
          'title' => $title,
        ],
      ]))
        ->toString();
    }
    $username = [
      '#theme' => 'username',
      '#account' => $this->userStorage
        ->load($dblog->uid),
    ];
    $rows[] = [
      'data' => [
        // Cells.
        [
          'class' => [
            'icon',
          ],
        ],
        $this
          ->t($dblog->type),
        $this->dateFormatter
          ->format($dblog->timestamp, 'short'),
        $message,
        [
          'data' => $username,
        ],
        [
          'data' => [
            '#markup' => $dblog->link,
          ],
        ],
      ],
      // Attributes for table row.
      'class' => [
        Html::getClass('dblog-' . $dblog->type),
        $classes[$dblog->severity],
      ],
    ];
  }
  $build['dblog_table'] = [
    '#type' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#attributes' => [
      'id' => 'admin-dblog',
      'class' => [
        'admin-dblog',
      ],
    ],
    '#empty' => $this
      ->t('No log messages available.'),
    '#attached' => [
      'library' => [
        'dblog/drupal.dblog',
      ],
    ],
  ];
  $build['dblog_pager'] = [
    '#type' => 'pager',
  ];
  return $build;
}