diff --git a/core/core.services.yml b/core/core.services.yml index 367cb74..0f21eb8 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -481,7 +481,7 @@ services: arguments: ['@theme_handler', '@config.factory', '@config.installer', '@module_handler', '@config.manager', '@asset.css.collection_optimizer', '@router.builder', '@logger.channel.default', '@state'] entity.manager: class: Drupal\Core\Entity\EntityManager - arguments: ['@language_manager', '@entity_type.manager', '@entity_type.repository', '@entity_type.bundle.manager', '@entity_display.manager', '@entity_field.manager', '@entity_type.listener', '@entity_definition.repository', '@entity_bundle.listener', '@field_storage_definition.listener', '@field_definition.listener'] + arguments: ['@entity_type.manager', '@entity_type.repository', '@entity_type.bundle.manager', '@entity_display.manager', '@entity_field.manager', '@entity_type.listener', '@entity_definition.repository', '@entity.repository', '@entity_bundle.listener', '@field_storage_definition.listener', '@field_definition.listener'] tags: - { name: plugin_manager_cache_clear } entity_type.manager: @@ -496,6 +496,9 @@ services: entity_type.bundle.manager: class: Drupal\Core\Entity\EntityTypeBundleManager arguments: ['@entity_type.manager', '@language_manager', '@module_handler', '@typed_data_manager', '@cache.discovery'] + entity.repository: + class: Drupal\Core\Entity\EntityRepository + arguments: ['@entity_type.manager', '@language_manager'] entity_display.manager: class: Drupal\Core\Entity\EntityDisplayManager arguments: ['@entity_type.manager', '@module_handler', '@cache.discovery', '@language_manager'] @@ -517,7 +520,7 @@ services: class: Drupal\Core\Entity\EntityDefinitionUpdateManager arguments: ['@entity.manager'] entity_definition.repository: - class: Drupal\Core\Entity\EntityDefinitionRepository + class: Drupal\Core\Entity\EntityInstalledDefinitionRepository arguments: ['@keyvalue'] field_storage_definition.listener: class: Drupal\Core\Field\FieldStorageDefinitionListener diff --git a/core/lib/Drupal/Core/Cache/CacheBackendTrait.php b/core/lib/Drupal/Core/Cache/CacheBackendTrait.php index e6147c1..3016575 100644 --- a/core/lib/Drupal/Core/Cache/CacheBackendTrait.php +++ b/core/lib/Drupal/Core/Cache/CacheBackendTrait.php @@ -29,6 +29,12 @@ /** * Fetches from the cache backend, respecting the use caches flag. * + * @param string $cid + * The cache ID of the data to retrieve. + * + * @return object|false + * The cache item or FALSE on failure. + * * @see \Drupal\Core\Cache\CacheBackendInterface::get() */ protected function cacheGet($cid) { @@ -41,6 +47,28 @@ protected function cacheGet($cid) { /** * Stores data in the persistent cache, respecting the use caches flag. * + * @param string $cid + * The cache ID of the data to store. + * @param mixed $data + * The data to store in the cache. + * Some storage engines only allow objects up to a maximum of 1MB in size to + * be stored by default. When caching large arrays or similar, take care to + * ensure $data does not exceed this size. + * @param int $expire + * One of the following values: + * - CacheBackendInterface::CACHE_PERMANENT: Indicates that the item should + * not be removed unless it is deleted explicitly. + * - A Unix timestamp: Indicates that the item will be considered invalid + * after this time, i.e. it will not be returned by get() unless + * $allow_invalid has been set to TRUE. When the item has expired, it may + * be permanently deleted by the garbage collector at any time. + * @param array $tags + * An array of tags to be stored with the cache item. These should normally + * identify objects used to build the cache item, which should trigger + * cache invalidation when updated. For example if a cached item represents + * a node, both the node ID and the author's user ID might be passed in as + * tags. For example array('node' => array(123), 'user' => array(92)). + * * @see \Drupal\Core\Cache\CacheBackendInterface::set() */ protected function cacheSet($cid, $data, $expire = Cache::PERMANENT, array $tags = []) { diff --git a/core/lib/Drupal/Core/Entity/EntityDefinitionRepository.php b/core/lib/Drupal/Core/Entity/EntityInstalledDefinitionRepository.php similarity index 91% rename from core/lib/Drupal/Core/Entity/EntityDefinitionRepository.php rename to core/lib/Drupal/Core/Entity/EntityInstalledDefinitionRepository.php index 9bc3d9b..78e04f0 100644 --- a/core/lib/Drupal/Core/Entity/EntityDefinitionRepository.php +++ b/core/lib/Drupal/Core/Entity/EntityInstalledDefinitionRepository.php @@ -2,7 +2,7 @@ /** * @file - * Contains \Drupal\Core\Entity\EntityDefinitionRepository. + * Contains \Drupal\Core\Entity\EntityInstalledDefinitionRepository. */ namespace Drupal\Core\Entity; @@ -11,9 +11,9 @@ use Drupal\Core\KeyValueStore\KeyValueFactoryInterface; /** - * Provides a repository for entity definitions. + * Provides a repository for installed entity definitions. */ -class EntityDefinitionRepository implements EntityDefinitionRepositoryInterface { +class EntityInstalledDefinitionRepository implements EntityInstalledDefinitionRepositoryInterface { /** * The key-value factory. @@ -23,7 +23,7 @@ class EntityDefinitionRepository implements EntityDefinitionRepositoryInterface protected $keyValueFactory; /** - * Constructs a new EntityDefinitionRepository. + * Constructs a new EntityInstalledDefinitionRepository. * * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value_factory * The key-value factory. diff --git a/core/lib/Drupal/Core/Entity/EntityDefinitionRepositoryInterface.php b/core/lib/Drupal/Core/Entity/EntityInstalledDefinitionRepositoryInterface.php similarity index 95% rename from core/lib/Drupal/Core/Entity/EntityDefinitionRepositoryInterface.php rename to core/lib/Drupal/Core/Entity/EntityInstalledDefinitionRepositoryInterface.php index 0c43dc6..326adba 100644 --- a/core/lib/Drupal/Core/Entity/EntityDefinitionRepositoryInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityInstalledDefinitionRepositoryInterface.php @@ -2,7 +2,7 @@ /** * @file - * Contains \Drupal\Core\Entity\EntityDefinitionRepositoryInterface. + * Contains \Drupal\Core\Entity\EntityInstalledDefinitionRepositoryInterface. */ namespace Drupal\Core\Entity; @@ -10,9 +10,9 @@ use Drupal\Core\Field\FieldStorageDefinitionInterface; /** - * Provides an interface for an entity definition repository. + * Provides an interface for an installed entity definition repository. */ -interface EntityDefinitionRepositoryInterface { +interface EntityInstalledDefinitionRepositoryInterface { /** * Gets the entity type definition in its most recently installed state. diff --git a/core/lib/Drupal/Core/Entity/EntityManager.php b/core/lib/Drupal/Core/Entity/EntityManager.php index 8bc7fd5..2a06574 100644 --- a/core/lib/Drupal/Core/Entity/EntityManager.php +++ b/core/lib/Drupal/Core/Entity/EntityManager.php @@ -11,9 +11,6 @@ use Drupal\Core\Field\FieldDefinitionListenerInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionListenerInterface; -use Drupal\Core\Language\LanguageInterface; -use Drupal\Core\Language\LanguageManagerInterface; -use Drupal\Core\TypedData\TranslatableInterface; /** * Provides a wrapper around many other services relating to entities. @@ -21,13 +18,6 @@ class EntityManager implements EntityManagerInterface { /** - * The language manager. - * - * @var \Drupal\Core\Language\LanguageManagerInterface - */ - protected $languageManager; - - /** * The entity type manager. * * @var \Drupal\Core\Entity\EntityTypeManagerInterface @@ -72,9 +62,9 @@ class EntityManager implements EntityManagerInterface { /** * The entity definition manager. * - * @var \Drupal\Core\Entity\EntityDefinitionRepositoryInterface + * @var \Drupal\Core\Entity\EntityInstalledDefinitionRepositoryInterface */ - protected $entityDefinitionRepository; + protected $entityInstalledDefinitionRepository; /** * The entity bundle listener. @@ -98,10 +88,15 @@ class EntityManager implements EntityManagerInterface { protected $fieldDefinitionListener; /** + * The entity repository. + * + * @var \Drupal\Core\Entity\EntityRepositoryInterface + */ + protected $entityRepository; + + /** * Constructs a new Entity plugin manager. * - * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager - * The language manager. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity type manager. * @param \Drupal\Core\Entity\EntityTypeRepositoryInterface $entity_type_repository @@ -114,8 +109,10 @@ class EntityManager implements EntityManagerInterface { * The entity field manager. * @param \Drupal\Core\Entity\EntityTypeListenerInterface $entity_type_listener * The entity type listener. - * @param \Drupal\Core\Entity\EntityDefinitionRepositoryInterface $entity_definition_repository + * @param \Drupal\Core\Entity\EntityInstalledDefinitionRepositoryInterface $entity_installed_definition_repository * The entity definition repository. + * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository + * The entity repository. * @param \Drupal\Core\Entity\EntityBundleListenerInterface $entity_bundle_listener * The entity bundle listener. * @param \Drupal\Core\Field\FieldStorageDefinitionListenerInterface $field_storage_definition_listener @@ -123,16 +120,15 @@ class EntityManager implements EntityManagerInterface { * @param \Drupal\Core\Field\FieldDefinitionListenerInterface $field_definition_listener * The field definition listener. */ - public function __construct(LanguageManagerInterface $language_manager, EntityTypeManagerInterface $entity_type_manager, EntityTypeRepositoryInterface $entity_type_repository, EntityTypeBundleManagerInterface $entity_type_bundle_manager, EntityDisplayManagerInterface $entity_display_manager, EntityFieldManagerInterface $entity_field_manager, EntityTypeListenerInterface $entity_type_listener, EntityDefinitionRepositoryInterface $entity_definition_repository, EntityBundleListenerInterface $entity_bundle_listener, FieldStorageDefinitionListenerInterface $field_storage_definition_listener, FieldDefinitionListenerInterface $field_definition_listener) { - $this->languageManager = $language_manager; - + public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeRepositoryInterface $entity_type_repository, EntityTypeBundleManagerInterface $entity_type_bundle_manager, EntityDisplayManagerInterface $entity_display_manager, EntityFieldManagerInterface $entity_field_manager, EntityTypeListenerInterface $entity_type_listener, EntityInstalledDefinitionRepositoryInterface $entity_installed_definition_repository, EntityRepositoryInterface $entity_repository, EntityBundleListenerInterface $entity_bundle_listener, FieldStorageDefinitionListenerInterface $field_storage_definition_listener, FieldDefinitionListenerInterface $field_definition_listener) { $this->entityTypeManager = $entity_type_manager; $this->entityTypeRepository = $entity_type_repository; $this->entityTypeBundleManager = $entity_type_bundle_manager; $this->entityDisplayManager = $entity_display_manager; $this->entityFieldManager = $entity_field_manager; $this->entityTypeListener = $entity_type_listener; - $this->entityDefinitionRepository = $entity_definition_repository; + $this->entityInstalledDefinitionRepository = $entity_installed_definition_repository; + $this->entityRepository = $entity_repository; $this->entityBundleListener = $entity_bundle_listener; $this->fieldStorageDefinitionListener = $field_storage_definition_listener; @@ -379,38 +375,11 @@ public function getEntityTypeLabels($group = FALSE) { /** * {@inheritdoc} + * + * @deprecated */ public function getTranslationFromContext(EntityInterface $entity, $langcode = NULL, $context = array()) { - $translation = $entity; - - if ($entity instanceof TranslatableInterface && count($entity->getTranslationLanguages()) > 1) { - if (empty($langcode)) { - $langcode = $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId(); - $entity->addCacheContexts(['languages:' . LanguageInterface::TYPE_CONTENT]); - } - - // Retrieve language fallback candidates to perform the entity language - // negotiation, unless the current translation is already the desired one. - if ($entity->language()->getId() != $langcode) { - $context['data'] = $entity; - $context += array('operation' => 'entity_view', 'langcode' => $langcode); - $candidates = $this->languageManager->getFallbackCandidates($context); - - // Ensure the default language has the proper language code. - $default_language = $entity->getUntranslated()->language(); - $candidates[$default_language->getId()] = LanguageInterface::LANGCODE_DEFAULT; - - // Return the most fitting entity translation. - foreach ($candidates as $candidate) { - if ($entity->hasTranslation($candidate)) { - $translation = $entity->getTranslation($candidate); - break; - } - } - } - } - - return $translation; + return $this->entityRepository->getTranslationFromContext($entity, $langcode, $context); } /** @@ -480,7 +449,7 @@ public function clearDisplayModeInfo() { * @deprecated */ public function loadEntityByUuid($entity_type_id, $uuid) { - return $this->entityTypeRepository->loadEntityByUuid($entity_type_id, $uuid); + return $this->entityRepository->loadEntityByUuid($entity_type_id, $uuid); } /** @@ -489,7 +458,7 @@ public function loadEntityByUuid($entity_type_id, $uuid) { * @deprecated */ public function loadEntityByConfigTarget($entity_type_id, $target) { - return $this->entityTypeRepository->loadEntityByConfigTarget($entity_type_id, $target); + return $this->entityRepository->loadEntityByConfigTarget($entity_type_id, $target); } /** @@ -586,7 +555,7 @@ public function onBundleDelete($bundle, $entity_type_id) { * @deprecated */ public function getLastInstalledDefinition($entity_type_id) { - return $this->entityDefinitionRepository->getLastInstalledDefinition($entity_type_id); + return $this->entityInstalledDefinitionRepository->getLastInstalledDefinition($entity_type_id); } /** @@ -606,7 +575,7 @@ public function useCaches($use_caches = FALSE) { * @deprecated */ public function getLastInstalledFieldStorageDefinitions($entity_type_id) { - return $this->entityDefinitionRepository->getLastInstalledFieldStorageDefinitions($entity_type_id); + return $this->entityInstalledDefinitionRepository->getLastInstalledFieldStorageDefinitions($entity_type_id); } /** diff --git a/core/lib/Drupal/Core/Entity/EntityManagerInterface.php b/core/lib/Drupal/Core/Entity/EntityManagerInterface.php index d6e9796..4a25201 100644 --- a/core/lib/Drupal/Core/Entity/EntityManagerInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityManagerInterface.php @@ -13,7 +13,8 @@ /** * Provides an interface for entity type managers. * - * Note: The following interfaces that are extended are deprecated as of 8.0.0 and will be removed in 9.0.0: + * Note: The following interfaces that are extended are deprecated as of 8.0.0 + * and will be removed in 9.0.0: * - \Drupal\Core\Entity\EntityTypeManagerInterface * - \Drupal\Core\Entity\EntityTypeRepositoryInterface * - \Drupal\Core\Entity\EntityTypeBundleManagerInterface @@ -27,37 +28,14 @@ interface EntityManagerInterface extends EntityTypeListenerInterface, EntityBundleListenerInterface, FieldStorageDefinitionListenerInterface, FieldDefinitionListenerInterface, EntityTypeManagerInterface, EntityTypeRepositoryInterface, EntityTypeBundleManagerInterface, EntityDisplayManagerInterface, EntityFieldManagerInterface { /** - * Gets the entity translation to be used in the given context. - * - * This will check whether a translation for the desired language is available - * and if not, it will fall back to the most appropriate translation based on - * the provided context. - * - * @param \Drupal\Core\Entity\EntityInterface $entity - * The entity whose translation will be returned. - * @param string $langcode - * (optional) The language of the current context. Defaults to the current - * content language. - * @param array $context - * (optional) An associative array of arbitrary data that can be useful to - * determine the proper fallback sequence. - * - * @return \Drupal\Core\Entity\EntityInterface - * An entity object for the translated data. - * - * @see \Drupal\Core\Language\LanguageManagerInterface::getFallbackCandidates() - */ - public function getTranslationFromContext(EntityInterface $entity, $langcode = NULL, $context = array()); - - /** - * @see \Drupal\Core\Entity\EntityDefinitionsRepositoryInterface::getLastInstalledDefinition() + * @see \Drupal\Core\Entity\EntityInstalledDefinitionRepositoryInterface::getLastInstalledDefinition() * * @deprecated */ public function getLastInstalledDefinition($entity_type_id); /** - * @see \Drupal\Core\Entity\EntityDefinitionRepositoryInterface::getLastInstalledFieldStorageDefinitions() + * @see \Drupal\Core\Entity\EntityInstalledDefinitionRepositoryInterface::getLastInstalledFieldStorageDefinitions() * * @deprecated */ diff --git a/core/lib/Drupal/Core/Entity/EntityRepository.php b/core/lib/Drupal/Core/Entity/EntityRepository.php new file mode 100644 index 0000000..b99b2ad --- /dev/null +++ b/core/lib/Drupal/Core/Entity/EntityRepository.php @@ -0,0 +1,120 @@ +entityTypeManager = $entity_type_manager; + $this->languageManager = $language_manager; + } + + /** + * {@inheritdoc} + */ + public function loadEntityByUuid($entity_type_id, $uuid) { + $entity_type = $this->entityTypeManager->getDefinition($entity_type_id); + + if (!$uuid_key = $entity_type->getKey('uuid')) { + throw new EntityStorageException("Entity type $entity_type_id does not support UUIDs."); + } + + $entities = $this->entityTypeManager->getStorage($entity_type_id)->loadByProperties([$uuid_key => $uuid]); + + return reset($entities); + } + + /** + * {@inheritdoc} + */ + public function loadEntityByConfigTarget($entity_type_id, $target) { + $entity_type = $this->entityTypeManager->getDefinition($entity_type_id); + + // For configuration entities, the config target is given by the entity ID. + // @todo Consider adding a method to allow entity types to indicate the + // target identifier key rather than hard-coding this check. Issue: + // https://www.drupal.org/node/2412983. + if ($entity_type instanceof ConfigEntityTypeInterface) { + $entity = $this->entityTypeManager->getStorage($entity_type_id)->load($target); + } + + // For content entities, the config target is given by the UUID. + else { + $entity = $this->loadEntityByUuid($entity_type_id, $target); + } + + return $entity; + } + + /** + * {@inheritdoc} + */ + public function getTranslationFromContext(EntityInterface $entity, $langcode = NULL, $context = array()) { + $translation = $entity; + + if ($entity instanceof TranslatableInterface && count($entity->getTranslationLanguages()) > 1) { + if (empty($langcode)) { + $langcode = $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId(); + $entity->addCacheContexts(['languages:' . LanguageInterface::TYPE_CONTENT]); + } + + // Retrieve language fallback candidates to perform the entity language + // negotiation, unless the current translation is already the desired one. + if ($entity->language()->getId() != $langcode) { + $context['data'] = $entity; + $context += array('operation' => 'entity_view', 'langcode' => $langcode); + $candidates = $this->languageManager->getFallbackCandidates($context); + + // Ensure the default language has the proper language code. + $default_language = $entity->getUntranslated()->language(); + $candidates[$default_language->getId()] = LanguageInterface::LANGCODE_DEFAULT; + + // Return the most fitting entity translation. + foreach ($candidates as $candidate) { + if ($entity->hasTranslation($candidate)) { + $translation = $entity->getTranslation($candidate); + break; + } + } + } + } + + return $translation; + } + +} diff --git a/core/lib/Drupal/Core/Entity/EntityRepositoryInterface.php b/core/lib/Drupal/Core/Entity/EntityRepositoryInterface.php new file mode 100644 index 0000000..c37be4a --- /dev/null +++ b/core/lib/Drupal/Core/Entity/EntityRepositoryInterface.php @@ -0,0 +1,77 @@ +entityTypeManager = $entity_type_manager; $this->entityFieldManager = $entity_field_manager; $this->eventDispatcher = $event_dispatcher; - $this->entityDefinitionRepository = $entity_definition_repository; + $this->entityInstalledDefinitionRepository = $entity_installed_definition_repository; } /** @@ -76,9 +76,9 @@ public function onEntityTypeCreate(EntityTypeInterface $entity_type) { $this->eventDispatcher->dispatch(EntityTypeEvents::CREATE, new EntityTypeEvent($entity_type)); - $this->entityDefinitionRepository->setLastInstalledDefinition($entity_type); + $this->entityInstalledDefinitionRepository->setLastInstalledDefinition($entity_type); if ($entity_type->isSubclassOf(FieldableEntityInterface::class)) { - $this->entityDefinitionRepository->setLastInstalledFieldStorageDefinitions($entity_type_id, $this->entityFieldManager->getFieldStorageDefinitions($entity_type_id)); + $this->entityInstalledDefinitionRepository->setLastInstalledFieldStorageDefinitions($entity_type_id, $this->entityFieldManager->getFieldStorageDefinitions($entity_type_id)); } } @@ -97,7 +97,7 @@ public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeI $this->eventDispatcher->dispatch(EntityTypeEvents::UPDATE, new EntityTypeEvent($entity_type, $original)); - $this->entityDefinitionRepository->setLastInstalledDefinition($entity_type); + $this->entityInstalledDefinitionRepository->setLastInstalledDefinition($entity_type); } /** @@ -115,7 +115,7 @@ public function onEntityTypeDelete(EntityTypeInterface $entity_type) { $this->eventDispatcher->dispatch(EntityTypeEvents::DELETE, new EntityTypeEvent($entity_type)); - $this->entityDefinitionRepository->deleteLastInstalledDefinition($entity_type_id); + $this->entityInstalledDefinitionRepository->deleteLastInstalledDefinition($entity_type_id); } } diff --git a/core/lib/Drupal/Core/Entity/EntityTypeRepository.php b/core/lib/Drupal/Core/Entity/EntityTypeRepository.php index 0ad9cab..d1adfea 100644 --- a/core/lib/Drupal/Core/Entity/EntityTypeRepository.php +++ b/core/lib/Drupal/Core/Entity/EntityTypeRepository.php @@ -7,7 +7,6 @@ namespace Drupal\Core\Entity; -use Drupal\Core\Config\Entity\ConfigEntityTypeInterface; use Drupal\Core\Entity\Exception\AmbiguousEntityClassException; use Drupal\Core\Entity\Exception\NoCorrespondingEntityClassException; use Drupal\Core\StringTranslation\StringTranslationTrait; @@ -48,43 +47,6 @@ public function __construct(EntityTypeManagerInterface $entity_type_manager) { /** * {@inheritdoc} */ - public function loadEntityByUuid($entity_type_id, $uuid) { - $entity_type = $this->entityTypeManager->getDefinition($entity_type_id); - - if (!$uuid_key = $entity_type->getKey('uuid')) { - throw new EntityStorageException("Entity type $entity_type_id does not support UUIDs."); - } - - $entities = $this->entityTypeManager->getStorage($entity_type_id)->loadByProperties([$uuid_key => $uuid]); - - return reset($entities); - } - - /** - * {@inheritdoc} - */ - public function loadEntityByConfigTarget($entity_type_id, $target) { - $entity_type = $this->entityTypeManager->getDefinition($entity_type_id); - - // For configuration entities, the config target is given by the entity ID. - // @todo Consider adding a method to allow entity types to indicate the - // target identifier key rather than hard-coding this check. Issue: - // https://www.drupal.org/node/2412983. - if ($entity_type instanceof ConfigEntityTypeInterface) { - $entity = $this->entityTypeManager->getStorage($entity_type_id)->load($target); - } - - // For content entities, the config target is given by the UUID. - else { - $entity = $this->loadEntityByUuid($entity_type_id, $target); - } - - return $entity; - } - - /** - * {@inheritdoc} - */ public function getEntityTypeLabels($group = FALSE) { $options = []; $definitions = $this->entityTypeManager->getDefinitions(); diff --git a/core/lib/Drupal/Core/Entity/EntityTypeRepositoryInterface.php b/core/lib/Drupal/Core/Entity/EntityTypeRepositoryInterface.php index 8c7a1d7..a3cfeb0 100644 --- a/core/lib/Drupal/Core/Entity/EntityTypeRepositoryInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityTypeRepositoryInterface.php @@ -13,45 +13,6 @@ interface EntityTypeRepositoryInterface { /** - * Loads an entity by UUID. - * - * Note that some entity types may not support UUIDs. - * - * @param string $entity_type_id - * The entity type ID to load from. - * @param string $uuid - * The UUID of the entity to load. - * - * @return \Drupal\Core\Entity\EntityInterface|null - * The entity object, or NULL if there is no entity with the given UUID. - * - * @throws \Drupal\Core\Entity\EntityStorageException - * Thrown in case the requested entity type does not support UUIDs. - */ - public function loadEntityByUuid($entity_type_id, $uuid); - - /** - * Loads an entity by the config target identifier. - * - * @param string $entity_type_id - * The entity type ID to load from. - * @param string $target - * The configuration target to load, as returned from - * \Drupal\Core\Entity\EntityInterface::getConfigTarget(). - * - * @return \Drupal\Core\Entity\EntityInterface|null - * The entity object, or NULL if there is no entity with the given config - * target identifier. - * - * @throws \Drupal\Core\Entity\EntityStorageException - * Thrown if the target identifier is a UUID but the entity type does not - * support UUIDs. - * - * @see \Drupal\Core\Entity\EntityInterface::getConfigTarget() - */ - public function loadEntityByConfigTarget($entity_type_id, $target); - - /** * Builds a list of entity type labels suitable for a Form API options list. * * @param bool $group diff --git a/core/lib/Drupal/Core/Field/FieldDefinitionListener.php b/core/lib/Drupal/Core/Field/FieldDefinitionListener.php index f19b903..ea6eebf 100644 --- a/core/lib/Drupal/Core/Field/FieldDefinitionListener.php +++ b/core/lib/Drupal/Core/Field/FieldDefinitionListener.php @@ -93,8 +93,7 @@ public function onFieldDefinitionCreate(FieldDefinitionInterface $field_definiti // If the field map is initialized, update it as well, so that calls to it // do not have to rebuild it again. - $field_map = $this->entityFieldManager->getFieldMap(); - if ($field_map) { + if ($field_map = $this->entityFieldManager->getFieldMap()) { if (!isset($field_map[$entity_type_id][$field_name])) { // This field did not exist yet, initialize it with the type and empty // bundle list. @@ -141,8 +140,7 @@ public function onFieldDefinitionDelete(FieldDefinitionInterface $field_definiti // If the field map is initialized, update it as well, so that calls to it // do not have to rebuild it again. - $field_map = $this->entityFieldManager->getFieldMap(); - if ($field_map) { + if ($field_map = $this->entityFieldManager->getFieldMap()) { unset($field_map[$entity_type_id][$field_name]['bundles'][$bundle]); if (empty($field_map[$entity_type_id][$field_name]['bundles'])) { unset($field_map[$entity_type_id][$field_name]); diff --git a/core/lib/Drupal/Core/Field/FieldStorageDefinitionListener.php b/core/lib/Drupal/Core/Field/FieldStorageDefinitionListener.php index 2e6dfc8..9070b12 100644 --- a/core/lib/Drupal/Core/Field/FieldStorageDefinitionListener.php +++ b/core/lib/Drupal/Core/Field/FieldStorageDefinitionListener.php @@ -7,7 +7,7 @@ namespace Drupal\Core\Field; -use Drupal\Core\Entity\EntityDefinitionRepositoryInterface; +use Drupal\Core\Entity\EntityInstalledDefinitionRepositoryInterface; use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -34,9 +34,9 @@ class FieldStorageDefinitionListener implements FieldStorageDefinitionListenerIn /** * The entity definition manager. * - * @var \Drupal\Core\Entity\EntityDefinitionRepositoryInterface + * @var \Drupal\Core\Entity\EntityInstalledDefinitionRepositoryInterface */ - protected $entityDefinitionRepository; + protected $entityInstalledDefinitionRepository; /** * The entity field manager. @@ -52,15 +52,15 @@ class FieldStorageDefinitionListener implements FieldStorageDefinitionListenerIn * The entity type manager. * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher * The event dispatcher. - * @param \Drupal\Core\Entity\EntityDefinitionRepositoryInterface $entity_definition_repository + * @param \Drupal\Core\Entity\EntityInstalledDefinitionRepositoryInterface $entity_installed_definition_repository * The entity definition repository. * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager * The entity field manager. */ - public function __construct(EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher, EntityDefinitionRepositoryInterface $entity_definition_repository, EntityFieldManagerInterface $entity_field_manager) { + public function __construct(EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher, EntityInstalledDefinitionRepositoryInterface $entity_installed_definition_repository, EntityFieldManagerInterface $entity_field_manager) { $this->entityTypeManager = $entity_type_manager; $this->eventDispatcher = $event_dispatcher; - $this->entityDefinitionRepository = $entity_definition_repository; + $this->entityInstalledDefinitionRepository = $entity_installed_definition_repository; $this->entityFieldManager = $entity_field_manager; } @@ -79,7 +79,7 @@ public function onFieldStorageDefinitionCreate(FieldStorageDefinitionInterface $ $this->eventDispatcher->dispatch(FieldStorageDefinitionEvents::CREATE, new FieldStorageDefinitionEvent($storage_definition)); - $this->entityDefinitionRepository->setLastInstalledFieldStorageDefinition($storage_definition); + $this->entityInstalledDefinitionRepository->setLastInstalledFieldStorageDefinition($storage_definition); $this->entityFieldManager->clearCachedFieldDefinitions(); } @@ -98,7 +98,7 @@ public function onFieldStorageDefinitionUpdate(FieldStorageDefinitionInterface $ $this->eventDispatcher->dispatch(FieldStorageDefinitionEvents::UPDATE, new FieldStorageDefinitionEvent($storage_definition, $original)); - $this->entityDefinitionRepository->setLastInstalledFieldStorageDefinition($storage_definition); + $this->entityInstalledDefinitionRepository->setLastInstalledFieldStorageDefinition($storage_definition); $this->entityFieldManager->clearCachedFieldDefinitions(); } @@ -117,7 +117,7 @@ public function onFieldStorageDefinitionDelete(FieldStorageDefinitionInterface $ $this->eventDispatcher->dispatch(FieldStorageDefinitionEvents::DELETE, new FieldStorageDefinitionEvent($storage_definition)); - $this->entityDefinitionRepository->deleteLastInstalledFieldStorageDefinition($storage_definition); + $this->entityInstalledDefinitionRepository->deleteLastInstalledFieldStorageDefinition($storage_definition); $this->entityFieldManager->clearCachedFieldDefinitions(); } diff --git a/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php index ffee4be..06af47f 100644 --- a/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php @@ -7,21 +7,18 @@ namespace Drupal\Tests\Core\Entity; -use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityBundleListenerInterface; -use Drupal\Core\Entity\EntityDefinitionRepositoryInterface; +use Drupal\Core\Entity\EntityInstalledDefinitionRepositoryInterface; use Drupal\Core\Entity\EntityDisplayManagerInterface; use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityManager; +use Drupal\Core\Entity\EntityRepositoryInterface; use Drupal\Core\Entity\EntityTypeBundleManagerInterface; use Drupal\Core\Entity\EntityTypeListenerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeRepositoryInterface; use Drupal\Core\Field\FieldDefinitionListenerInterface; use Drupal\Core\Field\FieldStorageDefinitionListenerInterface; -use Drupal\Core\Language\Language; -use Drupal\Core\Language\LanguageInterface; -use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Tests\UnitTestCase; use Prophecy\Argument; @@ -39,13 +36,6 @@ class EntityManagerTest extends UnitTestCase { protected $entityManager; /** - * The language manager. - * - * @var \Drupal\Core\Language\LanguageManagerInterface|\Prophecy\Prophecy\ProphecyInterface - */ - protected $languageManager; - - /** * The entity type manager. * * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\Prophecy\Prophecy\ProphecyInterface @@ -90,9 +80,16 @@ class EntityManagerTest extends UnitTestCase { /** * The entity definition repository. * - * @var \Drupal\Core\Entity\EntityDefinitionRepositoryInterface|\Prophecy\Prophecy\ProphecyInterface + * @var \Drupal\Core\Entity\EntityInstalledDefinitionRepositoryInterface|\Prophecy\Prophecy\ProphecyInterface */ - protected $entityDefinitionRepository; + protected $entityInstalledDefinitionRepository; + + /** + * The entity repository. + * + * @var \Drupal\Core\Entity\EntityRepositoryInterface|\Prophecy\Prophecy\ProphecyInterface + */ + protected $entityRepository; /** * The entity bundle listener. @@ -121,21 +118,20 @@ class EntityManagerTest extends UnitTestCase { protected function setUp() { parent::setUp(); - $this->languageManager = $this->prophesize(LanguageManagerInterface::class); - $this->entityTypeManager = $this->prophesize(EntityTypeManagerInterface::class); $this->entityTypeRepository = $this->prophesize(EntityTypeRepositoryInterface::class); $this->entityTypeBundleManager = $this->prophesize(EntityTypeBundleManagerInterface::class); $this->entityDisplayManager = $this->prophesize(EntityDisplayManagerInterface::class); $this->entityFieldManager = $this->prophesize(EntityFieldManagerInterface::class); $this->entityTypeListener = $this->prophesize(EntityTypeListenerInterface::class); - $this->entityDefinitionRepository = $this->prophesize(EntityDefinitionRepositoryInterface::class); + $this->entityInstalledDefinitionRepository = $this->prophesize(EntityInstalledDefinitionRepositoryInterface::class); + $this->entityRepository = $this->prophesize(EntityRepositoryInterface::class); $this->entityBundleListener = $this->prophesize(EntityBundleListenerInterface::class); $this->fieldStorageDefinitionListener = $this->prophesize(FieldStorageDefinitionListenerInterface::class); $this->fieldDefinitionListener = $this->prophesize(FieldDefinitionListenerInterface::class); - $this->entityManager = new EntityManager($this->languageManager->reveal(), $this->entityTypeManager->reveal(), $this->entityTypeRepository->reveal(), $this->entityTypeBundleManager->reveal(), $this->entityDisplayManager->reveal(), $this->entityFieldManager->reveal(), $this->entityTypeListener->reveal(), $this->entityDefinitionRepository->reveal(), $this->entityBundleListener->reveal(), $this->fieldStorageDefinitionListener->reveal(), $this->fieldDefinitionListener->reveal()); + $this->entityManager = new EntityManager($this->entityTypeManager->reveal(), $this->entityTypeRepository->reveal(), $this->entityTypeBundleManager->reveal(), $this->entityDisplayManager->reveal(), $this->entityFieldManager->reveal(), $this->entityTypeListener->reveal(), $this->entityInstalledDefinitionRepository->reveal(), $this->entityRepository->reveal(), $this->entityBundleListener->reveal(), $this->fieldStorageDefinitionListener->reveal(), $this->fieldDefinitionListener->reveal()); } /** @@ -145,44 +141,11 @@ protected function setUp() { */ public function testClearCachedDefinitions() { $this->entityTypeManager->clearCachedDefinitions()->shouldBeCalled(); + $this->entityTypeRepository->clearCachedDefinitions()->shouldBeCalled(); + $this->entityTypeBundleManager->clearCachedBundles()->shouldBeCalled(); + $this->entityFieldManager->clearCachedFieldDefinitions()->shouldBeCalled(); $this->entityManager->clearCachedDefinitions(); } - /** - * Tests the getTranslationFromContext() method. - * - * @covers ::getTranslationFromContext - */ - public function testGetTranslationFromContext() { - $language = new Language(['id' => 'en']); - $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT) - ->willReturn($language) - ->shouldBeCalledTimes(1); - $this->languageManager->getFallbackCandidates(Argument::type('array')) - ->will(function ($args) { - $context = $args[0]; - $candidates = array(); - if (!empty($context['langcode'])) { - $candidates[$context['langcode']] = $context['langcode']; - } - return $candidates; - }) - ->shouldBeCalledTimes(1); - - $translated_entity = $this->prophesize(ContentEntityInterface::class); - - $entity = $this->prophesize(ContentEntityInterface::class); - $entity->getUntranslated()->willReturn($entity); - $entity->language()->willReturn($language); - $entity->hasTranslation(LanguageInterface::LANGCODE_DEFAULT)->willReturn(FALSE); - $entity->hasTranslation('custom_langcode')->willReturn(TRUE); - $entity->getTranslation('custom_langcode')->willReturn($translated_entity->reveal()); - $entity->getTranslationLanguages()->willReturn([new Language(['id' => 'en']), new Language(['id' => 'custom_langcode'])]); - $entity->addCacheContexts(['languages:language_content'])->shouldBeCalled(); - - $this->assertSame($entity->reveal(), $this->entityManager->getTranslationFromContext($entity->reveal())); - $this->assertSame($translated_entity->reveal(), $this->entityManager->getTranslationFromContext($entity->reveal(), 'custom_langcode')); - } - } diff --git a/core/tests/Drupal/Tests/Core/Entity/EntityRepositoryTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityRepositoryTest.php new file mode 100644 index 0000000..4d718c7 --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Entity/EntityRepositoryTest.php @@ -0,0 +1,94 @@ +entityTypeManager = $this->prophesize(EntityTypeManagerInterface::class); + $this->languageManager = $this->prophesize(LanguageManagerInterface::class); + + $this->entityRepository = new EntityRepository($this->entityTypeManager->reveal(), $this->languageManager->reveal()); + } + + /** + * Tests the getTranslationFromContext() method. + * + * @covers ::getTranslationFromContext + */ + public function testGetTranslationFromContext() { + $language = new Language(['id' => 'en']); + $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT) + ->willReturn($language) + ->shouldBeCalledTimes(1); + $this->languageManager->getFallbackCandidates(Argument::type('array')) + ->will(function ($args) { + $context = $args[0]; + $candidates = array(); + if (!empty($context['langcode'])) { + $candidates[$context['langcode']] = $context['langcode']; + } + return $candidates; + }) + ->shouldBeCalledTimes(1); + + $translated_entity = $this->prophesize(ContentEntityInterface::class); + + $entity = $this->prophesize(ContentEntityInterface::class); + $entity->getUntranslated()->willReturn($entity); + $entity->language()->willReturn($language); + $entity->hasTranslation(LanguageInterface::LANGCODE_DEFAULT)->willReturn(FALSE); + $entity->hasTranslation('custom_langcode')->willReturn(TRUE); + $entity->getTranslation('custom_langcode')->willReturn($translated_entity->reveal()); + $entity->getTranslationLanguages()->willReturn([new Language(['id' => 'en']), new Language(['id' => 'custom_langcode'])]); + $entity->addCacheContexts(['languages:language_content'])->shouldBeCalled(); + + $this->assertSame($entity->reveal(), $this->entityRepository->getTranslationFromContext($entity->reveal())); + $this->assertSame($translated_entity->reveal(), $this->entityRepository->getTranslationFromContext($entity->reveal(), 'custom_langcode')); + } + +}