Download & Extend

drupal_anonymous_user() returns an object of type User

Project: 
Drupal core
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:

<?php
$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:

<?php
$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
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
nobody click here