Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\ParamConverter;
|
Chris@0
|
4
|
Chris@18
|
5 use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
|
Chris@0
|
6 use Drupal\Core\Entity\EntityManagerInterface;
|
Chris@18
|
7 use Drupal\Core\Entity\EntityRepositoryInterface;
|
Chris@18
|
8 use Drupal\Core\Entity\EntityTypeManagerInterface;
|
Chris@14
|
9 use Drupal\Core\Entity\RevisionableInterface;
|
Chris@14
|
10 use Drupal\Core\Language\LanguageInterface;
|
Chris@18
|
11 use Drupal\Core\Plugin\Context\Context;
|
Chris@18
|
12 use Drupal\Core\Plugin\Context\ContextDefinition;
|
Chris@0
|
13 use Drupal\Core\TypedData\TranslatableInterface;
|
Chris@0
|
14 use Symfony\Component\Routing\Route;
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * Parameter converter for upcasting entity IDs to full objects.
|
Chris@0
|
18 *
|
Chris@0
|
19 * This is useful in cases where the dynamic elements of the path can't be
|
Chris@0
|
20 * auto-determined; for example, if your path refers to multiple of the same
|
Chris@0
|
21 * type of entity ("example/{node1}/foo/{node2}") or if the path can act on any
|
Chris@0
|
22 * entity type ("example/{entity_type}/{entity}/foo").
|
Chris@0
|
23 *
|
Chris@0
|
24 * In order to use it you should specify some additional options in your route:
|
Chris@0
|
25 * @code
|
Chris@0
|
26 * example.route:
|
Chris@0
|
27 * path: foo/{example}
|
Chris@0
|
28 * options:
|
Chris@0
|
29 * parameters:
|
Chris@0
|
30 * example:
|
Chris@0
|
31 * type: entity:node
|
Chris@0
|
32 * @endcode
|
Chris@0
|
33 *
|
Chris@0
|
34 * If you want to have the entity type itself dynamic in the url you can
|
Chris@0
|
35 * specify it like the following:
|
Chris@0
|
36 * @code
|
Chris@0
|
37 * example.route:
|
Chris@0
|
38 * path: foo/{entity_type}/{example}
|
Chris@0
|
39 * options:
|
Chris@0
|
40 * parameters:
|
Chris@0
|
41 * example:
|
Chris@0
|
42 * type: entity:{entity_type}
|
Chris@0
|
43 * @endcode
|
Chris@14
|
44 *
|
Chris@14
|
45 * If your route needs to support pending revisions, you can specify the
|
Chris@14
|
46 * "load_latest_revision" parameter. This will ensure that the latest revision
|
Chris@14
|
47 * is returned, even if it is not the default one:
|
Chris@14
|
48 * @code
|
Chris@14
|
49 * example.route:
|
Chris@14
|
50 * path: foo/{example}
|
Chris@14
|
51 * options:
|
Chris@14
|
52 * parameters:
|
Chris@14
|
53 * example:
|
Chris@14
|
54 * type: entity:node
|
Chris@14
|
55 * load_latest_revision: TRUE
|
Chris@14
|
56 * @endcode
|
Chris@14
|
57 *
|
Chris@14
|
58 * When dealing with translatable entities, the "load_latest_revision" flag will
|
Chris@14
|
59 * make this converter load the latest revision affecting the translation
|
Chris@14
|
60 * matching the content language for the current request. If none can be found
|
Chris@14
|
61 * it will fall back to the latest revision. For instance, if an entity has an
|
Chris@14
|
62 * English default revision (revision 1) and an Italian pending revision
|
Chris@14
|
63 * (revision 2), "/foo/1" will return the former, while "/it/foo/1" will return
|
Chris@14
|
64 * the latter.
|
Chris@14
|
65 *
|
Chris@14
|
66 * @see entities_revisions_translations
|
Chris@0
|
67 */
|
Chris@0
|
68 class EntityConverter implements ParamConverterInterface {
|
Chris@0
|
69
|
Chris@18
|
70 use DeprecatedServicePropertyTrait;
|
Chris@18
|
71 use DynamicEntityTypeParamConverterTrait;
|
Chris@0
|
72
|
Chris@0
|
73 /**
|
Chris@18
|
74 * {@inheritdoc}
|
Chris@18
|
75 */
|
Chris@18
|
76 protected $deprecatedProperties = [
|
Chris@18
|
77 'entityManager' => 'entity.manager',
|
Chris@18
|
78 'languageManager' => 'language_manager',
|
Chris@18
|
79 ];
|
Chris@18
|
80
|
Chris@18
|
81 /**
|
Chris@18
|
82 * Entity type manager which performs the upcasting in the end.
|
Chris@14
|
83 *
|
Chris@18
|
84 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
|
Chris@14
|
85 */
|
Chris@18
|
86 protected $entityTypeManager;
|
Chris@18
|
87
|
Chris@18
|
88 /**
|
Chris@18
|
89 * Entity repository.
|
Chris@18
|
90 *
|
Chris@18
|
91 * @var \Drupal\Core\Entity\EntityRepositoryInterface
|
Chris@18
|
92 */
|
Chris@18
|
93 protected $entityRepository;
|
Chris@14
|
94
|
Chris@14
|
95 /**
|
Chris@0
|
96 * Constructs a new EntityConverter.
|
Chris@0
|
97 *
|
Chris@18
|
98 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
|
Chris@18
|
99 * The entity type manager.
|
Chris@18
|
100 * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
|
Chris@18
|
101 * The entity repository.
|
Chris@18
|
102 *
|
Chris@18
|
103 * @see https://www.drupal.org/node/2549139
|
Chris@18
|
104 * @see https://www.drupal.org/node/2938929
|
Chris@0
|
105 */
|
Chris@18
|
106 public function __construct(EntityTypeManagerInterface $entity_type_manager, $entity_repository = NULL) {
|
Chris@18
|
107 if ($entity_type_manager instanceof EntityManagerInterface) {
|
Chris@18
|
108 @trigger_error('Passing the entity.manager service to EntityConverter::__construct() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Pass the entity_type.manager service instead. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
|
Chris@18
|
109 }
|
Chris@18
|
110 $this->entityTypeManager = $entity_type_manager;
|
Chris@18
|
111
|
Chris@18
|
112 if (!($entity_repository instanceof EntityRepositoryInterface)) {
|
Chris@18
|
113 @trigger_error('Calling EntityConverter::__construct() with the $entity_repository argument is supported in drupal:8.7.0 and will be required before drupal:9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
|
Chris@18
|
114 $entity_repository = \Drupal::service('entity.repository');
|
Chris@18
|
115 }
|
Chris@18
|
116 $this->entityRepository = $entity_repository;
|
Chris@0
|
117 }
|
Chris@0
|
118
|
Chris@0
|
119 /**
|
Chris@0
|
120 * {@inheritdoc}
|
Chris@0
|
121 */
|
Chris@0
|
122 public function convert($value, $definition, $name, array $defaults) {
|
Chris@0
|
123 $entity_type_id = $this->getEntityTypeFromDefaults($definition, $name, $defaults);
|
Chris@14
|
124
|
Chris@14
|
125 // If the entity type is revisionable and the parameter has the
|
Chris@18
|
126 // "load_latest_revision" flag, load the active variant.
|
Chris@18
|
127 if (!empty($definition['load_latest_revision'])) {
|
Chris@18
|
128 return $this->entityRepository->getActive($entity_type_id, $value);
|
Chris@14
|
129 }
|
Chris@14
|
130
|
Chris@18
|
131 // Do not inject the context repository as it is not an actual dependency:
|
Chris@18
|
132 // it will be removed once both the TODOs below are fixed.
|
Chris@18
|
133 /** @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface $contexts_repository */
|
Chris@18
|
134 $contexts_repository = \Drupal::service('context.repository');
|
Chris@18
|
135 // @todo Consider deprecating the legacy context operation altogether in
|
Chris@18
|
136 // https://www.drupal.org/node/3031124.
|
Chris@18
|
137 $contexts = $contexts_repository->getAvailableContexts();
|
Chris@18
|
138 $contexts[EntityRepositoryInterface::CONTEXT_ID_LEGACY_CONTEXT_OPERATION] =
|
Chris@18
|
139 new Context(new ContextDefinition('string'), 'entity_upcast');
|
Chris@18
|
140 // @todo At the moment we do not need the current user context, which is
|
Chris@18
|
141 // triggering some test failures. We can remove these lines once
|
Chris@18
|
142 // https://www.drupal.org/node/2934192 is fixed.
|
Chris@18
|
143 $context_id = '@user.current_user_context:current_user';
|
Chris@18
|
144 if (isset($contexts[$context_id])) {
|
Chris@18
|
145 $account = $contexts[$context_id]->getContextValue();
|
Chris@18
|
146 unset($account->_skipProtectedUserFieldConstraint);
|
Chris@18
|
147 unset($contexts[$context_id]);
|
Chris@14
|
148 }
|
Chris@18
|
149 $entity = $this->entityRepository->getCanonical($entity_type_id, $value, $contexts);
|
Chris@14
|
150
|
Chris@14
|
151 return $entity;
|
Chris@14
|
152 }
|
Chris@14
|
153
|
Chris@14
|
154 /**
|
Chris@18
|
155 * Returns the latest revision translation of the specified entity.
|
Chris@14
|
156 *
|
Chris@18
|
157 * @param \Drupal\Core\Entity\RevisionableInterface $entity
|
Chris@14
|
158 * The default revision of the entity being converted.
|
Chris@14
|
159 * @param string $langcode
|
Chris@14
|
160 * The language of the revision translation to be loaded.
|
Chris@14
|
161 *
|
Chris@18
|
162 * @return \Drupal\Core\Entity\RevisionableInterface
|
Chris@14
|
163 * The latest translation-affecting revision for the specified entity, or
|
Chris@14
|
164 * just the latest revision, if the specified entity is not translatable or
|
Chris@14
|
165 * does not have a matching translation yet.
|
Chris@18
|
166 *
|
Chris@18
|
167 * @deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0.
|
Chris@18
|
168 * Use \Drupal\Core\Entity\EntityRepositoryInterface::getActive() instead.
|
Chris@14
|
169 */
|
Chris@14
|
170 protected function getLatestTranslationAffectedRevision(RevisionableInterface $entity, $langcode) {
|
Chris@18
|
171 @trigger_error('\Drupal\Core\ParamConverter\EntityConverter::getLatestTranslationAffectedRevision() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityRepositoryInterface::getActive() instead.', E_USER_DEPRECATED);
|
Chris@18
|
172 $data_type = 'language';
|
Chris@18
|
173 $context_id_prefix = '@language.current_language_context:';
|
Chris@18
|
174 $contexts = [
|
Chris@18
|
175 $context_id_prefix . LanguageInterface::TYPE_CONTENT => new Context(new ContextDefinition($data_type), $langcode),
|
Chris@18
|
176 $context_id_prefix . LanguageInterface::TYPE_INTERFACE => new Context(new ContextDefinition($data_type), $langcode),
|
Chris@18
|
177 ];
|
Chris@18
|
178 $revision = $this->entityRepository->getActive($entity->getEntityTypeId(), $entity->id(), $contexts);
|
Chris@18
|
179 // The EntityRepositoryInterface::getActive() method performs entity
|
Chris@18
|
180 // translation negotiation, but this used to return an untranslated entity
|
Chris@18
|
181 // object as translation negotiation happened later in ::convert().
|
Chris@18
|
182 if ($revision instanceof TranslatableInterface) {
|
Chris@18
|
183 $revision = $revision->getUntranslated();
|
Chris@14
|
184 }
|
Chris@14
|
185 return $revision;
|
Chris@14
|
186 }
|
Chris@14
|
187
|
Chris@14
|
188 /**
|
Chris@14
|
189 * Loads the specified entity revision.
|
Chris@14
|
190 *
|
Chris@18
|
191 * @param \Drupal\Core\Entity\RevisionableInterface $entity
|
Chris@14
|
192 * The default revision of the entity being converted.
|
Chris@14
|
193 * @param string $revision_id
|
Chris@14
|
194 * The identifier of the revision to be loaded.
|
Chris@14
|
195 *
|
Chris@18
|
196 * @return \Drupal\Core\Entity\RevisionableInterface
|
Chris@14
|
197 * An entity revision object.
|
Chris@18
|
198 *
|
Chris@18
|
199 * @deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0.
|
Chris@14
|
200 */
|
Chris@14
|
201 protected function loadRevision(RevisionableInterface $entity, $revision_id) {
|
Chris@18
|
202 @trigger_error('\Drupal\Core\ParamConverter\EntityConverter::loadRevision() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0.', E_USER_DEPRECATED);
|
Chris@14
|
203 // We explicitly perform a loose equality check, since a revision ID may
|
Chris@14
|
204 // be returned as an integer or a string.
|
Chris@14
|
205 if ($entity->getLoadedRevisionId() != $revision_id) {
|
Chris@18
|
206 /** @var \Drupal\Core\Entity\RevisionableStorageInterface $storage */
|
Chris@18
|
207 $storage = $this->entityTypeManager->getStorage($entity->getEntityTypeId());
|
Chris@14
|
208 return $storage->loadRevision($revision_id);
|
Chris@14
|
209 }
|
Chris@14
|
210 else {
|
Chris@0
|
211 return $entity;
|
Chris@0
|
212 }
|
Chris@0
|
213 }
|
Chris@0
|
214
|
Chris@0
|
215 /**
|
Chris@0
|
216 * {@inheritdoc}
|
Chris@0
|
217 */
|
Chris@0
|
218 public function applies($definition, $name, Route $route) {
|
Chris@0
|
219 if (!empty($definition['type']) && strpos($definition['type'], 'entity:') === 0) {
|
Chris@0
|
220 $entity_type_id = substr($definition['type'], strlen('entity:'));
|
Chris@0
|
221 if (strpos($definition['type'], '{') !== FALSE) {
|
Chris@0
|
222 $entity_type_slug = substr($entity_type_id, 1, -1);
|
Chris@0
|
223 return $name != $entity_type_slug && in_array($entity_type_slug, $route->compile()->getVariables(), TRUE);
|
Chris@0
|
224 }
|
Chris@18
|
225 return $this->entityTypeManager->hasDefinition($entity_type_id);
|
Chris@0
|
226 }
|
Chris@0
|
227 return FALSE;
|
Chris@0
|
228 }
|
Chris@0
|
229
|
Chris@0
|
230 /**
|
Chris@14
|
231 * Returns a language manager instance.
|
Chris@14
|
232 *
|
Chris@14
|
233 * @return \Drupal\Core\Language\LanguageManagerInterface
|
Chris@14
|
234 * The language manager.
|
Chris@14
|
235 *
|
Chris@14
|
236 * @internal
|
Chris@14
|
237 */
|
Chris@14
|
238 protected function languageManager() {
|
Chris@18
|
239 return $this->__get('languageManager');
|
Chris@14
|
240 }
|
Chris@14
|
241
|
Chris@0
|
242 }
|