You are here

public static function User::getAnonymousUser in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/user/src/Entity/User.php \Drupal\user\Entity\User::getAnonymousUser()
  2. 9 core/modules/user/src/Entity/User.php \Drupal\user\Entity\User::getAnonymousUser()

Returns an anonymous user entity.

Return value

\Drupal\user\UserInterface An anonymous user entity.

10 calls to User::getAnonymousUser()
AccessTest::testFileCacheability in core/modules/file/tests/src/Kernel/AccessTest.php
Tests cacheability metadata.
Comment::getOwner in core/modules/comment/src/Entity/Comment.php
DbLogController::eventDetails in core/modules/dblog/src/Controller/DbLogController.php
Displays details about a specific database log message.
EntityAccessControlHandlerTest::testUserLabelAccess in core/tests/Drupal/KernelTests/Core/Entity/EntityAccessControlHandlerTest.php
Ensures user labels are accessible for everyone.
FileFieldWidgetTest::testTemporaryFileRemovalExploitAnonymous in core/modules/file/tests/src/Functional/FileFieldWidgetTest.php
Tests exploiting the temporary file removal for anonymous users using fid.

... See full list

File

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

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;
}