diff --git a/core/includes/common.inc b/core/includes/common.inc
index aff7665..e926ac5 100644
--- a/core/includes/common.inc
+++ b/core/includes/common.inc
@@ -4518,7 +4518,7 @@ function drupal_render_cache_by_query($query, $function, $expire = CacheBackendI
* $granularity was passed in, more parts are added.
*/
function drupal_render_cid_parts($granularity = NULL) {
- global $theme, $base_root, $user;
+ global $theme, $base_root;
$cid_parts[] = $theme;
// If Locale is enabled but we have only one language we do not need it as cid
@@ -4534,10 +4534,10 @@ function drupal_render_cid_parts($granularity = NULL) {
// resource drag for sites with many users, so when a module is being
// equivocal, we favor the less expensive 'PER_ROLE' pattern.
if ($granularity & DRUPAL_CACHE_PER_ROLE) {
- $cid_parts[] = 'r.' . implode(',', $user->getRoles());
+ $cid_parts[] = 'r.' . implode(',', \Drupal::currentUser()->getRoles());
}
elseif ($granularity & DRUPAL_CACHE_PER_USER) {
- $cid_parts[] = 'u.' . $user->id();
+ $cid_parts[] = 'u.' . \Drupal::currentUser()->id();
}
if ($granularity & DRUPAL_CACHE_PER_PAGE) {
diff --git a/core/includes/install.core.inc b/core/includes/install.core.inc
index b44c59a..c2cbbc7 100644
--- a/core/includes/install.core.inc
+++ b/core/includes/install.core.inc
@@ -2738,7 +2738,7 @@ function install_configure_form_submit($form, &$form_state) {
$account->pass = $form_state['values']['account']['pass'];
$account->name = $form_state['values']['account']['name'];
$account->save();
- // Load global $user and perform final login tasks.
+ // Load current user and perform final login tasks.
$account = user_load(1);
user_login_finalize($account);
diff --git a/core/includes/language.inc b/core/includes/language.inc
index 618e3a7..aa7af69 100644
--- a/core/includes/language.inc
+++ b/core/includes/language.inc
@@ -459,8 +459,6 @@ function language_negotiation_method_invoke($method_id, $method = NULL, $request
$results = &drupal_static(__FUNCTION__);
if (!isset($results[$method_id])) {
- global $user;
-
$languages = language_list();
if (!isset($method)) {
@@ -482,7 +480,7 @@ function language_negotiation_method_invoke($method_id, $method = NULL, $request
}
// If the language negotiation method has no cache preference or this is
// satisfied we can execute the callback.
- $cache = !isset($method['cache']) || $user->isAuthenticated() || $method['cache'] == $cache_enabled;
+ $cache = !isset($method['cache']) || \Drupal::currentUser()->isAuthenticated() || $method['cache'] == $cache_enabled;
$callback = isset($method['callbacks']['negotiation']) ? $method['callbacks']['negotiation'] : FALSE;
$langcode = $cache && function_exists($callback) ? $callback($languages, $request) : FALSE;
$results[$method_id] = isset($languages[$langcode]) ? $languages[$langcode] : FALSE;
diff --git a/core/includes/theme.inc b/core/includes/theme.inc
index 3db84a9..891b093 100644
--- a/core/includes/theme.inc
+++ b/core/includes/theme.inc
@@ -84,7 +84,7 @@ function drupal_theme_access($theme) {
* Initializes the theme system by loading the theme.
*/
function drupal_theme_initialize() {
- global $theme, $user, $theme_key;
+ global $theme, $theme_key;
// If $theme is already set, assume the others are set, too, and do nothing
if (isset($theme)) {
@@ -1721,8 +1721,7 @@ function theme_table($variables) {
*/
function theme_mark($variables) {
$type = $variables['status'];
- global $user;
- if ($user->isAuthenticated()) {
+ if (\Drupal::currentUser()->isAuthenticated()) {
if ($type == MARK_NEW) {
return ' ' . t('new') . '';
}
diff --git a/core/lib/Drupal/Core/Entity/EntityAccessController.php b/core/lib/Drupal/Core/Entity/EntityAccessController.php
index f18cd2c..9285422 100644
--- a/core/lib/Drupal/Core/Entity/EntityAccessController.php
+++ b/core/lib/Drupal/Core/Entity/EntityAccessController.php
@@ -276,7 +276,7 @@ protected function checkCreateAccess(AccountInterface $account, array $context,
*/
protected function prepareUser(AccountInterface $account = NULL) {
if (!$account) {
- $account = $GLOBALS['user'];
+ $account = \Drupal::currentUser();
}
return $account;
}
diff --git a/core/lib/Drupal/Core/EventSubscriber/AuthenticationSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/AuthenticationSubscriber.php
index 5bdf61c..a2aa7d8 100644
--- a/core/lib/Drupal/Core/EventSubscriber/AuthenticationSubscriber.php
+++ b/core/lib/Drupal/Core/EventSubscriber/AuthenticationSubscriber.php
@@ -78,13 +78,13 @@ public function onException(GetResponseForExceptionEvent $event) {
* {@inheritdoc}
*
* The priority for request must be higher than the highest event subscriber
- * accessing the global $user.
+ * accessing the current user.
* The priority for the response must be as low as possible allowing e.g the
* Cookie provider to send all relevant session data to the user.
*/
public static function getSubscribedEvents() {
// Priority must be higher than LanguageRequestSubscriber as LanguageManager
- // access global $user in case language module enabled.
+ // access current user in case language module enabled.
$events[KernelEvents::REQUEST][] = array('onKernelRequestAuthenticate', 300);
$events[KernelEvents::RESPONSE][] = array('onRespond', 0);
$events[KernelEvents::EXCEPTION][] = array('onException', 0);
diff --git a/core/lib/Drupal/Core/Form/FormBuilder.php b/core/lib/Drupal/Core/Form/FormBuilder.php
index 355e560..960020c 100644
--- a/core/lib/Drupal/Core/Form/FormBuilder.php
+++ b/core/lib/Drupal/Core/Form/FormBuilder.php
@@ -1815,8 +1815,7 @@ protected function currentUser() {
$this->currentUser = \Drupal::currentUser();
}
else {
- global $user;
- $this->currentUser = $user;
+ $this->currentUser = \Drupal::currentUser();
}
}
return $this->currentUser;
diff --git a/core/modules/contact/lib/Drupal/contact/MessageFormController.php b/core/modules/contact/lib/Drupal/contact/MessageFormController.php
index ca4a607..2c4f7fd 100644
--- a/core/modules/contact/lib/Drupal/contact/MessageFormController.php
+++ b/core/modules/contact/lib/Drupal/contact/MessageFormController.php
@@ -20,7 +20,7 @@ class MessageFormController extends ContentEntityFormController {
* Overrides Drupal\Core\Entity\EntityFormController::form().
*/
public function form(array $form, array &$form_state) {
- global $user;
+ $user = \Drupal::currentUser();
$message = $this->entity;
$form = parent::form($form, $form_state, $message);
$form['#attributes']['class'][] = 'contact-form';
@@ -131,7 +131,7 @@ public function preview(array $form, array &$form_state) {
* Overrides Drupal\Core\Entity\EntityFormController::save().
*/
public function save(array $form, array &$form_state) {
- global $user;
+ $user = \Drupal::currentUser();
$language_interface = language(Language::TYPE_INTERFACE);
$message = $this->entity;
diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Field/FieldWidget/AutocompleteWidgetBase.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Field/FieldWidget/AutocompleteWidgetBase.php
index 4621c5c..1842c7c 100644
--- a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Field/FieldWidget/AutocompleteWidgetBase.php
+++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Field/FieldWidget/AutocompleteWidgetBase.php
@@ -69,8 +69,6 @@ public function settingsSummary() {
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, array &$form_state) {
- global $user;
-
$entity = $items->getEntity();
// Prepare the autocomplete route parameters.
@@ -95,7 +93,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
'#placeholder' => $this->getSetting('placeholder'),
'#element_validate' => array(array($this, 'elementValidate')),
// @todo: Use wrapper to get the user if exists or needed.
- '#autocreate_uid' => isset($entity->uid) ? $entity->uid : $user->id(),
+ '#autocreate_uid' => isset($entity->uid) ? $entity->uid : \Drupal::currentUser()->id(),
);
return array('target_id' => $element);
diff --git a/core/modules/filter/filter.module b/core/modules/filter/filter.module
index 4962044..151e7f3 100644
--- a/core/modules/filter/filter.module
+++ b/core/modules/filter/filter.module
@@ -674,7 +674,7 @@ function check_markup($text, $format_id = NULL, $langcode = '', $cache = FALSE,
* The expanded element.
*/
function filter_process_format($element) {
- global $user;
+ $user = \Drupal::currentUser();
// Ensure that children appear as subkeys of this element.
$element['#tree'] = TRUE;
@@ -868,9 +868,7 @@ function theme_text_format_wrapper($variables) {
* - id: Filter ID.
*/
function _filter_tips($format_id, $long = FALSE) {
- global $user;
-
- $formats = filter_formats($user);
+ $formats = filter_formats(\Drupal::currentUser());
$tips = array();
diff --git a/core/modules/serialization/lib/Drupal/serialization/Tests/EntitySerializationTest.php b/core/modules/serialization/lib/Drupal/serialization/Tests/EntitySerializationTest.php
index 262d0a6..874ee90 100644
--- a/core/modules/serialization/lib/Drupal/serialization/Tests/EntitySerializationTest.php
+++ b/core/modules/serialization/lib/Drupal/serialization/Tests/EntitySerializationTest.php
@@ -54,7 +54,7 @@ protected function setUp() {
// Create a test entity to serialize.
$this->values = array(
'name' => $this->randomName(),
- 'user_id' => $GLOBALS['user']->id(),
+ 'user_id' => \Drupal::currentUser()->id(),
'field_test_text' => array(
'value' => $this->randomName(),
'format' => 'full_html',
diff --git a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
index 7541fb2..518d563 100644
--- a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
+++ b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
@@ -605,7 +605,7 @@ protected function checkPermissions(array $permissions, $reset = FALSE) {
* If a user is already logged in, then the current user is logged out before
* logging in the specified user.
*
- * Please note that neither the global $user nor the passed-in user object is
+ * Please note that neither the current user nor the passed-in user object is
* populated with data of the logged in user. If you need full access to the
* user object after logging in, it must be updated manually. If you also need
* access to the plain-text password of the user (set by drupalCreateUser()),
diff --git a/core/modules/user/lib/Drupal/user/AccountFormController.php b/core/modules/user/lib/Drupal/user/AccountFormController.php
index 4983b0f..fefafb0 100644
--- a/core/modules/user/lib/Drupal/user/AccountFormController.php
+++ b/core/modules/user/lib/Drupal/user/AccountFormController.php
@@ -328,7 +328,7 @@ public function validate(array $form, array &$form_state) {
if ($mail_taken) {
// Format error message dependent on whether the user is logged in or not.
- if ($GLOBALS['user']->isAuthenticated()) {
+ if (\Drupal::currentUser()->isAuthenticated()) {
$this->setFormError('mail', $form_state, $this->t('The e-mail address %email is already taken.', array('%email' => $mail)));
}
else {
diff --git a/core/modules/user/lib/Drupal/user/Entity/User.php b/core/modules/user/lib/Drupal/user/Entity/User.php
index 35af76a..16f6204 100644
--- a/core/modules/user/lib/Drupal/user/Entity/User.php
+++ b/core/modules/user/lib/Drupal/user/Entity/User.php
@@ -120,7 +120,7 @@ public function postSave(EntityStorageControllerInterface $storage_controller, $
// user and recreate the current one.
if ($this->pass->value != $this->original->pass->value) {
drupal_session_destroy_uid($this->id());
- if ($this->id() == $GLOBALS['user']->id()) {
+ if ($this->id() == \Drupal::currentUser()->id()) {
drupal_session_regenerate();
}
}
diff --git a/core/modules/user/lib/Drupal/user/EventSubscriber/MaintenanceModeSubscriber.php b/core/modules/user/lib/Drupal/user/EventSubscriber/MaintenanceModeSubscriber.php
index 3c23086..f90b4bd 100644
--- a/core/modules/user/lib/Drupal/user/EventSubscriber/MaintenanceModeSubscriber.php
+++ b/core/modules/user/lib/Drupal/user/EventSubscriber/MaintenanceModeSubscriber.php
@@ -29,7 +29,7 @@ public function onKernelRequestMaintenance(GetResponseEvent $event) {
$path = $request->attributes->get('_system_path');
if ($site_status == MENU_SITE_OFFLINE) {
// If the site is offline, log out unprivileged users.
- if ($GLOBALS['user']->isAuthenticated() && !user_access('access site in maintenance mode')) {
+ if (\Drupal::currentUser()->isAuthenticated() && !user_access('access site in maintenance mode')) {
user_logout();
// Redirect to homepage.
$event->setResponse(new RedirectResponse(url('', array('absolute' => TRUE))));
@@ -56,7 +56,7 @@ public function onKernelRequestMaintenance(GetResponseEvent $event) {
}
}
}
- if ($GLOBALS['user']->isAuthenticated()) {
+ if (\Drupal::currentUser()->isAuthenticated()) {
if ($path == 'user/login') {
// If user is logged in, redirect to 'user' instead of giving 403.
$event->setResponse(new RedirectResponse(url('user', array('absolute' => TRUE))));
@@ -64,7 +64,7 @@ public function onKernelRequestMaintenance(GetResponseEvent $event) {
}
if ($path == 'user/register') {
// Authenticated user should be redirected to user edit page.
- $event->setResponse(new RedirectResponse(url('user/' . $GLOBALS['user']->id() . '/edit', array('absolute' => TRUE))));
+ $event->setResponse(new RedirectResponse(url('user/' . \Drupal::currentUser()->id() . '/edit', array('absolute' => TRUE))));
return;
}
}
diff --git a/core/modules/user/lib/Drupal/user/Plugin/Action/CancelUser.php b/core/modules/user/lib/Drupal/user/Plugin/Action/CancelUser.php
index b404ba0..75fa5f5 100644
--- a/core/modules/user/lib/Drupal/user/Plugin/Action/CancelUser.php
+++ b/core/modules/user/lib/Drupal/user/Plugin/Action/CancelUser.php
@@ -60,7 +60,7 @@ public static function create(ContainerInterface $container, array $configuratio
* {@inheritdoc}
*/
public function executeMultiple(array $entities) {
- $this->tempStoreFactory->get('user_user_operations_cancel')->set($GLOBALS['user']->id(), $entities);
+ $this->tempStoreFactory->get('user_user_operations_cancel')->set(\Drupal::currentUser()->id(), $entities);
}
/**
diff --git a/core/modules/user/lib/Drupal/user/Plugin/views/argument_default/CurrentUser.php b/core/modules/user/lib/Drupal/user/Plugin/views/argument_default/CurrentUser.php
index e763bae..f1d987b 100644
--- a/core/modules/user/lib/Drupal/user/Plugin/views/argument_default/CurrentUser.php
+++ b/core/modules/user/lib/Drupal/user/Plugin/views/argument_default/CurrentUser.php
@@ -10,7 +10,7 @@
use Drupal\views\Plugin\views\argument_default\ArgumentDefaultPluginBase;
/**
- * Default argument plugin to extract the global $user
+ * Default argument plugin to extract the current user
*
* This plugin actually has no options so it odes not need to do a great deal.
*
@@ -22,8 +22,7 @@
class CurrentUser extends ArgumentDefaultPluginBase {
public function getArgument() {
- global $user;
- return $user->id();
+ return \Drupal::currentUser()->id();
}
}
diff --git a/core/modules/user/lib/Drupal/user/Plugin/views/argument_validator/User.php b/core/modules/user/lib/Drupal/user/Plugin/views/argument_validator/User.php
index d385236..d7c3701 100644
--- a/core/modules/user/lib/Drupal/user/Plugin/views/argument_validator/User.php
+++ b/core/modules/user/lib/Drupal/user/Plugin/views/argument_validator/User.php
@@ -109,21 +109,21 @@ public function validateArgument($argument) {
// However, is_integer() will always fail, since $argument is a string.
if (is_numeric($argument) && $argument == (int)$argument) {
if ($type == 'uid' || $type == 'either') {
- if ($argument == $GLOBALS['user']->id()) {
+ if ($argument == \Drupal::currentUser()->id()) {
// If you assign an object to a variable in PHP, the variable
// automatically acts as a reference, not a copy, so we use
// clone to ensure that we don't actually mess with the
- // real global $user object.
- $account = clone $GLOBALS['user'];
+ // real current user object.
+ $account = clone \Drupal::currentUser();
}
$condition = 'uid';
}
}
else {
if ($type == 'name' || $type == 'either') {
- $name = $GLOBALS['user']->getUserName() ?: \Drupal::config('user.settings')->get('anonymous');
+ $name = \Drupal::currentUser()->getUserName() ?: \Drupal::config('user.settings')->get('anonymous');
if ($argument == $name) {
- $account = clone $GLOBALS['user'];
+ $account = clone \Drupal::currentUser();
}
$condition = 'name';
}
diff --git a/core/modules/user/lib/Drupal/user/RegisterFormController.php b/core/modules/user/lib/Drupal/user/RegisterFormController.php
index 38612a2..5a2fcc2 100644
--- a/core/modules/user/lib/Drupal/user/RegisterFormController.php
+++ b/core/modules/user/lib/Drupal/user/RegisterFormController.php
@@ -18,7 +18,6 @@ class RegisterFormController extends AccountFormController {
* Overrides Drupal\Core\Entity\EntityFormController::form().
*/
public function form(array $form, array &$form_state) {
- global $user;
$account = $this->entity;
$admin = user_access('administer users');
@@ -32,8 +31,8 @@ public function form(array $form, array &$form_state) {
);
// If we aren't admin but already logged on, go to the user page instead.
- if (!$admin && $user->isAuthenticated()) {
- return new RedirectResponse(url('user/' . $user->id(), array('absolute' => TRUE)));
+ if (!$admin && \Drupal::currentUser()->isAuthenticated()) {
+ return new RedirectResponse(url('user/' . \Drupal::currentUser()->id(), array('absolute' => TRUE)));
}
$form['#attached']['library'][] = array('system', 'jquery.cookie');
diff --git a/core/modules/user/lib/Drupal/user/TempStoreFactory.php b/core/modules/user/lib/Drupal/user/TempStoreFactory.php
index 45f421e..28d4147 100644
--- a/core/modules/user/lib/Drupal/user/TempStoreFactory.php
+++ b/core/modules/user/lib/Drupal/user/TempStoreFactory.php
@@ -61,7 +61,7 @@ function get($collection, $owner = NULL) {
// Use the currently authenticated user ID or the active user ID unless
// the owner is overridden.
if (!isset($owner)) {
- $owner = $GLOBALS['user']->id() ?: session_id();
+ $owner = \Drupal::currentUser()->id() ?: session_id();
}
// Store the data for this collection in the database.
diff --git a/core/modules/user/lib/Drupal/user/Tests/UserTokenReplaceTest.php b/core/modules/user/lib/Drupal/user/Tests/UserTokenReplaceTest.php
index cc706ba..13172de 100644
--- a/core/modules/user/lib/Drupal/user/Tests/UserTokenReplaceTest.php
+++ b/core/modules/user/lib/Drupal/user/Tests/UserTokenReplaceTest.php
@@ -57,7 +57,7 @@ function testUserTokenReplacement() {
$this->drupalLogin($user2);
$account = user_load($user1->id());
- $global_account = user_load($GLOBALS['user']->id());
+ $global_account = user_load(\Drupal::currentUser()->id());
// Generate and test sanitized tokens.
$tests = array();
diff --git a/core/modules/user/user.install b/core/modules/user/user.install
index a194b00..950b89d 100644
--- a/core/modules/user/user.install
+++ b/core/modules/user/user.install
@@ -516,8 +516,6 @@ function user_update_8009(&$sandbox) {
* Create user picture field.
*/
function user_update_8011() {
- global $user;
-
// User pictures can only be migrated to the new user picture image field
// if Image module is installed.
if (!\Drupal::moduleHandler()->moduleExists('image')) {
@@ -548,7 +546,7 @@ function user_update_8011() {
'uri' => $destination,
))
->fields(array(
- 'uid' => $user->id(),
+ 'uid' => \Drupal::currentUser()->id(),
'status' => FILE_STATUS_PERMANENT,
'filename' => drupal_basename($destination),
'uuid' => $uuid->generate(),
diff --git a/core/modules/user/user.module b/core/modules/user/user.module
index b5604a1..4e100af 100644
--- a/core/modules/user/user.module
+++ b/core/modules/user/user.module
@@ -687,7 +687,7 @@ function theme_username($variables) {
*/
function user_is_anonymous() {
// Menu administrators can see items for anonymous when administering.
- return $GLOBALS['user']->isAnonymous() || !empty($GLOBALS['menu_admin']);
+ return \Drupal::currentUser()->isAnonymous() || !empty($GLOBALS['menu_admin']);
}
/**
@@ -699,7 +699,7 @@ function user_is_anonymous() {
* @deprecated Use \Drupal\Core\Session\UserSession::isAuthenticated().
*/
function user_is_logged_in() {
- return $GLOBALS['user']->isAuthenticated();
+ return \Drupal::currentUser()->isAuthenticated();
}
/**
@@ -874,7 +874,7 @@ function user_uid_only_optional_to_arg($arg) {
*/
function user_uid_optional_load($uid = NULL) {
if (!isset($uid)) {
- $uid = $GLOBALS['user']->id();
+ $uid = \Drupal::currentUser()->id();
}
return user_load($uid);
}
@@ -888,7 +888,7 @@ function user_uid_optional_to_arg($arg) {
// Give back the current user uid when called from eg. tracker, aka.
// with an empty arg. Also use the current user uid when called from
// the menu with a % for the current account link.
- return empty($arg) || $arg == '%' ? $GLOBALS['user']->id() : $arg;
+ return empty($arg) || $arg == '%' ? \Drupal::currentUser()->id() : $arg;
}
/**
@@ -898,7 +898,7 @@ function user_uid_optional_to_arg($arg) {
* authenticated users are expected to see "My account".
*/
function user_menu_title() {
- if ($GLOBALS['user']->isAnonymous()) {
+ if (\Drupal::currentUser()->isAnonymous()) {
switch (current_path()) {
case 'user' :
case 'user/login' :
diff --git a/core/modules/user/user.pages.inc b/core/modules/user/user.pages.inc
index 2977253..c4425b9 100644
--- a/core/modules/user/user.pages.inc
+++ b/core/modules/user/user.pages.inc
@@ -17,7 +17,7 @@
* @deprecated Use \Drupal\user\Form\UserForm::resetPass()
*/
function user_pass_reset($form, &$form_state, $uid, $timestamp, $hashed_pass, $action = NULL) {
- global $user;
+ $user = \Drupal::currentUser();
// When processing the one-time login link, we have to make sure that a user
// isn't already logged in.
diff --git a/core/modules/user/user.tokens.inc b/core/modules/user/user.tokens.inc
index 5412a38..e3c22f2 100644
--- a/core/modules/user/user.tokens.inc
+++ b/core/modules/user/user.tokens.inc
@@ -125,7 +125,7 @@ function user_tokens($type, $tokens, array $data = array(), array $options = arr
}
if ($type == 'current-user') {
- $account = user_load($GLOBALS['user']->id());
+ $account = user_load(\Drupal::currentUser()->id());
$replacements += $token_service->generate('user', $tokens, array('user' => $account), $options);
}
diff --git a/core/modules/user/user.views_execution.inc b/core/modules/user/user.views_execution.inc
index 622cdbf..c9a3f1d 100644
--- a/core/modules/user/user.views_execution.inc
+++ b/core/modules/user/user.views_execution.inc
@@ -13,6 +13,5 @@
* Allow replacement of current userid so we can cache these queries.
*/
function user_views_query_substitutions(ViewExecutable $view) {
- global $user;
- return array('***CURRENT_USER***' => $user->id());
+ return array('***CURRENT_USER***' => \Drupal::currentUser()->id());
}