Commit 7abce708 authored by alexpott's avatar alexpott

Issue #2163203 by joelpittet, longwave, saki007ster: Remove calls to...

Issue #2163203 by joelpittet, longwave, saki007ster: Remove calls to deprecated global $user wherever possible (outside bootstrap and authentication).
parent d38ab8a3
......@@ -4571,7 +4571,7 @@ function drupal_render_cache_by_query($query, $function, $expire = Cache::PERMAN
* $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;
......@@ -4588,10 +4588,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) {
......
......@@ -2728,7 +2728,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);
......
......@@ -88,7 +88,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)) {
......@@ -1693,8 +1693,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 ' <span class="marker">' . t('new') . '</span>';
}
......
......@@ -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);
......
......@@ -28,7 +28,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';
......@@ -139,7 +139,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 = \Drupal::languageManager()->getCurrentLanguage();
$message = $this->entity;
......
......@@ -71,8 +71,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.
......@@ -96,7 +94,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
'#size' => $this->getSetting('size'),
'#placeholder' => $this->getSetting('placeholder'),
'#element_validate' => array(array($this, 'elementValidate')),
'#autocreate_uid' => ($entity instanceof EntityOwnerInterface) ? $entity->getOwnerId() : $user->id(),
'#autocreate_uid' => ($entity instanceof EntityOwnerInterface) ? $entity->getOwnerId() : \Drupal::currentUser()->id(),
);
return array('target_id' => $element);
......
......@@ -120,7 +120,7 @@ public function testSort() {
// Test as a non-admin.
$normal_user = $this->drupalCreateUser(array('access content'));
$GLOBALS['user'] = $normal_user;
$this->container->set('current_user', $normal_user);
$handler = $this->container->get('plugin.manager.entity_reference.selection')->getSelectionHandler($instance);
......
......@@ -466,7 +466,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;
......@@ -639,9 +639,7 @@ function filter_form_access_denied($element) {
* - id: Filter ID.
*/
function _filter_tips($format_id, $long = FALSE) {
global $user;
$formats = filter_formats($user);
$formats = filter_formats(\Drupal::currentUser());
$tips = array();
......
......@@ -58,7 +58,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',
......
......@@ -973,8 +973,7 @@ protected function beforePrepareEnvironment() {
* @see TestBase::beforePrepareEnvironment()
*/
private function prepareEnvironment() {
global $user;
$user = \Drupal::currentUser();
// Allow (base) test classes to backup global state information.
$this->beforePrepareEnvironment();
......@@ -1013,9 +1012,6 @@ private function prepareEnvironment() {
// Ensure that the current session is not changed by the new environment.
require_once DRUPAL_ROOT . '/' . settings()->get('session_inc', 'core/includes/session.inc');
drupal_save_session(FALSE);
// Run all tests as a anonymous user by default, web tests will replace that
// during the test set up.
$user = drupal_anonymous_user();
// Save and clean the shutdown callbacks array because it is static cached
// and will be changed by the test run. Otherwise it will contain callbacks
......@@ -1070,7 +1066,10 @@ private function prepareEnvironment() {
$request = Request::create('/');
$this->container->set('request', $request);
$this->container->set('current_user', $GLOBALS['user']);
// Run all tests as a anonymous user by default, web tests will replace that
// during the test set up.
$this->container->set('current_user', drupal_anonymous_user());
\Drupal::setContainer($this->container);
......@@ -1122,9 +1121,9 @@ protected function rebuildContainer($environment = 'testing') {
// DrupalKernel replaces the container in \Drupal::getContainer() with a
// different object, so we need to replace the instance on this test class.
$this->container = \Drupal::getContainer();
// The global $user is set in TestBase::prepareEnvironment().
// The current user is set in TestBase::prepareEnvironment().
$this->container->set('request', $request);
$this->container->set('current_user', $GLOBALS['user']);
$this->container->set('current_user', \Drupal::currentUser());
}
/**
......@@ -1145,8 +1144,6 @@ protected function tearDown() {
* @see TestBase::prepareEnvironment()
*/
private function restoreEnvironment() {
global $user;
// Reset all static variables.
// Unsetting static variables will potentially invoke destruct methods,
// which might call into functions that prime statics and caches again.
......@@ -1230,7 +1227,7 @@ private function restoreEnvironment() {
$callbacks = $this->originalShutdownCallbacks;
// Restore original user session.
$user = $this->originalUser;
$this->container->set('current_user', $this->originalUser);
drupal_save_session(TRUE);
}
......
......@@ -278,7 +278,7 @@ protected function drupalCreateNode(array $settings = array()) {
$settings['uid'] = $this->loggedInUser->id();
}
else {
$user = \Drupal::currentUser() ?: $GLOBALS['user'];
$user = \Drupal::currentUser() ?: drupal_anonymous_user();
$settings['uid'] = $user->id();
}
}
......@@ -638,7 +638,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()),
......
......@@ -349,7 +349,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 {
......
......@@ -122,7 +122,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();
}
}
......
......@@ -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);
}
/**
......
......@@ -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();
}
}
......@@ -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';
}
......
......@@ -41,7 +41,7 @@ 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)));
return new RedirectResponse(url('user/' . \Drupal::currentUser()->id(), array('absolute' => TRUE)));
}
$form['#attached']['library'][] = array('core', 'jquery.cookie');
......
......@@ -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.
......
......@@ -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();
......
......@@ -963,8 +963,6 @@ function user_pass_rehash($password, $timestamp, $login) {
* @see _user_cancel()
*/
function user_cancel($edit, $uid, $method) {
global $user;
$account = user_load($uid);
if (!$account) {
......@@ -997,7 +995,7 @@ function user_cancel($edit, $uid, $method) {
);
// After cancelling account, ensure that user is logged out.
if ($account->id() == $user->id()) {
if ($account->id() == \Drupal::currentUser()->id()) {
// Batch API stores data in the session, so use the finished operation to
// manipulate the current user's session id.
$batch['finished'] = '_user_cancel_session_regenerate';
......@@ -1574,8 +1572,6 @@ function user_form_process_password_confirm($element) {
);
if (\Drupal::config('user.settings')->get('password_strength')) {
global $user;
$password_settings['showStrengthIndicator'] = TRUE;
$password_settings += array(
'strengthTitle' => t('Password strength:'),
......@@ -1590,7 +1586,7 @@ function user_form_process_password_confirm($element) {
'fair' => t('Fair'),
'good' => t('Good'),
'strong' => t('Strong'),
'username' => $user->getUsername(),
'username' => \Drupal::currentUser()->getUsername(),
);
}
......@@ -1676,7 +1672,7 @@ function user_file_download_access($field, EntityInterface $entity, File $file)
* Implements hook_toolbar().
*/
function user_toolbar() {
global $user;
$user = \Drupal::currentUser();
// Add logout & user account links or login link.
if ($user->isAuthenticated()) {
......@@ -1750,7 +1746,7 @@ function user_toolbar() {
* Logs the current user out.
*/
function user_logout() {
global $user;
$user = \Drupal::currentUser();
watchdog('user', 'Session closed for %name.', array('%name' => $user->getUsername()));
......
......@@ -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);
}
......
......@@ -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());
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment