comparison core/lib/Drupal/Core/ParamConverter/EntityConverter.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Core\ParamConverter;
4
5 use Drupal\Core\Entity\EntityInterface;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\Core\TypedData\TranslatableInterface;
8 use Symfony\Component\Routing\Route;
9
10 /**
11 * Parameter converter for upcasting entity IDs to full objects.
12 *
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").
17 *
18 * In order to use it you should specify some additional options in your route:
19 * @code
20 * example.route:
21 * path: foo/{example}
22 * options:
23 * parameters:
24 * example:
25 * type: entity:node
26 * @endcode
27 *
28 * If you want to have the entity type itself dynamic in the url you can
29 * specify it like the following:
30 * @code
31 * example.route:
32 * path: foo/{entity_type}/{example}
33 * options:
34 * parameters:
35 * example:
36 * type: entity:{entity_type}
37 * @endcode
38 */
39 class EntityConverter implements ParamConverterInterface {
40
41 /**
42 * Entity manager which performs the upcasting in the end.
43 *
44 * @var \Drupal\Core\Entity\EntityManagerInterface
45 */
46 protected $entityManager;
47
48 /**
49 * Constructs a new EntityConverter.
50 *
51 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
52 * The entity manager.
53 */
54 public function __construct(EntityManagerInterface $entity_manager) {
55 $this->entityManager = $entity_manager;
56 }
57
58 /**
59 * {@inheritdoc}
60 */
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']);
69 }
70 return $entity;
71 }
72 }
73
74 /**
75 * {@inheritdoc}
76 */
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);
83 }
84 return $this->entityManager->hasDefinition($entity_type_id);
85 }
86 return FALSE;
87 }
88
89 /**
90 * Determines the entity type ID given a route definition and route defaults.
91 *
92 * @param mixed $definition
93 * The parameter definition provided in the route options.
94 * @param string $name
95 * The name of the parameter.
96 * @param array $defaults
97 * The route defaults array.
98 *
99 * @return string
100 * The entity type ID.
101 *
102 * @throws \Drupal\Core\ParamConverter\ParamNotConvertedException
103 * Thrown when the dynamic entity type is not found in the route defaults.
104 */
105 protected function getEntityTypeFromDefaults($definition, $name, array $defaults) {
106 $entity_type_id = substr($definition['type'], strlen('entity:'));
107
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));
113 }
114 $entity_type_id = $defaults[$entity_type_slug];
115 }
116 return $entity_type_id;
117 }
118
119 }