Chris@0: 'entity.manager', Chris@18: 'languageManager' => 'language_manager', Chris@18: ]; Chris@18: Chris@18: /** Chris@18: * Entity type manager which performs the upcasting in the end. Chris@14: * Chris@18: * @var \Drupal\Core\Entity\EntityTypeManagerInterface Chris@14: */ Chris@18: protected $entityTypeManager; Chris@18: Chris@18: /** Chris@18: * Entity repository. Chris@18: * Chris@18: * @var \Drupal\Core\Entity\EntityRepositoryInterface Chris@18: */ Chris@18: protected $entityRepository; Chris@14: Chris@14: /** Chris@0: * Constructs a new EntityConverter. Chris@0: * Chris@18: * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager Chris@18: * The entity type manager. Chris@18: * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository Chris@18: * The entity repository. Chris@18: * Chris@18: * @see https://www.drupal.org/node/2549139 Chris@18: * @see https://www.drupal.org/node/2938929 Chris@0: */ Chris@18: public function __construct(EntityTypeManagerInterface $entity_type_manager, $entity_repository = NULL) { Chris@18: if ($entity_type_manager instanceof EntityManagerInterface) { Chris@18: @trigger_error('Passing the entity.manager service to EntityConverter::__construct() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Pass the entity_type.manager service instead. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); Chris@18: } Chris@18: $this->entityTypeManager = $entity_type_manager; Chris@18: Chris@18: if (!($entity_repository instanceof EntityRepositoryInterface)) { Chris@18: @trigger_error('Calling EntityConverter::__construct() with the $entity_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); Chris@18: $entity_repository = \Drupal::service('entity.repository'); Chris@18: } Chris@18: $this->entityRepository = $entity_repository; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function convert($value, $definition, $name, array $defaults) { Chris@0: $entity_type_id = $this->getEntityTypeFromDefaults($definition, $name, $defaults); Chris@14: Chris@14: // If the entity type is revisionable and the parameter has the Chris@18: // "load_latest_revision" flag, load the active variant. Chris@18: if (!empty($definition['load_latest_revision'])) { Chris@18: return $this->entityRepository->getActive($entity_type_id, $value); Chris@14: } Chris@14: Chris@18: // Do not inject the context repository as it is not an actual dependency: Chris@18: // it will be removed once both the TODOs below are fixed. Chris@18: /** @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface $contexts_repository */ Chris@18: $contexts_repository = \Drupal::service('context.repository'); Chris@18: // @todo Consider deprecating the legacy context operation altogether in Chris@18: // https://www.drupal.org/node/3031124. Chris@18: $contexts = $contexts_repository->getAvailableContexts(); Chris@18: $contexts[EntityRepositoryInterface::CONTEXT_ID_LEGACY_CONTEXT_OPERATION] = Chris@18: new Context(new ContextDefinition('string'), 'entity_upcast'); Chris@18: // @todo At the moment we do not need the current user context, which is Chris@18: // triggering some test failures. We can remove these lines once Chris@18: // https://www.drupal.org/node/2934192 is fixed. Chris@18: $context_id = '@user.current_user_context:current_user'; Chris@18: if (isset($contexts[$context_id])) { Chris@18: $account = $contexts[$context_id]->getContextValue(); Chris@18: unset($account->_skipProtectedUserFieldConstraint); Chris@18: unset($contexts[$context_id]); Chris@14: } Chris@18: $entity = $this->entityRepository->getCanonical($entity_type_id, $value, $contexts); Chris@14: Chris@14: return $entity; Chris@14: } Chris@14: Chris@14: /** Chris@18: * Returns the latest revision translation of the specified entity. Chris@14: * Chris@18: * @param \Drupal\Core\Entity\RevisionableInterface $entity Chris@14: * The default revision of the entity being converted. Chris@14: * @param string $langcode Chris@14: * The language of the revision translation to be loaded. Chris@14: * Chris@18: * @return \Drupal\Core\Entity\RevisionableInterface Chris@14: * The latest translation-affecting revision for the specified entity, or Chris@14: * just the latest revision, if the specified entity is not translatable or Chris@14: * does not have a matching translation yet. Chris@18: * Chris@18: * @deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Chris@18: * Use \Drupal\Core\Entity\EntityRepositoryInterface::getActive() instead. Chris@14: */ Chris@14: protected function getLatestTranslationAffectedRevision(RevisionableInterface $entity, $langcode) { Chris@18: @trigger_error('\Drupal\Core\ParamConverter\EntityConverter::getLatestTranslationAffectedRevision() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityRepositoryInterface::getActive() instead.', E_USER_DEPRECATED); Chris@18: $data_type = 'language'; Chris@18: $context_id_prefix = '@language.current_language_context:'; Chris@18: $contexts = [ Chris@18: $context_id_prefix . LanguageInterface::TYPE_CONTENT => new Context(new ContextDefinition($data_type), $langcode), Chris@18: $context_id_prefix . LanguageInterface::TYPE_INTERFACE => new Context(new ContextDefinition($data_type), $langcode), Chris@18: ]; Chris@18: $revision = $this->entityRepository->getActive($entity->getEntityTypeId(), $entity->id(), $contexts); Chris@18: // The EntityRepositoryInterface::getActive() method performs entity Chris@18: // translation negotiation, but this used to return an untranslated entity Chris@18: // object as translation negotiation happened later in ::convert(). Chris@18: if ($revision instanceof TranslatableInterface) { Chris@18: $revision = $revision->getUntranslated(); Chris@14: } Chris@14: return $revision; Chris@14: } Chris@14: Chris@14: /** Chris@14: * Loads the specified entity revision. Chris@14: * Chris@18: * @param \Drupal\Core\Entity\RevisionableInterface $entity Chris@14: * The default revision of the entity being converted. Chris@14: * @param string $revision_id Chris@14: * The identifier of the revision to be loaded. Chris@14: * Chris@18: * @return \Drupal\Core\Entity\RevisionableInterface Chris@14: * An entity revision object. Chris@18: * Chris@18: * @deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Chris@14: */ Chris@14: protected function loadRevision(RevisionableInterface $entity, $revision_id) { Chris@18: @trigger_error('\Drupal\Core\ParamConverter\EntityConverter::loadRevision() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0.', E_USER_DEPRECATED); Chris@14: // We explicitly perform a loose equality check, since a revision ID may Chris@14: // be returned as an integer or a string. Chris@14: if ($entity->getLoadedRevisionId() != $revision_id) { Chris@18: /** @var \Drupal\Core\Entity\RevisionableStorageInterface $storage */ Chris@18: $storage = $this->entityTypeManager->getStorage($entity->getEntityTypeId()); Chris@14: return $storage->loadRevision($revision_id); Chris@14: } Chris@14: else { Chris@0: return $entity; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function applies($definition, $name, Route $route) { Chris@0: if (!empty($definition['type']) && strpos($definition['type'], 'entity:') === 0) { Chris@0: $entity_type_id = substr($definition['type'], strlen('entity:')); Chris@0: if (strpos($definition['type'], '{') !== FALSE) { Chris@0: $entity_type_slug = substr($entity_type_id, 1, -1); Chris@0: return $name != $entity_type_slug && in_array($entity_type_slug, $route->compile()->getVariables(), TRUE); Chris@0: } Chris@18: return $this->entityTypeManager->hasDefinition($entity_type_id); Chris@0: } Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: /** Chris@14: * Returns a language manager instance. Chris@14: * Chris@14: * @return \Drupal\Core\Language\LanguageManagerInterface Chris@14: * The language manager. Chris@14: * Chris@14: * @internal Chris@14: */ Chris@14: protected function languageManager() { Chris@18: return $this->__get('languageManager'); Chris@14: } Chris@14: Chris@0: }