Change record status: 
Project: 
Introduced in branch: 
8.x
Description: 

The current $user object is now a \Drupal\Core\Session\UserSession class that implements the \Drupal\Core\Session\AccountInterface interface as do user entities. The account interface can be used as a type hint when a function or method accepts either the global user object or a user entity.

Instead of accessing properties like uid and roles, the corresponding methods must now be used. Some of the new methods are listed below, check the linked documentation for details and the full list.

  • AccountInterface::id() - Returns the user id or 0 for the anonymous account
  • AccountInterface::isAnonymous() - Returns TRUE if the account is anonymous
  • AccountInterface::isAuthenticated() - Returns TRUE if the account is authenticated
  • AccountInterface::getRoles() - Returns a list of account's roles
  • AccountInterface::hasPermission() - Returns TRUE if the account has a certain permission
  • AccountInterface::getUsername() - Returns the username of this account
  • AccountInterface::getDisplayName() - Returns formatted username, replaces user_format_name()
  • AccountInterface::getEmail() - Returns the email address of this account
  • AccountInterface::getTimeZone() - Returns the timezone of this account
  • AccountInterface::getLastAccessedTime() - The timestamp when the account last accessed the site
  • AccountInterface::getPreferredLangcode() - Returns the preferred language code of the account
  • AccountInterface::getPreferredAdminLangcode() - Returns the preferred administrative language code of the account

7.x

function mymodule_function() {
  global $user;
  if ($user->uid) {
    // User is logged in.
  }
}

function mymodule_needs_user($user) {

}

8.x


use Drupal\Core\Session\AccountInterface;

function mymodule_function() {
  $account = \Drupal::currentUser();
  if ($account->isAuthenticated()) {
    // User is logged in.
  }
}

function mymodule_needs_user(AccountInterface $user) {

}
Impacts: 
Module developers
Updates Done (doc team, etc.)
Online documentation: 
Not done
Theming guide: 
Not done
Module developer documentation: 
Not done
Examples project: 
Not done
Coder Review: 
Not done
Coder Upgrade: 
Not done
Other: 
Other updates done