From 05bbd9d036939acba862460483ad13a5f425201d Mon Sep 17 00:00:00 2001 From: Nathaniel Catchpole Date: Fri, 4 Jan 2019 11:52:43 +0000 Subject: [PATCH] Issue #3006409 by Berdir: Update deprecated usages of Entity::getEntityManager() --- .../Config/Entity/ConfigEntityBundleBase.php | 18 +++++++++--------- .../Drupal/Core/Entity/ContentEntityBase.php | 12 ++++++------ core/lib/Drupal/Core/Entity/Entity.php | 1 + .../Drupal/Core/Entity/EntityDisplayBase.php | 6 +++--- .../Core/Field/Entity/BaseFieldOverride.php | 4 ++-- core/lib/Drupal/Core/Field/FieldConfigBase.php | 8 ++++---- core/modules/field/src/Entity/FieldConfig.php | 2 +- core/modules/node/src/Entity/NodeType.php | 2 +- 8 files changed, 27 insertions(+), 26 deletions(-) diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigEntityBundleBase.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBundleBase.php index 59128d86c77..33c8fc25034 100644 --- a/core/lib/Drupal/Core/Config/Entity/ConfigEntityBundleBase.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBundleBase.php @@ -19,13 +19,13 @@ abstract class ConfigEntityBundleBase extends ConfigEntityBase { protected function deleteDisplays() { // Remove entity displays of the deleted bundle. if ($displays = $this->loadDisplays('entity_view_display')) { - $storage = $this->entityManager()->getStorage('entity_view_display'); + $storage = $this->entityTypeManager()->getStorage('entity_view_display'); $storage->delete($displays); } // Remove entity form displays of the deleted bundle. if ($displays = $this->loadDisplays('entity_form_display')) { - $storage = $this->entityManager()->getStorage('entity_form_display'); + $storage = $this->entityTypeManager()->getStorage('entity_form_display'); $storage->delete($displays); } } @@ -36,20 +36,20 @@ abstract class ConfigEntityBundleBase extends ConfigEntityBase { public function postSave(EntityStorageInterface $storage, $update = TRUE) { parent::postSave($storage, $update); - $entity_manager = $this->entityManager(); + $entity_type_manager = $this->entityTypeManager(); $bundle_of = $this->getEntityType()->getBundleOf(); if (!$update) { - $entity_manager->onBundleCreate($this->id(), $bundle_of); + \Drupal::service('entity_bundle.listener')->onBundleCreate($this->id(), $bundle_of); } else { // Invalidate the render cache of entities for which this entity // is a bundle. - if ($entity_manager->hasHandler($bundle_of, 'view_builder')) { - $entity_manager->getViewBuilder($bundle_of)->resetCache(); + if ($entity_type_manager->hasHandler($bundle_of, 'view_builder')) { + $entity_type_manager->getViewBuilder($bundle_of)->resetCache(); } // Entity bundle field definitions may depend on bundle settings. - $entity_manager->clearCachedFieldDefinitions(); - $entity_manager->clearCachedBundles(); + \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions(); + $this->entityTypeBundleInfo()->clearCachedBundles(); } } @@ -106,7 +106,7 @@ abstract class ConfigEntityBundleBase extends ConfigEntityBase { ->condition('id', $this->getEntityType()->getBundleOf() . '.' . $this->getOriginalId() . '.', 'STARTS_WITH') ->execute(); if ($ids) { - $storage = $this->entityManager()->getStorage($entity_type_id); + $storage = $this->entityTypeManager()->getStorage($entity_type_id); return $storage->loadMultiple($ids); } return []; diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index b01d608c31a..74e0f1c19ae 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -429,7 +429,7 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C public function isTranslatable() { // Check the bundle is translatable, the entity has a language defined, and // the site has more than one language. - $bundles = $this->entityManager()->getBundleInfo($this->entityTypeId); + $bundles = $this->entityTypeBundleInfo()->getBundleInfo($this->entityTypeId); return !empty($bundles[$this->bundle()]['translatable']) && !$this->getUntranslated()->language()->isLocked() && $this->languageManager()->isMultilingual(); } @@ -679,7 +679,7 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C */ public function getFieldDefinitions() { if (!isset($this->fieldDefinitions)) { - $this->fieldDefinitions = $this->entityManager()->getFieldDefinitions($this->entityTypeId, $this->bundle()); + $this->fieldDefinitions = \Drupal::service('entity_field.manager')->getFieldDefinitions($this->entityTypeId, $this->bundle()); } return $this->fieldDefinitions; } @@ -700,11 +700,11 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C */ public function access($operation, AccountInterface $account = NULL, $return_as_object = FALSE) { if ($operation == 'create') { - return $this->entityManager() + return $this->entityTypeManager() ->getAccessControlHandler($this->entityTypeId) ->createAccess($this->bundle(), $account, [], $return_as_object); } - return $this->entityManager() + return $this->entityTypeManager() ->getAccessControlHandler($this->entityTypeId) ->access($this, $operation, $account, $return_as_object); } @@ -960,7 +960,7 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C // Initialize the translation object. /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */ - $storage = $this->entityManager()->getStorage($this->getEntityTypeId()); + $storage = $this->entityTypeManager()->getStorage($this->getEntityTypeId()); $this->translations[$langcode]['status'] = !isset($this->translations[$langcode]['status_existed']) ? static::TRANSLATION_CREATED : static::TRANSLATION_EXISTING; return $storage->createTranslation($this, $langcode, $values); } @@ -1411,7 +1411,7 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C if (!$original) { $id = $this->getOriginalId() !== NULL ? $this->getOriginalId() : $this->id(); - $original = $this->entityManager()->getStorage($this->getEntityTypeId())->loadUnchanged($id); + $original = $this->entityTypeManager()->getStorage($this->getEntityTypeId())->loadUnchanged($id); } // If the current translation has just been added, we have a change. diff --git a/core/lib/Drupal/Core/Entity/Entity.php b/core/lib/Drupal/Core/Entity/Entity.php index eedfe6f0805..c5b6b9a1cfe 100644 --- a/core/lib/Drupal/Core/Entity/Entity.php +++ b/core/lib/Drupal/Core/Entity/Entity.php @@ -76,6 +76,7 @@ abstract class Entity implements EntityInterface { * correct interface or service. */ protected function entityManager() { + @trigger_error('Entity::getEntityManager() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use ::getEntityTypeManager() instead. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); return \Drupal::entityManager(); } diff --git a/core/lib/Drupal/Core/Entity/EntityDisplayBase.php b/core/lib/Drupal/Core/Entity/EntityDisplayBase.php index dbda544ef05..e696a982fed 100644 --- a/core/lib/Drupal/Core/Entity/EntityDisplayBase.php +++ b/core/lib/Drupal/Core/Entity/EntityDisplayBase.php @@ -289,7 +289,7 @@ abstract class EntityDisplayBase extends ConfigEntityBase implements EntityDispl */ public function calculateDependencies() { parent::calculateDependencies(); - $target_entity_type = $this->entityManager()->getDefinition($this->targetEntityType); + $target_entity_type = $this->entityTypeManager()->getDefinition($this->targetEntityType); // Create dependency on the bundle. $bundle_config_dependency = $target_entity_type->getBundleConfigDependency($this->bundle); @@ -300,7 +300,7 @@ abstract class EntityDisplayBase extends ConfigEntityBase implements EntityDispl // field overrides, since the field still exists without them. if (\Drupal::moduleHandler()->moduleExists('field')) { $components = $this->content + $this->hidden; - $field_definitions = $this->entityManager()->getFieldDefinitions($this->targetEntityType, $this->bundle); + $field_definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions($this->targetEntityType, $this->bundle); foreach (array_intersect_key($field_definitions, $components) as $field_name => $field_definition) { if ($field_definition instanceof ConfigEntityInterface && $field_definition->getEntityTypeId() == 'field_config') { $this->addDependency('config', $field_definition->getConfigDependencyName()); @@ -310,7 +310,7 @@ abstract class EntityDisplayBase extends ConfigEntityBase implements EntityDispl // Depend on configured modes. if ($this->mode != 'default') { - $mode_entity = $this->entityManager()->getStorage('entity_' . $this->displayContext . '_mode')->load($target_entity_type->id() . '.' . $this->mode); + $mode_entity = $this->entityTypeManager()->getStorage('entity_' . $this->displayContext . '_mode')->load($target_entity_type->id() . '.' . $this->mode); $this->addDependency('config', $mode_entity->getConfigDependencyName()); } return $this; diff --git a/core/lib/Drupal/Core/Field/Entity/BaseFieldOverride.php b/core/lib/Drupal/Core/Field/Entity/BaseFieldOverride.php index 555e22a2609..cd8d33965f5 100644 --- a/core/lib/Drupal/Core/Field/Entity/BaseFieldOverride.php +++ b/core/lib/Drupal/Core/Field/Entity/BaseFieldOverride.php @@ -163,7 +163,7 @@ class BaseFieldOverride extends FieldConfigBase { */ protected function getBaseFieldDefinition() { if (!isset($this->baseFieldDefinition)) { - $fields = $this->entityManager()->getBaseFieldDefinitions($this->entity_type); + $fields = \Drupal::service('entity_field.manager')->getBaseFieldDefinitions($this->entity_type); $this->baseFieldDefinition = $fields[$this->getName()]; } return $this->baseFieldDefinition; @@ -204,7 +204,7 @@ class BaseFieldOverride extends FieldConfigBase { $previous_definition = $this->original; } // Notify the entity storage. - $this->entityManager()->getStorage($this->getTargetEntityTypeId())->onFieldDefinitionUpdate($this, $previous_definition); + $this->entityTypeManager()->getStorage($this->getTargetEntityTypeId())->onFieldDefinitionUpdate($this, $previous_definition); } /** diff --git a/core/lib/Drupal/Core/Field/FieldConfigBase.php b/core/lib/Drupal/Core/Field/FieldConfigBase.php index 0d58b65614d..ac6596f82ac 100644 --- a/core/lib/Drupal/Core/Field/FieldConfigBase.php +++ b/core/lib/Drupal/Core/Field/FieldConfigBase.php @@ -246,7 +246,7 @@ abstract class FieldConfigBase extends ConfigEntityBase implements FieldConfigIn $this->addDependencies($definition['class']::calculateDependencies($this)); // Create dependency on the bundle. - $bundle_config_dependency = $this->entityManager()->getDefinition($this->entity_type)->getBundleConfigDependency($this->bundle); + $bundle_config_dependency = $this->entityTypeManager()->getDefinition($this->entity_type)->getBundleConfigDependency($this->bundle); $this->addDependency($bundle_config_dependency['type'], $bundle_config_dependency['name']); return $this; @@ -283,12 +283,12 @@ abstract class FieldConfigBase extends ConfigEntityBase implements FieldConfigIn */ public function postSave(EntityStorageInterface $storage, $update = TRUE) { // Clear the cache. - $this->entityManager()->clearCachedFieldDefinitions(); + \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions(); // Invalidate the render cache for all affected entities. $entity_type = $this->getFieldStorageDefinition()->getTargetEntityTypeId(); - if ($this->entityManager()->hasHandler($entity_type, 'view_builder')) { - $this->entityManager()->getViewBuilder($entity_type)->resetCache(); + if ($this->entityTypeManager()->hasHandler($entity_type, 'view_builder')) { + $this->entityTypeManager()->getViewBuilder($entity_type)->resetCache(); } } diff --git a/core/modules/field/src/Entity/FieldConfig.php b/core/modules/field/src/Entity/FieldConfig.php index 69be43c2b0c..7e3e5c8b62e 100644 --- a/core/modules/field/src/Entity/FieldConfig.php +++ b/core/modules/field/src/Entity/FieldConfig.php @@ -296,7 +296,7 @@ class FieldConfig extends FieldConfigBase implements FieldConfigInterface { if (!$this->fieldStorage) { $field_storage_definition = NULL; - $field_storage_definitions = $this->entityManager()->getFieldStorageDefinitions($this->entity_type); + $field_storage_definitions = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions($this->entity_type); if (isset($field_storage_definitions[$this->field_name])) { $field_storage_definition = $field_storage_definitions[$this->field_name]; } diff --git a/core/modules/node/src/Entity/NodeType.php b/core/modules/node/src/Entity/NodeType.php index 56efb8608dd..3338b93f1ee 100644 --- a/core/modules/node/src/Entity/NodeType.php +++ b/core/modules/node/src/Entity/NodeType.php @@ -198,7 +198,7 @@ class NodeType extends ConfigEntityBundleBase implements NodeTypeInterface { if ($update) { // Clear the cached field definitions as some settings affect the field // definitions. - $this->entityManager()->clearCachedFieldDefinitions(); + \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions(); } }