Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\Entity;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Cache\Cache;
|
Chris@0
|
6 use Drupal\Core\Cache\RefinableCacheableDependencyTrait;
|
Chris@0
|
7 use Drupal\Core\DependencyInjection\DependencySerializationTrait;
|
Chris@0
|
8 use Drupal\Component\Utility\Unicode;
|
Chris@0
|
9 use Drupal\Core\Config\Entity\Exception\ConfigEntityIdLengthException;
|
Chris@0
|
10 use Drupal\Core\Entity\Exception\UndefinedLinkTemplateException;
|
Chris@0
|
11 use Drupal\Core\Language\Language;
|
Chris@0
|
12 use Drupal\Core\Language\LanguageInterface;
|
Chris@0
|
13 use Drupal\Core\Link;
|
Chris@0
|
14 use Drupal\Core\Session\AccountInterface;
|
Chris@0
|
15 use Drupal\Core\Url;
|
Chris@0
|
16 use Symfony\Component\Routing\Exception\RouteNotFoundException;
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * Defines a base entity class.
|
Chris@0
|
20 */
|
Chris@0
|
21 abstract class Entity implements EntityInterface {
|
Chris@0
|
22
|
Chris@0
|
23 use RefinableCacheableDependencyTrait;
|
Chris@0
|
24
|
Chris@0
|
25 use DependencySerializationTrait {
|
Chris@0
|
26 __sleep as traitSleep;
|
Chris@0
|
27 }
|
Chris@0
|
28
|
Chris@0
|
29 /**
|
Chris@0
|
30 * The entity type.
|
Chris@0
|
31 *
|
Chris@0
|
32 * @var string
|
Chris@0
|
33 */
|
Chris@0
|
34 protected $entityTypeId;
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * Boolean indicating whether the entity should be forced to be new.
|
Chris@0
|
38 *
|
Chris@0
|
39 * @var bool
|
Chris@0
|
40 */
|
Chris@0
|
41 protected $enforceIsNew;
|
Chris@0
|
42
|
Chris@0
|
43 /**
|
Chris@0
|
44 * A typed data object wrapping this entity.
|
Chris@0
|
45 *
|
Chris@0
|
46 * @var \Drupal\Core\TypedData\ComplexDataInterface
|
Chris@0
|
47 */
|
Chris@0
|
48 protected $typedData;
|
Chris@0
|
49
|
Chris@0
|
50 /**
|
Chris@0
|
51 * Constructs an Entity object.
|
Chris@0
|
52 *
|
Chris@0
|
53 * @param array $values
|
Chris@0
|
54 * An array of values to set, keyed by property name. If the entity type
|
Chris@0
|
55 * has bundles, the bundle key has to be specified.
|
Chris@0
|
56 * @param string $entity_type
|
Chris@0
|
57 * The type of the entity to create.
|
Chris@0
|
58 */
|
Chris@0
|
59 public function __construct(array $values, $entity_type) {
|
Chris@0
|
60 $this->entityTypeId = $entity_type;
|
Chris@0
|
61 // Set initial values.
|
Chris@0
|
62 foreach ($values as $key => $value) {
|
Chris@0
|
63 $this->$key = $value;
|
Chris@0
|
64 }
|
Chris@0
|
65 }
|
Chris@0
|
66
|
Chris@0
|
67 /**
|
Chris@0
|
68 * Gets the entity manager.
|
Chris@0
|
69 *
|
Chris@0
|
70 * @return \Drupal\Core\Entity\EntityManagerInterface
|
Chris@0
|
71 *
|
Chris@0
|
72 * @deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0.
|
Chris@0
|
73 * Use \Drupal::entityTypeManager() instead in most cases. If the needed
|
Chris@0
|
74 * method is not on \Drupal\Core\Entity\EntityTypeManagerInterface, see the
|
Chris@0
|
75 * deprecated \Drupal\Core\Entity\EntityManager to find the
|
Chris@0
|
76 * correct interface or service.
|
Chris@0
|
77 */
|
Chris@0
|
78 protected function entityManager() {
|
Chris@0
|
79 return \Drupal::entityManager();
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 /**
|
Chris@0
|
83 * Gets the entity type manager.
|
Chris@0
|
84 *
|
Chris@0
|
85 * @return \Drupal\Core\Entity\EntityTypeManagerInterface
|
Chris@0
|
86 */
|
Chris@0
|
87 protected function entityTypeManager() {
|
Chris@0
|
88 return \Drupal::entityTypeManager();
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 /**
|
Chris@14
|
92 * Gets the entity type bundle info service.
|
Chris@14
|
93 *
|
Chris@14
|
94 * @return \Drupal\Core\Entity\EntityTypeBundleInfoInterface
|
Chris@14
|
95 */
|
Chris@14
|
96 protected function entityTypeBundleInfo() {
|
Chris@14
|
97 return \Drupal::service('entity_type.bundle.info');
|
Chris@14
|
98 }
|
Chris@14
|
99
|
Chris@14
|
100 /**
|
Chris@0
|
101 * Gets the language manager.
|
Chris@0
|
102 *
|
Chris@0
|
103 * @return \Drupal\Core\Language\LanguageManagerInterface
|
Chris@0
|
104 */
|
Chris@0
|
105 protected function languageManager() {
|
Chris@0
|
106 return \Drupal::languageManager();
|
Chris@0
|
107 }
|
Chris@0
|
108
|
Chris@0
|
109 /**
|
Chris@0
|
110 * Gets the UUID generator.
|
Chris@0
|
111 *
|
Chris@0
|
112 * @return \Drupal\Component\Uuid\UuidInterface
|
Chris@0
|
113 */
|
Chris@0
|
114 protected function uuidGenerator() {
|
Chris@0
|
115 return \Drupal::service('uuid');
|
Chris@0
|
116 }
|
Chris@0
|
117
|
Chris@0
|
118 /**
|
Chris@0
|
119 * {@inheritdoc}
|
Chris@0
|
120 */
|
Chris@0
|
121 public function id() {
|
Chris@0
|
122 return isset($this->id) ? $this->id : NULL;
|
Chris@0
|
123 }
|
Chris@0
|
124
|
Chris@0
|
125 /**
|
Chris@0
|
126 * {@inheritdoc}
|
Chris@0
|
127 */
|
Chris@0
|
128 public function uuid() {
|
Chris@0
|
129 return isset($this->uuid) ? $this->uuid : NULL;
|
Chris@0
|
130 }
|
Chris@0
|
131
|
Chris@0
|
132 /**
|
Chris@0
|
133 * {@inheritdoc}
|
Chris@0
|
134 */
|
Chris@0
|
135 public function isNew() {
|
Chris@0
|
136 return !empty($this->enforceIsNew) || !$this->id();
|
Chris@0
|
137 }
|
Chris@0
|
138
|
Chris@0
|
139 /**
|
Chris@0
|
140 * {@inheritdoc}
|
Chris@0
|
141 */
|
Chris@0
|
142 public function enforceIsNew($value = TRUE) {
|
Chris@0
|
143 $this->enforceIsNew = $value;
|
Chris@0
|
144
|
Chris@0
|
145 return $this;
|
Chris@0
|
146 }
|
Chris@0
|
147
|
Chris@0
|
148 /**
|
Chris@0
|
149 * {@inheritdoc}
|
Chris@0
|
150 */
|
Chris@0
|
151 public function getEntityTypeId() {
|
Chris@0
|
152 return $this->entityTypeId;
|
Chris@0
|
153 }
|
Chris@0
|
154
|
Chris@0
|
155 /**
|
Chris@0
|
156 * {@inheritdoc}
|
Chris@0
|
157 */
|
Chris@0
|
158 public function bundle() {
|
Chris@0
|
159 return $this->entityTypeId;
|
Chris@0
|
160 }
|
Chris@0
|
161
|
Chris@0
|
162 /**
|
Chris@0
|
163 * {@inheritdoc}
|
Chris@0
|
164 */
|
Chris@0
|
165 public function label() {
|
Chris@0
|
166 $label = NULL;
|
Chris@0
|
167 $entity_type = $this->getEntityType();
|
Chris@0
|
168 if (($label_callback = $entity_type->getLabelCallback()) && is_callable($label_callback)) {
|
Chris@0
|
169 $label = call_user_func($label_callback, $this);
|
Chris@0
|
170 }
|
Chris@0
|
171 elseif (($label_key = $entity_type->getKey('label')) && isset($this->{$label_key})) {
|
Chris@0
|
172 $label = $this->{$label_key};
|
Chris@0
|
173 }
|
Chris@0
|
174 return $label;
|
Chris@0
|
175 }
|
Chris@0
|
176
|
Chris@0
|
177 /**
|
Chris@0
|
178 * {@inheritdoc}
|
Chris@0
|
179 */
|
Chris@0
|
180 public function urlInfo($rel = 'canonical', array $options = []) {
|
Chris@0
|
181 return $this->toUrl($rel, $options);
|
Chris@0
|
182 }
|
Chris@0
|
183
|
Chris@0
|
184 /**
|
Chris@0
|
185 * {@inheritdoc}
|
Chris@0
|
186 */
|
Chris@0
|
187 public function toUrl($rel = 'canonical', array $options = []) {
|
Chris@0
|
188 if ($this->id() === NULL) {
|
Chris@0
|
189 throw new EntityMalformedException(sprintf('The "%s" entity cannot have a URI as it does not have an ID', $this->getEntityTypeId()));
|
Chris@0
|
190 }
|
Chris@0
|
191
|
Chris@0
|
192 // The links array might contain URI templates set in annotations.
|
Chris@0
|
193 $link_templates = $this->linkTemplates();
|
Chris@0
|
194
|
Chris@0
|
195 // Links pointing to the current revision point to the actual entity. So
|
Chris@0
|
196 // instead of using the 'revision' link, use the 'canonical' link.
|
Chris@0
|
197 if ($rel === 'revision' && $this instanceof RevisionableInterface && $this->isDefaultRevision()) {
|
Chris@0
|
198 $rel = 'canonical';
|
Chris@0
|
199 }
|
Chris@0
|
200
|
Chris@0
|
201 if (isset($link_templates[$rel])) {
|
Chris@0
|
202 $route_parameters = $this->urlRouteParameters($rel);
|
Chris@0
|
203 $route_name = "entity.{$this->entityTypeId}." . str_replace(['-', 'drupal:'], ['_', ''], $rel);
|
Chris@0
|
204 $uri = new Url($route_name, $route_parameters);
|
Chris@0
|
205 }
|
Chris@0
|
206 else {
|
Chris@0
|
207 $bundle = $this->bundle();
|
Chris@0
|
208 // A bundle-specific callback takes precedence over the generic one for
|
Chris@0
|
209 // the entity type.
|
Chris@14
|
210 $bundles = $this->entityTypeBundleInfo()->getBundleInfo($this->getEntityTypeId());
|
Chris@0
|
211 if (isset($bundles[$bundle]['uri_callback'])) {
|
Chris@0
|
212 $uri_callback = $bundles[$bundle]['uri_callback'];
|
Chris@0
|
213 }
|
Chris@0
|
214 elseif ($entity_uri_callback = $this->getEntityType()->getUriCallback()) {
|
Chris@0
|
215 $uri_callback = $entity_uri_callback;
|
Chris@0
|
216 }
|
Chris@0
|
217
|
Chris@0
|
218 // Invoke the callback to get the URI. If there is no callback, use the
|
Chris@0
|
219 // default URI format.
|
Chris@0
|
220 if (isset($uri_callback) && is_callable($uri_callback)) {
|
Chris@0
|
221 $uri = call_user_func($uri_callback, $this);
|
Chris@0
|
222 }
|
Chris@0
|
223 else {
|
Chris@0
|
224 throw new UndefinedLinkTemplateException("No link template '$rel' found for the '{$this->getEntityTypeId()}' entity type");
|
Chris@0
|
225 }
|
Chris@0
|
226 }
|
Chris@0
|
227
|
Chris@0
|
228 // Pass the entity data through as options, so that alter functions do not
|
Chris@0
|
229 // need to look up this entity again.
|
Chris@0
|
230 $uri
|
Chris@0
|
231 ->setOption('entity_type', $this->getEntityTypeId())
|
Chris@0
|
232 ->setOption('entity', $this);
|
Chris@0
|
233
|
Chris@0
|
234 // Display links by default based on the current language.
|
Chris@0
|
235 // Link relations that do not require an existing entity should not be
|
Chris@0
|
236 // affected by this entity's language, however.
|
Chris@0
|
237 if (!in_array($rel, ['collection', 'add-page', 'add-form'], TRUE)) {
|
Chris@0
|
238 $options += ['language' => $this->language()];
|
Chris@0
|
239 }
|
Chris@0
|
240
|
Chris@0
|
241 $uri_options = $uri->getOptions();
|
Chris@0
|
242 $uri_options += $options;
|
Chris@0
|
243
|
Chris@0
|
244 return $uri->setOptions($uri_options);
|
Chris@0
|
245 }
|
Chris@0
|
246
|
Chris@0
|
247 /**
|
Chris@0
|
248 * {@inheritdoc}
|
Chris@0
|
249 */
|
Chris@0
|
250 public function hasLinkTemplate($rel) {
|
Chris@0
|
251 $link_templates = $this->linkTemplates();
|
Chris@0
|
252 return isset($link_templates[$rel]);
|
Chris@0
|
253 }
|
Chris@0
|
254
|
Chris@0
|
255 /**
|
Chris@0
|
256 * Gets an array link templates.
|
Chris@0
|
257 *
|
Chris@0
|
258 * @return array
|
Chris@0
|
259 * An array of link templates containing paths.
|
Chris@0
|
260 */
|
Chris@0
|
261 protected function linkTemplates() {
|
Chris@0
|
262 return $this->getEntityType()->getLinkTemplates();
|
Chris@0
|
263 }
|
Chris@0
|
264
|
Chris@0
|
265 /**
|
Chris@0
|
266 * {@inheritdoc}
|
Chris@0
|
267 */
|
Chris@0
|
268 public function link($text = NULL, $rel = 'canonical', array $options = []) {
|
Chris@0
|
269 return $this->toLink($text, $rel, $options)->toString();
|
Chris@0
|
270 }
|
Chris@0
|
271
|
Chris@0
|
272 /**
|
Chris@0
|
273 * {@inheritdoc}
|
Chris@0
|
274 */
|
Chris@0
|
275 public function toLink($text = NULL, $rel = 'canonical', array $options = []) {
|
Chris@0
|
276 if (!isset($text)) {
|
Chris@0
|
277 $text = $this->label();
|
Chris@0
|
278 }
|
Chris@0
|
279 $url = $this->toUrl($rel);
|
Chris@0
|
280 $options += $url->getOptions();
|
Chris@0
|
281 $url->setOptions($options);
|
Chris@0
|
282 return new Link($text, $url);
|
Chris@0
|
283 }
|
Chris@0
|
284
|
Chris@0
|
285 /**
|
Chris@0
|
286 * {@inheritdoc}
|
Chris@0
|
287 */
|
Chris@0
|
288 public function url($rel = 'canonical', $options = []) {
|
Chris@0
|
289 // While self::toUrl() will throw an exception if the entity has no id,
|
Chris@0
|
290 // the expected result for a URL is always a string.
|
Chris@0
|
291 if ($this->id() === NULL || !$this->hasLinkTemplate($rel)) {
|
Chris@0
|
292 return '';
|
Chris@0
|
293 }
|
Chris@0
|
294
|
Chris@0
|
295 $uri = $this->toUrl($rel);
|
Chris@0
|
296 $options += $uri->getOptions();
|
Chris@0
|
297 $uri->setOptions($options);
|
Chris@0
|
298 return $uri->toString();
|
Chris@0
|
299 }
|
Chris@0
|
300
|
Chris@0
|
301 /**
|
Chris@0
|
302 * Gets an array of placeholders for this entity.
|
Chris@0
|
303 *
|
Chris@0
|
304 * Individual entity classes may override this method to add additional
|
Chris@0
|
305 * placeholders if desired. If so, they should be sure to replicate the
|
Chris@0
|
306 * property caching logic.
|
Chris@0
|
307 *
|
Chris@0
|
308 * @param string $rel
|
Chris@0
|
309 * The link relationship type, for example: canonical or edit-form.
|
Chris@0
|
310 *
|
Chris@0
|
311 * @return array
|
Chris@0
|
312 * An array of URI placeholders.
|
Chris@0
|
313 */
|
Chris@0
|
314 protected function urlRouteParameters($rel) {
|
Chris@0
|
315 $uri_route_parameters = [];
|
Chris@0
|
316
|
Chris@0
|
317 if (!in_array($rel, ['collection', 'add-page', 'add-form'], TRUE)) {
|
Chris@0
|
318 // The entity ID is needed as a route parameter.
|
Chris@0
|
319 $uri_route_parameters[$this->getEntityTypeId()] = $this->id();
|
Chris@0
|
320 }
|
Chris@0
|
321 if ($rel === 'add-form' && ($this->getEntityType()->hasKey('bundle'))) {
|
Chris@0
|
322 $parameter_name = $this->getEntityType()->getBundleEntityType() ?: $this->getEntityType()->getKey('bundle');
|
Chris@0
|
323 $uri_route_parameters[$parameter_name] = $this->bundle();
|
Chris@0
|
324 }
|
Chris@0
|
325 if ($rel === 'revision' && $this instanceof RevisionableInterface) {
|
Chris@0
|
326 $uri_route_parameters[$this->getEntityTypeId() . '_revision'] = $this->getRevisionId();
|
Chris@0
|
327 }
|
Chris@0
|
328
|
Chris@0
|
329 return $uri_route_parameters;
|
Chris@0
|
330 }
|
Chris@0
|
331
|
Chris@0
|
332 /**
|
Chris@0
|
333 * {@inheritdoc}
|
Chris@0
|
334 */
|
Chris@0
|
335 public function uriRelationships() {
|
Chris@0
|
336 return array_filter(array_keys($this->linkTemplates()), function ($link_relation_type) {
|
Chris@0
|
337 // It's not guaranteed that every link relation type also has a
|
Chris@0
|
338 // corresponding route. For some, additional modules or configuration may
|
Chris@0
|
339 // be necessary. The interface demands that we only return supported URI
|
Chris@0
|
340 // relationships.
|
Chris@0
|
341 try {
|
Chris@0
|
342 $this->toUrl($link_relation_type)->toString(TRUE)->getGeneratedUrl();
|
Chris@0
|
343 }
|
Chris@0
|
344 catch (RouteNotFoundException $e) {
|
Chris@0
|
345 return FALSE;
|
Chris@0
|
346 }
|
Chris@0
|
347 return TRUE;
|
Chris@0
|
348 });
|
Chris@0
|
349 }
|
Chris@0
|
350
|
Chris@0
|
351 /**
|
Chris@0
|
352 * {@inheritdoc}
|
Chris@0
|
353 */
|
Chris@0
|
354 public function access($operation, AccountInterface $account = NULL, $return_as_object = FALSE) {
|
Chris@0
|
355 if ($operation == 'create') {
|
Chris@14
|
356 return $this->entityTypeManager()
|
Chris@0
|
357 ->getAccessControlHandler($this->entityTypeId)
|
Chris@0
|
358 ->createAccess($this->bundle(), $account, [], $return_as_object);
|
Chris@0
|
359 }
|
Chris@14
|
360 return $this->entityTypeManager()
|
Chris@0
|
361 ->getAccessControlHandler($this->entityTypeId)
|
Chris@0
|
362 ->access($this, $operation, $account, $return_as_object);
|
Chris@0
|
363 }
|
Chris@0
|
364
|
Chris@0
|
365 /**
|
Chris@0
|
366 * {@inheritdoc}
|
Chris@0
|
367 */
|
Chris@0
|
368 public function language() {
|
Chris@0
|
369 if ($key = $this->getEntityType()->getKey('langcode')) {
|
Chris@0
|
370 $langcode = $this->$key;
|
Chris@0
|
371 $language = $this->languageManager()->getLanguage($langcode);
|
Chris@0
|
372 if ($language) {
|
Chris@0
|
373 return $language;
|
Chris@0
|
374 }
|
Chris@0
|
375 }
|
Chris@0
|
376 // Make sure we return a proper language object.
|
Chris@0
|
377 $langcode = !empty($this->langcode) ? $this->langcode : LanguageInterface::LANGCODE_NOT_SPECIFIED;
|
Chris@0
|
378 $language = new Language(['id' => $langcode]);
|
Chris@0
|
379 return $language;
|
Chris@0
|
380 }
|
Chris@0
|
381
|
Chris@0
|
382 /**
|
Chris@0
|
383 * {@inheritdoc}
|
Chris@0
|
384 */
|
Chris@0
|
385 public function save() {
|
Chris@14
|
386 $storage = $this->entityTypeManager()->getStorage($this->entityTypeId);
|
Chris@14
|
387 return $storage->save($this);
|
Chris@0
|
388 }
|
Chris@0
|
389
|
Chris@0
|
390 /**
|
Chris@0
|
391 * {@inheritdoc}
|
Chris@0
|
392 */
|
Chris@0
|
393 public function delete() {
|
Chris@0
|
394 if (!$this->isNew()) {
|
Chris@14
|
395 $this->entityTypeManager()->getStorage($this->entityTypeId)->delete([$this->id() => $this]);
|
Chris@0
|
396 }
|
Chris@0
|
397 }
|
Chris@0
|
398
|
Chris@0
|
399 /**
|
Chris@0
|
400 * {@inheritdoc}
|
Chris@0
|
401 */
|
Chris@0
|
402 public function createDuplicate() {
|
Chris@0
|
403 $duplicate = clone $this;
|
Chris@0
|
404 $entity_type = $this->getEntityType();
|
Chris@0
|
405 // Reset the entity ID and indicate that this is a new entity.
|
Chris@0
|
406 $duplicate->{$entity_type->getKey('id')} = NULL;
|
Chris@0
|
407 $duplicate->enforceIsNew();
|
Chris@0
|
408
|
Chris@0
|
409 // Check if the entity type supports UUIDs and generate a new one if so.
|
Chris@0
|
410 if ($entity_type->hasKey('uuid')) {
|
Chris@0
|
411 $duplicate->{$entity_type->getKey('uuid')} = $this->uuidGenerator()->generate();
|
Chris@0
|
412 }
|
Chris@0
|
413 return $duplicate;
|
Chris@0
|
414 }
|
Chris@0
|
415
|
Chris@0
|
416 /**
|
Chris@0
|
417 * {@inheritdoc}
|
Chris@0
|
418 */
|
Chris@0
|
419 public function getEntityType() {
|
Chris@14
|
420 return $this->entityTypeManager()->getDefinition($this->getEntityTypeId());
|
Chris@0
|
421 }
|
Chris@0
|
422
|
Chris@0
|
423 /**
|
Chris@0
|
424 * {@inheritdoc}
|
Chris@0
|
425 */
|
Chris@0
|
426 public function preSave(EntityStorageInterface $storage) {
|
Chris@0
|
427 // Check if this is an entity bundle.
|
Chris@0
|
428 if ($this->getEntityType()->getBundleOf()) {
|
Chris@0
|
429 // Throw an exception if the bundle ID is longer than 32 characters.
|
Chris@0
|
430 if (Unicode::strlen($this->id()) > EntityTypeInterface::BUNDLE_MAX_LENGTH) {
|
Chris@0
|
431 throw new ConfigEntityIdLengthException("Attempt to create a bundle with an ID longer than " . EntityTypeInterface::BUNDLE_MAX_LENGTH . " characters: $this->id().");
|
Chris@0
|
432 }
|
Chris@0
|
433 }
|
Chris@0
|
434 }
|
Chris@0
|
435
|
Chris@0
|
436 /**
|
Chris@0
|
437 * {@inheritdoc}
|
Chris@0
|
438 */
|
Chris@0
|
439 public function postSave(EntityStorageInterface $storage, $update = TRUE) {
|
Chris@0
|
440 $this->invalidateTagsOnSave($update);
|
Chris@0
|
441 }
|
Chris@0
|
442
|
Chris@0
|
443 /**
|
Chris@0
|
444 * {@inheritdoc}
|
Chris@0
|
445 */
|
Chris@0
|
446 public static function preCreate(EntityStorageInterface $storage, array &$values) {
|
Chris@0
|
447 }
|
Chris@0
|
448
|
Chris@0
|
449 /**
|
Chris@0
|
450 * {@inheritdoc}
|
Chris@0
|
451 */
|
Chris@0
|
452 public function postCreate(EntityStorageInterface $storage) {
|
Chris@0
|
453 }
|
Chris@0
|
454
|
Chris@0
|
455 /**
|
Chris@0
|
456 * {@inheritdoc}
|
Chris@0
|
457 */
|
Chris@0
|
458 public static function preDelete(EntityStorageInterface $storage, array $entities) {
|
Chris@0
|
459 }
|
Chris@0
|
460
|
Chris@0
|
461 /**
|
Chris@0
|
462 * {@inheritdoc}
|
Chris@0
|
463 */
|
Chris@0
|
464 public static function postDelete(EntityStorageInterface $storage, array $entities) {
|
Chris@0
|
465 static::invalidateTagsOnDelete($storage->getEntityType(), $entities);
|
Chris@0
|
466 }
|
Chris@0
|
467
|
Chris@0
|
468 /**
|
Chris@0
|
469 * {@inheritdoc}
|
Chris@0
|
470 */
|
Chris@0
|
471 public static function postLoad(EntityStorageInterface $storage, array &$entities) {
|
Chris@0
|
472 }
|
Chris@0
|
473
|
Chris@0
|
474 /**
|
Chris@0
|
475 * {@inheritdoc}
|
Chris@0
|
476 */
|
Chris@0
|
477 public function referencedEntities() {
|
Chris@0
|
478 return [];
|
Chris@0
|
479 }
|
Chris@0
|
480
|
Chris@0
|
481 /**
|
Chris@0
|
482 * {@inheritdoc}
|
Chris@0
|
483 */
|
Chris@0
|
484 public function getCacheContexts() {
|
Chris@0
|
485 return $this->cacheContexts;
|
Chris@0
|
486 }
|
Chris@0
|
487
|
Chris@0
|
488 /**
|
Chris@0
|
489 * {@inheritdoc}
|
Chris@0
|
490 */
|
Chris@0
|
491 public function getCacheTagsToInvalidate() {
|
Chris@0
|
492 // @todo Add bundle-specific listing cache tag?
|
Chris@0
|
493 // https://www.drupal.org/node/2145751
|
Chris@0
|
494 if ($this->isNew()) {
|
Chris@0
|
495 return [];
|
Chris@0
|
496 }
|
Chris@0
|
497 return [$this->entityTypeId . ':' . $this->id()];
|
Chris@0
|
498 }
|
Chris@0
|
499
|
Chris@0
|
500 /**
|
Chris@0
|
501 * {@inheritdoc}
|
Chris@0
|
502 */
|
Chris@0
|
503 public function getCacheTags() {
|
Chris@0
|
504 if ($this->cacheTags) {
|
Chris@0
|
505 return Cache::mergeTags($this->getCacheTagsToInvalidate(), $this->cacheTags);
|
Chris@0
|
506 }
|
Chris@0
|
507 return $this->getCacheTagsToInvalidate();
|
Chris@0
|
508 }
|
Chris@0
|
509
|
Chris@0
|
510 /**
|
Chris@0
|
511 * {@inheritdoc}
|
Chris@0
|
512 */
|
Chris@0
|
513 public function getCacheMaxAge() {
|
Chris@0
|
514 return $this->cacheMaxAge;
|
Chris@0
|
515 }
|
Chris@0
|
516
|
Chris@0
|
517 /**
|
Chris@0
|
518 * {@inheritdoc}
|
Chris@0
|
519 */
|
Chris@0
|
520 public static function load($id) {
|
Chris@14
|
521 $entity_type_repository = \Drupal::service('entity_type.repository');
|
Chris@14
|
522 $entity_type_manager = \Drupal::entityTypeManager();
|
Chris@14
|
523 $storage = $entity_type_manager->getStorage($entity_type_repository->getEntityTypeFromClass(get_called_class()));
|
Chris@14
|
524 return $storage->load($id);
|
Chris@0
|
525 }
|
Chris@0
|
526
|
Chris@0
|
527 /**
|
Chris@0
|
528 * {@inheritdoc}
|
Chris@0
|
529 */
|
Chris@0
|
530 public static function loadMultiple(array $ids = NULL) {
|
Chris@14
|
531 $entity_type_repository = \Drupal::service('entity_type.repository');
|
Chris@14
|
532 $entity_type_manager = \Drupal::entityTypeManager();
|
Chris@14
|
533 $storage = $entity_type_manager->getStorage($entity_type_repository->getEntityTypeFromClass(get_called_class()));
|
Chris@14
|
534 return $storage->loadMultiple($ids);
|
Chris@0
|
535 }
|
Chris@0
|
536
|
Chris@0
|
537 /**
|
Chris@0
|
538 * {@inheritdoc}
|
Chris@0
|
539 */
|
Chris@0
|
540 public static function create(array $values = []) {
|
Chris@14
|
541 $entity_type_repository = \Drupal::service('entity_type.repository');
|
Chris@14
|
542 $entity_type_manager = \Drupal::entityTypeManager();
|
Chris@14
|
543 $storage = $entity_type_manager->getStorage($entity_type_repository->getEntityTypeFromClass(get_called_class()));
|
Chris@14
|
544 return $storage->create($values);
|
Chris@0
|
545 }
|
Chris@0
|
546
|
Chris@0
|
547 /**
|
Chris@0
|
548 * Invalidates an entity's cache tags upon save.
|
Chris@0
|
549 *
|
Chris@0
|
550 * @param bool $update
|
Chris@0
|
551 * TRUE if the entity has been updated, or FALSE if it has been inserted.
|
Chris@0
|
552 */
|
Chris@0
|
553 protected function invalidateTagsOnSave($update) {
|
Chris@0
|
554 // An entity was created or updated: invalidate its list cache tags. (An
|
Chris@0
|
555 // updated entity may start to appear in a listing because it now meets that
|
Chris@0
|
556 // listing's filtering requirements. A newly created entity may start to
|
Chris@0
|
557 // appear in listings because it did not exist before.)
|
Chris@0
|
558 $tags = $this->getEntityType()->getListCacheTags();
|
Chris@0
|
559 if ($this->hasLinkTemplate('canonical')) {
|
Chris@0
|
560 // Creating or updating an entity may change a cached 403 or 404 response.
|
Chris@0
|
561 $tags = Cache::mergeTags($tags, ['4xx-response']);
|
Chris@0
|
562 }
|
Chris@0
|
563 if ($update) {
|
Chris@0
|
564 // An existing entity was updated, also invalidate its unique cache tag.
|
Chris@0
|
565 $tags = Cache::mergeTags($tags, $this->getCacheTagsToInvalidate());
|
Chris@0
|
566 }
|
Chris@0
|
567 Cache::invalidateTags($tags);
|
Chris@0
|
568 }
|
Chris@0
|
569
|
Chris@0
|
570 /**
|
Chris@0
|
571 * Invalidates an entity's cache tags upon delete.
|
Chris@0
|
572 *
|
Chris@0
|
573 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
|
Chris@0
|
574 * The entity type definition.
|
Chris@0
|
575 * @param \Drupal\Core\Entity\EntityInterface[] $entities
|
Chris@0
|
576 * An array of entities.
|
Chris@0
|
577 */
|
Chris@0
|
578 protected static function invalidateTagsOnDelete(EntityTypeInterface $entity_type, array $entities) {
|
Chris@0
|
579 $tags = $entity_type->getListCacheTags();
|
Chris@0
|
580 foreach ($entities as $entity) {
|
Chris@0
|
581 // An entity was deleted: invalidate its own cache tag, but also its list
|
Chris@0
|
582 // cache tags. (A deleted entity may cause changes in a paged list on
|
Chris@0
|
583 // other pages than the one it's on. The one it's on is handled by its own
|
Chris@0
|
584 // cache tag, but subsequent list pages would not be invalidated, hence we
|
Chris@0
|
585 // must invalidate its list cache tags as well.)
|
Chris@0
|
586 $tags = Cache::mergeTags($tags, $entity->getCacheTagsToInvalidate());
|
Chris@0
|
587 }
|
Chris@0
|
588 Cache::invalidateTags($tags);
|
Chris@0
|
589 }
|
Chris@0
|
590
|
Chris@0
|
591 /**
|
Chris@0
|
592 * {@inheritdoc}
|
Chris@0
|
593 */
|
Chris@0
|
594 public function getOriginalId() {
|
Chris@0
|
595 // By default, entities do not support renames and do not have original IDs.
|
Chris@0
|
596 return NULL;
|
Chris@0
|
597 }
|
Chris@0
|
598
|
Chris@0
|
599 /**
|
Chris@0
|
600 * {@inheritdoc}
|
Chris@0
|
601 */
|
Chris@0
|
602 public function setOriginalId($id) {
|
Chris@0
|
603 // By default, entities do not support renames and do not have original IDs.
|
Chris@0
|
604 // If the specified ID is anything except NULL, this should mark this entity
|
Chris@0
|
605 // as no longer new.
|
Chris@0
|
606 if ($id !== NULL) {
|
Chris@0
|
607 $this->enforceIsNew(FALSE);
|
Chris@0
|
608 }
|
Chris@0
|
609
|
Chris@0
|
610 return $this;
|
Chris@0
|
611 }
|
Chris@0
|
612
|
Chris@0
|
613 /**
|
Chris@0
|
614 * {@inheritdoc}
|
Chris@0
|
615 */
|
Chris@0
|
616 public function toArray() {
|
Chris@0
|
617 return [];
|
Chris@0
|
618 }
|
Chris@0
|
619
|
Chris@0
|
620 /**
|
Chris@0
|
621 * {@inheritdoc}
|
Chris@0
|
622 */
|
Chris@0
|
623 public function getTypedData() {
|
Chris@0
|
624 if (!isset($this->typedData)) {
|
Chris@0
|
625 $class = \Drupal::typedDataManager()->getDefinition('entity')['class'];
|
Chris@0
|
626 $this->typedData = $class::createFromEntity($this);
|
Chris@0
|
627 }
|
Chris@0
|
628 return $this->typedData;
|
Chris@0
|
629 }
|
Chris@0
|
630
|
Chris@0
|
631 /**
|
Chris@0
|
632 * {@inheritdoc}
|
Chris@0
|
633 */
|
Chris@0
|
634 public function __sleep() {
|
Chris@0
|
635 $this->typedData = NULL;
|
Chris@0
|
636 return $this->traitSleep();
|
Chris@0
|
637 }
|
Chris@0
|
638
|
Chris@0
|
639 /**
|
Chris@0
|
640 * {@inheritdoc}
|
Chris@0
|
641 */
|
Chris@0
|
642 public function getConfigDependencyKey() {
|
Chris@0
|
643 return $this->getEntityType()->getConfigDependencyKey();
|
Chris@0
|
644 }
|
Chris@0
|
645
|
Chris@0
|
646 /**
|
Chris@0
|
647 * {@inheritdoc}
|
Chris@0
|
648 */
|
Chris@0
|
649 public function getConfigDependencyName() {
|
Chris@0
|
650 return $this->getEntityTypeId() . ':' . $this->bundle() . ':' . $this->uuid();
|
Chris@0
|
651 }
|
Chris@0
|
652
|
Chris@0
|
653 /**
|
Chris@0
|
654 * {@inheritdoc}
|
Chris@0
|
655 */
|
Chris@0
|
656 public function getConfigTarget() {
|
Chris@0
|
657 // For content entities, use the UUID for the config target identifier.
|
Chris@0
|
658 // This ensures that references to the target can be deployed reliably.
|
Chris@0
|
659 return $this->uuid();
|
Chris@0
|
660 }
|
Chris@0
|
661
|
Chris@0
|
662 }
|