From 8485135f1a1b057f1e69f86b17fb0854bc6e2933 Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Thu, 12 Nov 2015 15:35:16 +0000 Subject: [PATCH] Issue #2611086 by dawehner, xjm: Expose entity type manager on controller base / Entity / Drupal --- core/lib/Drupal.php | 16 +++++++++++++ .../Drupal/Core/Controller/ControllerBase.php | 24 +++++++++++++++++++ core/lib/Drupal/Core/Entity/Entity.php | 15 ++++++++++++ core/tests/Drupal/Tests/Core/DrupalTest.php | 10 ++++++++ 4 files changed, 65 insertions(+) diff --git a/core/lib/Drupal.php b/core/lib/Drupal.php index a4ef20b26ebf..416d80238b8b 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 ce0354ff2cab..544e247fe758 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 2a4b199ee1c7..3ea549dbb074 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 315ce38c0d5d..d22ed78b535a 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. * -- GitLab