Chris@0: entityManager = $entity_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@0: if ($storage = $this->entityManager->getStorage($entity_type_id)) { Chris@0: $entity = $storage->load($value); Chris@0: // If the entity type is translatable, ensure we return the proper Chris@0: // translation object for the current context. Chris@0: if ($entity instanceof EntityInterface && $entity instanceof TranslatableInterface) { Chris@0: $entity = $this->entityManager->getTranslationFromContext($entity, NULL, ['operation' => 'entity_upcast']); Chris@0: } 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@0: }