function DbLogController::formatMessage

Same name and namespace in other branches
  1. 9 core/modules/dblog/src/Controller/DbLogController.php \Drupal\dblog\Controller\DbLogController::formatMessage()
  2. 8.9.x core/modules/dblog/src/Controller/DbLogController.php \Drupal\dblog\Controller\DbLogController::formatMessage()
  3. 11.x core/modules/dblog/src/Controller/DbLogController.php \Drupal\dblog\Controller\DbLogController::formatMessage()

Formats a database log message.

Parameters

object $row: The record from the watchdog table. The object properties are: wid, uid, severity, type, timestamp, message, variables, link, name.

If the variables contain a @backtrace_string placeholder which is not used in the message, the formatted backtrace will be assigned to a new backtrace property on the row object which can be displayed separately.

Return value

string|\Drupal\Core\StringTranslation\TranslatableMarkup|false The formatted log message or FALSE if the message or variables properties are not set.

3 calls to DbLogController::formatMessage()
DbLogController::eventDetails in core/modules/dblog/src/Controller/DbLogController.php
Displays details about a specific database log message.
DbLogController::overview in core/modules/dblog/src/Controller/DbLogController.php
Displays a listing of database log messages.
DbLogController::topLogMessages in core/modules/dblog/src/Controller/DbLogController.php
Shows the most frequent log messages of a given event type.

File

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

Class

DbLogController
Returns responses for dblog routes.

Namespace

Drupal\dblog\Controller

Code

public function formatMessage($row) {
  // Check for required properties.
  if (isset($row->message, $row->variables)) {
    $variables = @unserialize($row->variables);
    // Messages without variables or user specified text.
    if ($variables === NULL) {
      $message = Xss::filterAdmin($row->message);
    }
    elseif (!is_array($variables)) {
      $message = $this->t('Log data is corrupted and cannot be unserialized: @message', [
        '@message' => Xss::filterAdmin($row->message),
      ]);
    }
    else {
      // Ensure backtrace strings are properly formatted.
      if (isset($variables['@backtrace_string'])) {
        $variables['@backtrace_string'] = new FormattableMarkup('<pre class="backtrace">@backtrace_string</pre>', $variables);
        // Save a reference so the backtrace can be displayed separately.
        if (!str_contains($row->message, '@backtrace_string')) {
          $row->backtrace = $variables['@backtrace_string'];
        }
      }
      $message = $this->t(Xss::filterAdmin($row->message), $variables);
    }
  }
  else {
    $message = FALSE;
  }
  return $message;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.