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

Summary

Convert drupal_anonymous_user() to return an object of type User.

API Changes

  • drupal_anonymous_user() returns an object of type User.

Examples

When this function is used in core, the returned object is either passed off to another function or assigned to the global $user object. These examples are purely hypothetical.

Access properties of the returned object.

Drupal 7

Since the user object is of type stdClass, you can make up your own properties:

$account = drupal_anonymous_user();
$account->extra_local_info = 'Secret';
$html = theme('user_username', $account);

Drupal 8

The User class has a defined list of properties:

$account = drupal_anonymous_user();
  $defaults = array(
    'name' => '',
    'mail' => '',
    'access' => 0,
    'login' => 0,
    'status' => 1,
    'langcode' => LANGUAGE_NOT_SPECIFIED,
    'preferred_langcode' => LANGUAGE_NOT_SPECIFIED,
    'preferred_admin_langcode' => LANGUAGE_NOT_SPECIFIED,
    'init' => '',
  );
  foreach ($defaults as $property => $value) {
    if ($account->$property !== $value) {
      // Something went wrong.
      break;
    }
  }
Impacts: 
Module developers