Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save amcgowanca/28b296dd76622835b8dcc7f546c04f93 to your computer and use it in GitHub Desktop.
Save amcgowanca/28b296dd76622835b8dcc7f546c04f93 to your computer and use it in GitHub Desktop.
Drupal 8: Moves anonymous user creation from User::getAnonymousUser() to storage handler.
diff --git a/core/modules/user/src/Entity/User.php b/core/modules/user/src/Entity/User.php
index b8675866..6586cdb8 100644
--- a/core/modules/user/src/Entity/User.php
+++ b/core/modules/user/src/Entity/User.php
@@ -416,20 +416,9 @@ public function checkExistingPassword(UserInterface $account_unchanged) {
*/
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());
+ $entity_type_manager = \Drupal::entityTypeManager();
+ static::$anonymousUser = $entity_type_manager->getStorage('user')
+ ->getAnonymousUser();
}
return clone static::$anonymousUser;
}
diff --git a/core/modules/user/src/UserStorage.php b/core/modules/user/src/UserStorage.php
index 7ca4f243..a5366b91 100644
--- a/core/modules/user/src/UserStorage.php
+++ b/core/modules/user/src/UserStorage.php
@@ -73,4 +73,18 @@ public function deleteRoleReferences(array $rids) {
$this->resetCache();
}
+ /**
+ * {@inheritdoc}
+ */
+ public function getAnonymousUser() {
+ $class = $this->entityClass;
+ return 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]
+ ], $this->entityTypeId);
+ }
+
}
diff --git a/core/modules/user/src/UserStorageInterface.php b/core/modules/user/src/UserStorageInterface.php
index 4cfd26b1..ea69c169 100644
--- a/core/modules/user/src/UserStorageInterface.php
+++ b/core/modules/user/src/UserStorageInterface.php
@@ -36,4 +36,12 @@ public function updateLastAccessTimestamp(AccountInterface $account, $timestamp)
*/
public function deleteRoleReferences(array $rids);
+ /**
+ * Returns an anonymous user entity.
+ *
+ * @return \Drupal\user\UserInterface
+ * An anonymous user entity.
+ */
+ public function getAnonymousUser();
+
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment