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