annotate core/lib/Drupal/Core/Entity/Entity.php @ 13:5fb285c0d0e3

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