From a7f6cfb4408832b17150668370ce1d1292c66401 Mon Sep 17 00:00:00 2001 From: Moshe Weitzman Date: Fri, 23 Sep 2022 13:04:37 -0400 Subject: [PATCH] Fix #5202,#5198. watchdog:show silently ignores username column. --- src/Drupal/Commands/core/WatchdogCommands.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Drupal/Commands/core/WatchdogCommands.php b/src/Drupal/Commands/core/WatchdogCommands.php index 69781fb5d0..5eefa24901 100644 --- a/src/Drupal/Commands/core/WatchdogCommands.php +++ b/src/Drupal/Commands/core/WatchdogCommands.php @@ -6,6 +6,7 @@ use Consolidation\OutputFormatters\StructuredData\RowsOfFields; use Drupal\Core\Database\Database; use Drupal\Core\Logger\RfcLogLevel; +use Drupal\Core\Session\AnonymousUserSession; use Drupal\user\Entity\User; use Drush\Commands\DrushCommands; use Drupal\Component\Utility\Unicode; @@ -46,6 +47,7 @@ class WatchdogCommands extends DrushCommands * hostname: Hostname * date: Date * username: Username + * uid: Uid * @default-fields wid,date,type,severity,message * @filter-default-field message * @return RowsOfFields @@ -263,7 +265,7 @@ public function showOne($id, $options = ['format' => 'yaml']): PropertyList if (!$result) { throw new \Exception(dt('Watchdog message #!wid not found.', ['!wid' => $id])); } - return new PropertyList($this->formatResult($result)); + return new PropertyList($this->formatResult($result, true)); } /** @@ -342,6 +344,13 @@ protected function formatResult($result, $extended = false) $result->date = date('d/M H:i', $result->timestamp); unset($result->timestamp); + // Username. + $result->username = (new AnonymousUserSession())->getAccountName() ?: dt('Anonymous'); + $account = User::load($result->uid); + if ($account && !$account->isAnonymous()) { + $result->username = $account->getAccountName(); + } + // Message. $variables = $result->variables; if (is_string($variables)) { @@ -362,9 +371,6 @@ protected function formatResult($result, $extended = false) if (empty($result->referer)) { unset($result->referer); } - // Username. - $result->username = ($account = User::load($result->uid)) ? $account->name : dt('Anonymous'); - unset($result->uid); $message_length = PHP_INT_MAX; } $result->message = Unicode::truncate(strip_tags(Html::decodeEntities($result->message)), $message_length, false, false);