User::getAnonymousUser

public static User::getAnonymousUser()

Returns an anonymous user entity.

Return value

\Drupal\user\UserInterface An anonymous user entity.

File

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

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_manager = \Drupal::entityManager();
    $entity_type = $entity_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;
}
doc_Drupal
2016-10-29 09:52:05
Comments
Leave a Comment

Please login to continue.