diff --git a/core/lib/Drupal/Core/Entity/EntityFieldManagerInterface.php b/core/lib/Drupal/Core/Entity/EntityFieldManagerInterface.php index 5a5b43ffc981..327d9e309ff9 100644 --- a/core/lib/Drupal/Core/Entity/EntityFieldManagerInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityFieldManagerInterface.php @@ -108,10 +108,6 @@ interface EntityFieldManagerInterface { * * @param bool $use_caches * FALSE to not use any caches. - * - * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0. - * - * @todo Remove in https://www.drupal.org/node/2549143. */ public function useCaches($use_caches = FALSE); diff --git a/core/lib/Drupal/Core/Entity/EntityManager.php b/core/lib/Drupal/Core/Entity/EntityManager.php index 691b282276ba..56fe0a72185a 100644 --- a/core/lib/Drupal/Core/Entity/EntityManager.php +++ b/core/lib/Drupal/Core/Entity/EntityManager.php @@ -362,6 +362,7 @@ class EntityManager implements EntityManagerInterface, ContainerAwareInterface { * @see https://www.drupal.org/node/2549139 */ public function getEntityTypeLabels($group = FALSE) { + @trigger_error('EntityManagerInterface::getEntityTypeLabels() is deprecated in 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityTypeRepositoryInterface::getEntityTypeLabels() instead. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); return $this->container->get('entity_type.repository')->getEntityTypeLabels($group); } @@ -534,6 +535,7 @@ class EntityManager implements EntityManagerInterface, ContainerAwareInterface { * @see https://www.drupal.org/node/2549139 */ public function getEntityTypeFromClass($class_name) { + @trigger_error('EntityManagerInterface::getEntityTypeFromClass() is deprecated in 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityTypeRepositoryInterface::getEntityTypeFromClass() instead. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); return $this->container->get('entity_type.repository')->getEntityTypeFromClass($class_name); } @@ -666,13 +668,22 @@ class EntityManager implements EntityManagerInterface, ContainerAwareInterface { * @see https://www.drupal.org/node/2549139 */ public function getLastInstalledDefinition($entity_type_id) { + @trigger_error('EntityManagerInterface::getLastInstalledDefinition() is deprecated in 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface::getLastInstalledDefinition() instead. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); return $this->container->get('entity.last_installed_schema.repository')->getLastInstalledDefinition($entity_type_id); } /** * {@inheritdoc} + * + * @deprecated EntityManagerInterface::useCaches() is deprecated in 8.0.0 and + * will be removed before Drupal 9.0.0. Use + * \Drupal\Core\Entity\EntityTypeManagerInterface::useCaches() and/or + * Drupal\Core\Entity\EntityFieldManagerInterface::useCaches() instead. + * + * @see https://www.drupal.org/node/2549139 */ public function useCaches($use_caches = FALSE) { + @trigger_error('EntityManagerInterface::useCaches() is deprecated in 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityTypeManagerInterface::useCaches() and/or Drupal\Core\Entity\EntityFieldManagerInterface::useCaches() instead. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); $this->container->get('entity_type.manager')->useCaches($use_caches); // @todo EntityFieldManager is not a plugin manager, and should not co-opt @@ -690,6 +701,7 @@ class EntityManager implements EntityManagerInterface, ContainerAwareInterface { * @see https://www.drupal.org/node/2549139 */ public function getLastInstalledFieldStorageDefinitions($entity_type_id) { + @trigger_error('EntityManagerInterface::getLastInstalledFieldStorageDefinitions() is deprecated in 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface::getLastInstalledFieldStorageDefinitions() instead. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); return $this->container->get('entity.last_installed_schema.repository')->getLastInstalledFieldStorageDefinitions($entity_type_id); } @@ -729,6 +741,7 @@ class EntityManager implements EntityManagerInterface, ContainerAwareInterface { * @see https://www.drupal.org/node/2549139 */ public function createInstance($plugin_id, array $configuration = []) { + @trigger_error('EntityManagerInterface::createInstance() is deprecated in 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityTypeManagerInterface::createInstance() instead. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); return $this->container->get('entity_type.manager')->createInstance($plugin_id, $configuration); } @@ -742,6 +755,7 @@ class EntityManager implements EntityManagerInterface, ContainerAwareInterface { * @see https://www.drupal.org/node/2549139 */ public function getInstance(array $options) { + @trigger_error('EntityManagerInterface::getInstance() is deprecated in 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityTypeManagerInterface::getInstance() instead. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); return $this->container->get('entity_type.manager')->getInstance($options); } diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php index dd256a737e96..f87a0d8bd6e0 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php @@ -357,7 +357,7 @@ class EntityReferenceItem extends FieldItemBase implements OptionsProviderInterf $element['target_type'] = [ '#type' => 'select', '#title' => t('Type of item to reference'), - '#options' => \Drupal::entityManager()->getEntityTypeLabels(TRUE), + '#options' => \Drupal::service('entity_type.repository')->getEntityTypeLabels(TRUE), '#default_value' => $this->getSetting('target_type'), '#required' => TRUE, '#disabled' => $has_data, diff --git a/core/modules/content_translation/content_translation.module b/core/modules/content_translation/content_translation.module index 3265cd96f2d1..d6f6f0c60399 100644 --- a/core/modules/content_translation/content_translation.module +++ b/core/modules/content_translation/content_translation.module @@ -223,7 +223,7 @@ function content_translation_entity_base_field_info(EntityTypeInterface $entity_ $entity_type_id = $entity_type->id(); if ($manager->isSupported($entity_type_id)) { $definitions = $manager->getTranslationHandler($entity_type_id)->getFieldDefinitions(); - $installed_storage_definitions = \Drupal::entityManager()->getLastInstalledFieldStorageDefinitions($entity_type_id); + $installed_storage_definitions = \Drupal::service('entity.last_installed_schema.repository')->getLastInstalledFieldStorageDefinitions($entity_type_id); // We return metadata storage fields whenever content translation is enabled // or it was enabled before, so that we keep translation metadata around // when translation is disabled. diff --git a/core/modules/content_translation/src/ContentTranslationHandler.php b/core/modules/content_translation/src/ContentTranslationHandler.php index 20b0206185f4..7e9e980a1a87 100644 --- a/core/modules/content_translation/src/ContentTranslationHandler.php +++ b/core/modules/content_translation/src/ContentTranslationHandler.php @@ -9,8 +9,9 @@ use Drupal\Core\Entity\EntityChangedInterface; use Drupal\Core\Entity\EntityChangesDetectionTrait; use Drupal\Core\Entity\EntityHandlerInterface; use Drupal\Core\Entity\EntityInterface; -use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface; use Drupal\Core\Entity\EntityTypeInterface; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Language\LanguageInterface; @@ -107,8 +108,8 @@ class ContentTranslationHandler implements ContentTranslationHandlerInterface, E * The language manager. * @param \Drupal\content_translation\ContentTranslationManagerInterface $manager * The content translation manager service. - * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager - * The entity manager. + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager + * The entity type manager. * @param \Drupal\Core\Session\AccountInterface $current_user * The current user. * @param \Drupal\Core\Messenger\MessengerInterface $messenger @@ -116,14 +117,18 @@ class ContentTranslationHandler implements ContentTranslationHandlerInterface, E * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter * The date formatter service. */ - public function __construct(EntityTypeInterface $entity_type, LanguageManagerInterface $language_manager, ContentTranslationManagerInterface $manager, EntityManagerInterface $entity_manager, AccountInterface $current_user, MessengerInterface $messenger, DateFormatterInterface $date_formatter) { + public function __construct(EntityTypeInterface $entity_type, LanguageManagerInterface $language_manager, ContentTranslationManagerInterface $manager, EntityTypeManagerInterface $entity_type_manager, AccountInterface $current_user, MessengerInterface $messenger, DateFormatterInterface $date_formatter, EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository = NULL) { $this->entityTypeId = $entity_type->id(); $this->entityType = $entity_type; $this->languageManager = $language_manager; $this->manager = $manager; - $this->entityTypeManager = $entity_manager; + $this->entityTypeManager = $entity_type_manager; $this->currentUser = $current_user; - $this->fieldStorageDefinitions = $entity_manager->getLastInstalledFieldStorageDefinitions($this->entityTypeId); + if (!$entity_last_installed_schema_repository) { + @trigger_error('Calling ContentTranslationHandler::__construct() with the $entity_last_installed_schema_repository argument is supported in drupal:8.7.0 and will be required before drupal:9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); + $entity_last_installed_schema_repository = \Drupal::service('entity.last_installed_schema.repository'); + } + $this->fieldStorageDefinitions = $entity_last_installed_schema_repository->getLastInstalledFieldStorageDefinitions($this->entityTypeId); $this->messenger = $messenger; $this->dateFormatter = $date_formatter; } @@ -136,10 +141,11 @@ class ContentTranslationHandler implements ContentTranslationHandlerInterface, E $entity_type, $container->get('language_manager'), $container->get('content_translation.manager'), - $container->get('entity.manager'), + $container->get('entity_type.manager'), $container->get('current_user'), $container->get('messenger'), - $container->get('date.formatter') + $container->get('date.formatter'), + $container->get('entity.last_installed_schema.repository') ); } diff --git a/core/modules/system/tests/src/Functional/Entity/Update/UpdateApiEntityDefinitionUpdateTest.php b/core/modules/system/tests/src/Functional/Entity/Update/UpdateApiEntityDefinitionUpdateTest.php index 1c3c6e3e0aee..38672c4d590e 100644 --- a/core/modules/system/tests/src/Functional/Entity/Update/UpdateApiEntityDefinitionUpdateTest.php +++ b/core/modules/system/tests/src/Functional/Entity/Update/UpdateApiEntityDefinitionUpdateTest.php @@ -20,13 +20,6 @@ class UpdateApiEntityDefinitionUpdateTest extends BrowserTestBase { */ protected static $modules = ['entity_test']; - /** - * The entity manager. - * - * @var \Drupal\Core\Entity\EntityManagerInterface - */ - protected $entityManager; - /** * The entity definition update manager. * @@ -40,7 +33,6 @@ class UpdateApiEntityDefinitionUpdateTest extends BrowserTestBase { protected function setUp() { parent::setUp(); - $this->entityManager = $this->container->get('entity.manager'); $this->updatesManager = $this->container->get('entity.definition_update_manager'); $admin = $this->drupalCreateUser([], FALSE, TRUE); @@ -181,8 +173,8 @@ class UpdateApiEntityDefinitionUpdateTest extends BrowserTestBase { * The reloaded entity object. */ protected function reloadEntity(EntityTest $entity) { - $this->entityManager->useCaches(FALSE); - $this->entityManager->getStorage('entity_test')->resetCache([$entity->id()]); + \Drupal::entityTypeManager()->useCaches(FALSE); + \Drupal::service('entity_field.manager')->useCaches(FALSE); return EntityTest::load($entity->id()); } diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php index 684dfd49a43a..39c24d2f2504 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php @@ -411,7 +411,7 @@ class EntityDefinitionUpdateTest extends EntityKernelTestBase { /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */ $table_mapping = $storage->getTableMapping(); - $storage_definition = $this->entityManager->getLastInstalledFieldStorageDefinitions($entity_type_id)['new_base_field']; + $storage_definition = \Drupal::service('entity.last_installed_schema.repository')->getLastInstalledFieldStorageDefinitions($entity_type_id)['new_base_field']; // Save an entity with the base field populated. $entity = $storage->create(['new_base_field' => 'foo']); @@ -574,7 +574,7 @@ class EntityDefinitionUpdateTest extends EntityKernelTestBase { /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */ $table_mapping = $storage->getTableMapping(); - $storage_definition = $this->entityManager->getLastInstalledFieldStorageDefinitions('entity_test_update')['new_bundle_field']; + $storage_definition = \Drupal::service('entity.last_installed_schema.repository')->getLastInstalledFieldStorageDefinitions('entity_test_update')['new_bundle_field']; // Check that the bundle field has a dedicated table. $dedicated_table_name = $table_mapping->getDedicatedDataTableName($storage_definition); diff --git a/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php index 9131b8883cae..c3c659ca88d5 100644 --- a/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php @@ -6,7 +6,9 @@ use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\Entity\EntityRepositoryInterface; use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface; use Drupal\Core\Entity\EntityManager; +use Drupal\Core\Entity\EntityType; use Drupal\Core\Entity\EntityTypeBundleInfoInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeRepositoryInterface; @@ -61,6 +63,13 @@ class EntityManagerTest extends UnitTestCase { */ protected $entityRepository; + /** + * The entity last installed schema repository. + * + * @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepository|\Prophecy\Prophecy\ProphecyInterface + */ + protected $entityLastInstalledSchemaRepository; + /** * {@inheritdoc} */ @@ -72,6 +81,7 @@ class EntityManagerTest extends UnitTestCase { $this->entityTypeBundleInfo = $this->prophesize(EntityTypeBundleInfoInterface::class); $this->entityFieldManager = $this->prophesize(EntityFieldManagerInterface::class); $this->entityRepository = $this->prophesize(EntityRepositoryInterface::class); + $this->entityLastInstalledSchemaRepository = $this->prophesize(EntityLastInstalledSchemaRepositoryInterface::class); $container = new ContainerBuilder(); $container->set('entity_type.manager', $this->entityTypeManager->reveal()); @@ -79,6 +89,7 @@ class EntityManagerTest extends UnitTestCase { $container->set('entity_type.bundle.info', $this->entityTypeBundleInfo->reveal()); $container->set('entity_field.manager', $this->entityFieldManager->reveal()); $container->set('entity.repository', $this->entityRepository->reveal()); + $container->set('entity.last_installed_schema.repository', $this->entityLastInstalledSchemaRepository->reveal()); $this->entityManager = new EntityManager(); $this->entityManager->setContainer($container); @@ -180,4 +191,87 @@ class EntityManagerTest extends UnitTestCase { $this->assertInstanceOf(EntityInterface::class, $this->entityManager->loadEntityByConfigTarget('config_test', 'test')); } + /** + * Tests the getEntityTypeFromClass() method. + * + * @covers ::getEntityTypeFromClass + * + * @expectedDeprecation EntityManagerInterface::getEntityTypeFromClass() is deprecated in 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityTypeRepositoryInterface::getEntityTypeFromClass() instead. See https://www.drupal.org/node/2549139. + */ + public function testGetEntityTypeFromClass() { + $class = '\Drupal\example\Entity\ExampleEntity'; + $this->entityTypeRepository->getEntityTypeFromClass($class)->shouldBeCalled()->willReturn('example_entity_type'); + + $this->assertEquals('example_entity_type', $this->entityManager->getEntityTypeFromClass($class)); + } + + /** + * Tests the getLastInstalledDefinition() method. + * + * @covers ::getLastInstalledDefinition + * + * @expectedDeprecation EntityManagerInterface::getLastInstalledDefinition() is deprecated in 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface::getLastInstalledDefinition() instead. See https://www.drupal.org/node/2549139. + */ + public function testGetLastInstalledDefinition() { + $entity_type_id = 'example_entity_type'; + $entity_type = new EntityType(['id' => $entity_type_id]); + $this->entityLastInstalledSchemaRepository->getLastInstalledDefinition($entity_type_id)->shouldBeCalled()->willReturn($entity_type); + + $this->assertEquals($entity_type, $this->entityManager->getLastInstalledDefinition($entity_type_id)); + } + + /** + * Tests the getLastInstalledFieldStorageDefinitions() method. + * + * @covers ::getLastInstalledFieldStorageDefinitions + * + * @expectedDeprecation EntityManagerInterface::getLastInstalledFieldStorageDefinitions() is deprecated in 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface::getLastInstalledFieldStorageDefinitions() instead. See https://www.drupal.org/node/2549139. + */ + public function testGetLastInstalledFieldStorageDefinitions() { + $entity_type_id = 'example_entity_type'; + $this->entityLastInstalledSchemaRepository->getLastInstalledFieldStorageDefinitions($entity_type_id)->shouldBeCalled()->willReturn([]); + + $this->assertEquals([], $this->entityManager->getLastInstalledFieldStorageDefinitions($entity_type_id)); + } + + /** + * Tests the useCaches() method. + * + * @covers ::useCaches + * + * @expectedDeprecation EntityManagerInterface::useCaches() is deprecated in 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityTypeManagerInterface::useCaches() and/or Drupal\Core\Entity\EntityFieldManagerInterface::useCaches() instead. See https://www.drupal.org/node/2549139. + */ + public function testUseCaches() { + $this->entityTypeManager->useCaches(TRUE)->shouldBeCalled(); + $this->entityFieldManager->useCaches(TRUE)->shouldBeCalled(); + + $this->entityManager->useCaches(TRUE); + } + + /** + * Tests the createInstance() method. + * + * @covers ::createInstance + * + * @expectedDeprecation EntityManagerInterface::createInstance() is deprecated in 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityTypeManagerInterface::createInstance() instead. See https://www.drupal.org/node/2549139. + */ + public function testCreateInstance() { + $this->entityTypeManager->createInstance('plugin_id', ['example' => TRUE])->shouldBeCalled(); + + $this->entityManager->createInstance('plugin_id', ['example' => TRUE]); + } + + /** + * Tests the getInstance() method. + * + * @covers ::getInstance + * + * @expectedDeprecation EntityManagerInterface::getInstance() is deprecated in 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityTypeManagerInterface::getInstance() instead. See https://www.drupal.org/node/2549139. + */ + public function testGetInstance() { + $this->entityTypeManager->getInstance(['example' => TRUE])->shouldBeCalled(); + + $this->entityManager->getInstance(['example' => TRUE]); + } + }