Created
March 6, 2019 11:53
-
-
Save amcgowanca/28b296dd76622835b8dcc7f546c04f93 to your computer and use it in GitHub Desktop.
Drupal 8: Moves anonymous user creation from User::getAnonymousUser() to storage handler.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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