Chris@0: entityManager = $entity_manager; Chris@14: $this->languageManager = $language_manager; 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: $storage = $this->entityManager->getStorage($entity_type_id); Chris@14: $entity_definition = $this->entityManager->getDefinition($entity_type_id); Chris@14: Chris@14: $entity = $storage->load($value); Chris@14: Chris@14: // If the entity type is revisionable and the parameter has the Chris@14: // "load_latest_revision" flag, load the latest revision. Chris@14: if ($entity instanceof RevisionableInterface && !empty($definition['load_latest_revision']) && $entity_definition->isRevisionable()) { Chris@14: // Retrieve the latest revision ID taking translations into account. Chris@14: $langcode = $this->languageManager() Chris@14: ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT) Chris@14: ->getId(); Chris@14: $entity = $this->getLatestTranslationAffectedRevision($entity, $langcode); Chris@14: } Chris@14: Chris@14: // If the entity type is translatable, ensure we return the proper Chris@14: // translation object for the current context. Chris@14: if ($entity instanceof EntityInterface && $entity instanceof TranslatableInterface) { Chris@14: $entity = $this->entityManager->getTranslationFromContext($entity, NULL, ['operation' => 'entity_upcast']); Chris@14: } Chris@14: Chris@14: return $entity; Chris@14: } Chris@14: Chris@14: /** Chris@14: * Returns the ID of the latest revision translation of the specified entity. Chris@14: * Chris@14: * @param \Drupal\Core\Entity\EntityInterface|\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@14: * @return \Drupal\Core\Entity\EntityInterface|\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@14: */ Chris@14: protected function getLatestTranslationAffectedRevision(RevisionableInterface $entity, $langcode) { Chris@14: $revision = NULL; Chris@14: $storage = $this->entityManager->getStorage($entity->getEntityTypeId()); Chris@14: Chris@14: if ($entity instanceof TranslatableRevisionableInterface && $entity->isTranslatable()) { Chris@14: /** @var \Drupal\Core\Entity\TranslatableRevisionableStorageInterface $storage */ Chris@14: $revision_id = $storage->getLatestTranslationAffectedRevisionId($entity->id(), $langcode); Chris@14: Chris@14: // If the latest translation-affecting revision was a default revision, it Chris@14: // is fine to load the latest revision instead, because in this case the Chris@14: // latest revision, regardless of it being default or pending, will always Chris@14: // contain the most up-to-date values for the specified translation. This Chris@14: // provides a BC behavior when the route is defined by a module always Chris@14: // expecting the latest revision to be loaded and to be the default Chris@14: // revision. In this particular case the latest revision is always going Chris@14: // to be the default revision, since pending revisions would not be Chris@14: // supported. Chris@14: /** @var \Drupal\Core\Entity\TranslatableRevisionableInterface $revision */ Chris@14: $revision = $revision_id ? $this->loadRevision($entity, $revision_id) : NULL; Chris@14: if (!$revision || ($revision->wasDefaultRevision() && !$revision->isDefaultRevision())) { Chris@14: $revision = NULL; Chris@0: } Chris@14: } Chris@14: Chris@14: // Fall back to the latest revisions if no affected revision for the current Chris@14: // content language could be found. This is acceptable as it means the Chris@14: // entity is not translated. This is the correct logic also on monolingual Chris@14: // sites. Chris@14: if (!isset($revision)) { Chris@14: $revision_id = $storage->getLatestRevisionId($entity->id()); Chris@14: $revision = $this->loadRevision($entity, $revision_id); Chris@14: } Chris@14: Chris@14: return $revision; Chris@14: } Chris@14: Chris@14: /** Chris@14: * Loads the specified entity revision. Chris@14: * Chris@14: * @param \Drupal\Core\Entity\EntityInterface|\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@14: * @return \Drupal\Core\Entity\EntityInterface|\Drupal\Core\Entity\RevisionableInterface Chris@14: * An entity revision object. Chris@14: */ Chris@14: protected function loadRevision(RevisionableInterface $entity, $revision_id) { 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@14: $storage = $this->entityManager->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@0: return $this->entityManager->hasDefinition($entity_type_id); Chris@0: } Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Determines the entity type ID given a route definition and route defaults. Chris@0: * Chris@0: * @param mixed $definition Chris@0: * The parameter definition provided in the route options. Chris@0: * @param string $name Chris@0: * The name of the parameter. Chris@0: * @param array $defaults Chris@0: * The route defaults array. Chris@0: * Chris@0: * @return string Chris@0: * The entity type ID. Chris@0: * Chris@0: * @throws \Drupal\Core\ParamConverter\ParamNotConvertedException Chris@0: * Thrown when the dynamic entity type is not found in the route defaults. Chris@0: */ Chris@0: protected function getEntityTypeFromDefaults($definition, $name, array $defaults) { Chris@0: $entity_type_id = substr($definition['type'], strlen('entity:')); Chris@0: Chris@0: // If the entity type is dynamic, it will be pulled from the route defaults. Chris@0: if (strpos($entity_type_id, '{') === 0) { Chris@0: $entity_type_slug = substr($entity_type_id, 1, -1); Chris@0: if (!isset($defaults[$entity_type_slug])) { Chris@0: throw new ParamNotConvertedException(sprintf('The "%s" parameter was not converted because the "%s" parameter is missing', $name, $entity_type_slug)); Chris@0: } Chris@0: $entity_type_id = $defaults[$entity_type_slug]; Chris@0: } Chris@0: return $entity_type_id; Chris@0: } Chris@0: Chris@14: /** 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@14: if (!isset($this->languageManager)) { Chris@14: $this->languageManager = \Drupal::languageManager(); Chris@14: // @todo Turn this into a proper error (E_USER_ERROR) in Chris@14: // https://www.drupal.org/node/2938929. Chris@14: @trigger_error('The language manager parameter has been added to EntityConverter since version 8.5.0 and will be made required in version 9.0.0 when requesting the latest translation-affected revision of an entity.', E_USER_DEPRECATED); Chris@14: } Chris@14: return $this->languageManager; Chris@14: } Chris@14: Chris@0: }