Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. User.php

function User::getAnonymousUser

Returns an anonymous user entity.

Return value

\Drupal\user\UserInterface An anonymous user entity.

2 calls to User::getAnonymousUser()
Comment::getOwner in core/modules/comment/src/Entity/Comment.php
Returns the entity owner's user entity.
DbLogController::eventDetails in core/modules/dblog/src/Controller/DbLogController.php
Displays details about a specific database log message.

File

core/modules/user/src/Entity/User.php, line 444

Class

User
Defines the user entity class.

Namespace

Drupal\user\Entity

Code

public static function getAnonymousUser() {
    if (!isset(static::$anonymousUser)) {
        // @todo Use the entity factory once available, see
        //   https://www.drupal.org/node/1867228.
        $entity_type_manager = \Drupal::entityTypeManager();
        $entity_type = $entity_type_manager->getDefinition('user');
        $class = $entity_type->getClass();
        static::$anonymousUser = new $class([
            'uid' => [
                LanguageInterface::LANGCODE_DEFAULT => 0,
            ],
            'name' => [
                LanguageInterface::LANGCODE_DEFAULT => '',
            ],
            // Explicitly set the langcode to ensure that field definitions do not
            // need to be fetched to figure out a default.
'langcode' => [
                LanguageInterface::LANGCODE_DEFAULT => LanguageInterface::LANGCODE_NOT_SPECIFIED,
            ],
        ], $entity_type->id());
    }
    return clone static::$anonymousUser;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal