diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index 1b8a1b7d558..d6b95e8fc5a 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -1276,7 +1276,6 @@ function comment_get_display_page($cid, $instance) { * @param \Drupal\comment\CommentInterface $comment */ function comment_preview(CommentInterface $comment, array &$form_state) { - global $user; $preview_build = array(); $entity = entity_load($comment->entity_type->value, $comment->entity_id->value); @@ -1285,8 +1284,8 @@ function comment_preview(CommentInterface $comment, array &$form_state) { if (!empty($comment->name->value)) { $account = user_load_by_name($comment->name->value); } - elseif ($user->isAuthenticated() && empty($comment->is_anonymous)) { - $account = $user; + elseif (\Drupal::currentUser()->isAuthenticated() && empty($comment->is_anonymous)) { + $account = \Drupal::currentUser(); } if (!empty($account) && $account->isAuthenticated()) { @@ -1514,13 +1513,12 @@ function template_preprocess_comment(&$variables) { function theme_comment_post_forbidden($variables) { $entity = $variables['commented_entity']; $field_name = $variables['field_name']; - global $user; // Since this is expensive to compute, we cache it so that a page with many // comments only has to query the database once for all the links. $authenticated_post_comments = &drupal_static(__FUNCTION__, NULL); - if ($user->isAnonymous()) { + if (\Drupal::currentUser()->isAnonymous()) { if (!isset($authenticated_post_comments)) { // We only output a link if we are certain that users will get permission // to post comments by logging in. diff --git a/core/modules/comment/lib/Drupal/comment/Entity/Comment.php b/core/modules/comment/lib/Drupal/comment/Entity/Comment.php index 13008f8b5ff..1b29d84b116 100644 --- a/core/modules/comment/lib/Drupal/comment/Entity/Comment.php +++ b/core/modules/comment/lib/Drupal/comment/Entity/Comment.php @@ -218,10 +218,8 @@ class Comment extends ContentEntityBase implements CommentInterface { public function preSave(EntityStorageControllerInterface $storage_controller) { parent::preSave($storage_controller); - $user = \Drupal::currentUser(); - if (!isset($this->status->value)) { - $this->status->value = $user->hasPermission('skip comment approval') ? COMMENT_PUBLISHED : COMMENT_NOT_PUBLISHED; + $this->status->value = \Drupal::currentUser()->hasPermission('skip comment approval') ? COMMENT_PUBLISHED : COMMENT_NOT_PUBLISHED; } if ($this->isNew()) { // Add the comment to database. This next section builds the thread field. @@ -290,8 +288,8 @@ class Comment extends ContentEntityBase implements CommentInterface { } // We test the value with '===' because we need to modify anonymous // users as well. - if ($this->uid->target_id === $user->id() && $user->isAuthenticated()) { - $this->name->value = $user->getUsername(); + if ($this->uid->target_id === \Drupal::currentUser()->id() && \Drupal::currentUser()->isAuthenticated()) { + $this->name->value = \Drupal::currentUser()->getUsername(); } // Add the values which aren't passed into the function. $this->thread->value = $thread; diff --git a/core/modules/comment/lib/Drupal/comment/Plugin/views/field/NodeNewComments.php b/core/modules/comment/lib/Drupal/comment/Plugin/views/field/NodeNewComments.php index edaaa006fc3..3ce1d29ed00 100644 --- a/core/modules/comment/lib/Drupal/comment/Plugin/views/field/NodeNewComments.php +++ b/core/modules/comment/lib/Drupal/comment/Plugin/views/field/NodeNewComments.php @@ -100,7 +100,7 @@ class NodeNewComments extends Numeric { } public function preRender(&$values) { - global $user; + $user = \Drupal::currentUser(); if ($user->isAnonymous() || empty($values)) { return; } diff --git a/core/modules/content_translation/lib/Drupal/content_translation/ContentTranslationController.php b/core/modules/content_translation/lib/Drupal/content_translation/ContentTranslationController.php index f5c5203bd70..6d14c1b7994 100644 --- a/core/modules/content_translation/lib/Drupal/content_translation/ContentTranslationController.php +++ b/core/modules/content_translation/lib/Drupal/content_translation/ContentTranslationController.php @@ -237,7 +237,7 @@ class ContentTranslationController implements ContentTranslationControllerInterf // Default to the anonymous user. $name = ''; if ($new_translation) { - $name = $GLOBALS['user']->getUsername(); + $name = \Drupal::currentUser()->getUsername(); } elseif ($entity->translation[$form_langcode]['uid']) { $name = user_load($entity->translation[$form_langcode]['uid'])->getUsername();