entityTypeManager = $entity_type_manager; $this->entityRepository = $entity_repository; } /** * {@inheritdoc} */ public function convert($value, $definition, $name, array $defaults) { $entity_type_id = $this->getEntityTypeFromDefaults($definition, $name, $defaults); // If the entity type is revisionable and the parameter has the // "load_latest_revision" flag, load the active variant. if (!empty($definition['load_latest_revision'])) { $entity = $this->entityRepository->getActive($entity_type_id, $value); if ( !empty($definition['bundle']) && $entity instanceof EntityInterface && !in_array($entity->bundle(), $definition['bundle'], TRUE) ) { return NULL; } return $entity; } $contexts = ['operation' => 'entity_upcast']; $entity = $this->entityRepository->getCanonical($entity_type_id, $value, $contexts); if ( !empty($definition['bundle']) && $entity instanceof EntityInterface && !in_array($entity->bundle(), $definition['bundle'], TRUE) ) { return NULL; } return $entity; } /** * {@inheritdoc} */ public function applies($definition, $name, Route $route) { if (!empty($definition['type']) && str_starts_with($definition['type'], 'entity:')) { $entity_type_id = substr($definition['type'], strlen('entity:')); if (str_contains($definition['type'], '{')) { $entity_type_slug = substr($entity_type_id, 1, -1); return $name != $entity_type_slug && in_array($entity_type_slug, $route->compile()->getVariables(), TRUE); } return $this->entityTypeManager->hasDefinition($entity_type_id); } return FALSE; } }