3 namespace Drupal\Core\ParamConverter;
5 use Drupal\Core\Entity\EntityInterface;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\Core\TypedData\TranslatableInterface;
8 use Symfony\Component\Routing\Route;
11 * Parameter converter for upcasting entity IDs to full objects.
13 * This is useful in cases where the dynamic elements of the path can't be
14 * auto-determined; for example, if your path refers to multiple of the same
15 * type of entity ("example/{node1}/foo/{node2}") or if the path can act on any
16 * entity type ("example/{entity_type}/{entity}/foo").
18 * In order to use it you should specify some additional options in your route:
28 * If you want to have the entity type itself dynamic in the url you can
29 * specify it like the following:
32 * path: foo/{entity_type}/{example}
36 * type: entity:{entity_type}
39 class EntityConverter implements ParamConverterInterface {
42 * Entity manager which performs the upcasting in the end.
44 * @var \Drupal\Core\Entity\EntityManagerInterface
46 protected $entityManager;
49 * Constructs a new EntityConverter.
51 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
54 public function __construct(EntityManagerInterface $entity_manager) {
55 $this->entityManager = $entity_manager;
61 public function convert($value, $definition, $name, array $defaults) {
62 $entity_type_id = $this->getEntityTypeFromDefaults($definition, $name, $defaults);
63 if ($storage = $this->entityManager->getStorage($entity_type_id)) {
64 $entity = $storage->load($value);
65 // If the entity type is translatable, ensure we return the proper
66 // translation object for the current context.
67 if ($entity instanceof EntityInterface && $entity instanceof TranslatableInterface) {
68 $entity = $this->entityManager->getTranslationFromContext($entity, NULL, ['operation' => 'entity_upcast']);
77 public function applies($definition, $name, Route $route) {
78 if (!empty($definition['type']) && strpos($definition['type'], 'entity:') === 0) {
79 $entity_type_id = substr($definition['type'], strlen('entity:'));
80 if (strpos($definition['type'], '{') !== FALSE) {
81 $entity_type_slug = substr($entity_type_id, 1, -1);
82 return $name != $entity_type_slug && in_array($entity_type_slug, $route->compile()->getVariables(), TRUE);
84 return $this->entityManager->hasDefinition($entity_type_id);
90 * Determines the entity type ID given a route definition and route defaults.
92 * @param mixed $definition
93 * The parameter definition provided in the route options.
95 * The name of the parameter.
96 * @param array $defaults
97 * The route defaults array.
100 * The entity type ID.
102 * @throws \Drupal\Core\ParamConverter\ParamNotConvertedException
103 * Thrown when the dynamic entity type is not found in the route defaults.
105 protected function getEntityTypeFromDefaults($definition, $name, array $defaults) {
106 $entity_type_id = substr($definition['type'], strlen('entity:'));
108 // If the entity type is dynamic, it will be pulled from the route defaults.
109 if (strpos($entity_type_id, '{') === 0) {
110 $entity_type_slug = substr($entity_type_id, 1, -1);
111 if (!isset($defaults[$entity_type_slug])) {
112 throw new ParamNotConvertedException(sprintf('The "%s" parameter was not converted because the "%s" parameter is missing', $name, $entity_type_slug));
114 $entity_type_id = $defaults[$entity_type_slug];
116 return $entity_type_id;