diff --git a/core/lib/Drupal.php b/core/lib/Drupal.php index a4ef20b26ebf847ac3aa6b0cc486a388fec9db6d..416d80238b8bee75df1f019a7072209cb819cce7 100644 --- a/core/lib/Drupal.php +++ b/core/lib/Drupal.php @@ -252,11 +252,27 @@ public static function currentUser() { * * @return \Drupal\Core\Entity\EntityManagerInterface * The entity manager service. + * + * @deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. + * Use \Drupal::entityTypeManager() instead in most cases. If the needed + * method is not on \Drupal\Core\Entity\EntityTypeManagerInterface, see the + * deprecated \Drupal\Core\Entity\EntityManager to find the + * correct interface or service. */ public static function entityManager() { return static::getContainer()->get('entity.manager'); } + /** + * Retrieves the entity type manager. + * + * @return \Drupal\Core\Entity\EntityTypeManagerInterface + * The entity type manager. + */ + public static function entityTypeManager() { + return static::getContainer()->get('entity_type.manager'); + } + /** * Returns the current primary database. * diff --git a/core/lib/Drupal/Core/Controller/ControllerBase.php b/core/lib/Drupal/Core/Controller/ControllerBase.php index ce0354ff2cab5b996ce2b7f49a3aa4c584456bf7..544e247fe7584c7863e767970aee6e2703f28bcb 100644 --- a/core/lib/Drupal/Core/Controller/ControllerBase.php +++ b/core/lib/Drupal/Core/Controller/ControllerBase.php @@ -49,6 +49,13 @@ abstract class ControllerBase implements ContainerInjectionInterface { */ protected $entityManager; + /** + * The entity type manager. + * + * @var \Drupal\Core\Entity\EntityTypeManagerInterface + */ + protected $entityTypeManager; + /** * The entity form builder. * @@ -117,6 +124,10 @@ public static function create(ContainerInterface $container) { * * @return \Drupal\Core\Entity\EntityManagerInterface * The entity manager service. + * + * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0. + * Most of the time static::entityTypeManager() is supposed to be used + * instead. */ protected function entityManager() { if (!$this->entityManager) { @@ -125,6 +136,19 @@ protected function entityManager() { return $this->entityManager; } + /** + * Retrieves the entity type manager. + * + * @return \Drupal\Core\Entity\EntityTypeManagerInterface + * The entity type manager. + */ + protected function entityTypeManager() { + if (!isset($this->entityTypeManager)) { + $this->entityTypeManager = $this->container()->get('entity_type.manager'); + } + return $this->entityTypeManager; + } + /** * Retrieves the entity form builder. * diff --git a/core/lib/Drupal/Core/Entity/Entity.php b/core/lib/Drupal/Core/Entity/Entity.php index 2a4b199ee1c775304ba57c7a69859f97f11c8d08..3ea549dbb074b2a6de93db7183ffc1f9aedb936a 100644 --- a/core/lib/Drupal/Core/Entity/Entity.php +++ b/core/lib/Drupal/Core/Entity/Entity.php @@ -72,11 +72,26 @@ public function __construct(array $values, $entity_type) { * Gets the entity manager. * * @return \Drupal\Core\Entity\EntityManagerInterface + * + * @deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. + * Use \Drupal::entityTypeManager() instead in most cases. If the needed + * method is not on \Drupal\Core\Entity\EntityTypeManagerInterface, see the + * deprecated \Drupal\Core\Entity\EntityManager to find the + * correct interface or service. */ protected function entityManager() { return \Drupal::entityManager(); } + /** + * Gets the entity type manager. + * + * @return \Drupal\Core\Entity\EntityTypeManagerInterface + */ + protected function entityTypeManager() { + return \Drupal::entityTypeManager(); + } + /** * Gets the language manager. * diff --git a/core/tests/Drupal/Tests/Core/DrupalTest.php b/core/tests/Drupal/Tests/Core/DrupalTest.php index 315ce38c0d5d758c147c29bb3c10d4c4b3e27dd6..d22ed78b535ac1b6f2df95477e4aec599c084c06 100644 --- a/core/tests/Drupal/Tests/Core/DrupalTest.php +++ b/core/tests/Drupal/Tests/Core/DrupalTest.php @@ -86,6 +86,16 @@ public function testEntityManager() { $this->assertNotNull(\Drupal::entityManager()); } + /** + * Tests the entityTypeManager() method. + * + * @covers ::entityTypeManager + */ + public function testEntityTypeManager() { + $this->setMockContainerService('entity_type.manager'); + $this->assertNotNull(\Drupal::entityTypeManager()); + } + /** * Tests the database() method. *