annotate core/lib/Drupal/Core/Entity/Entity.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents c2387f117808
children af1871eacc83
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\Core\Config\Entity\Exception\ConfigEntityIdLengthException;
Chris@0 9 use Drupal\Core\Entity\Exception\UndefinedLinkTemplateException;
Chris@0 10 use Drupal\Core\Language\Language;
Chris@0 11 use Drupal\Core\Language\LanguageInterface;
Chris@0 12 use Drupal\Core\Link;
Chris@0 13 use Drupal\Core\Session\AccountInterface;
Chris@0 14 use Drupal\Core\Url;
Chris@16 15 use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
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@16 347 catch (MissingMandatoryParametersException $e) {
Chris@16 348 return FALSE;
Chris@16 349 }
Chris@0 350 return TRUE;
Chris@0 351 });
Chris@0 352 }
Chris@0 353
Chris@0 354 /**
Chris@0 355 * {@inheritdoc}
Chris@0 356 */
Chris@0 357 public function access($operation, AccountInterface $account = NULL, $return_as_object = FALSE) {
Chris@0 358 if ($operation == 'create') {
Chris@14 359 return $this->entityTypeManager()
Chris@0 360 ->getAccessControlHandler($this->entityTypeId)
Chris@0 361 ->createAccess($this->bundle(), $account, [], $return_as_object);
Chris@0 362 }
Chris@14 363 return $this->entityTypeManager()
Chris@0 364 ->getAccessControlHandler($this->entityTypeId)
Chris@0 365 ->access($this, $operation, $account, $return_as_object);
Chris@0 366 }
Chris@0 367
Chris@0 368 /**
Chris@0 369 * {@inheritdoc}
Chris@0 370 */
Chris@0 371 public function language() {
Chris@0 372 if ($key = $this->getEntityType()->getKey('langcode')) {
Chris@0 373 $langcode = $this->$key;
Chris@0 374 $language = $this->languageManager()->getLanguage($langcode);
Chris@0 375 if ($language) {
Chris@0 376 return $language;
Chris@0 377 }
Chris@0 378 }
Chris@0 379 // Make sure we return a proper language object.
Chris@0 380 $langcode = !empty($this->langcode) ? $this->langcode : LanguageInterface::LANGCODE_NOT_SPECIFIED;
Chris@0 381 $language = new Language(['id' => $langcode]);
Chris@0 382 return $language;
Chris@0 383 }
Chris@0 384
Chris@0 385 /**
Chris@0 386 * {@inheritdoc}
Chris@0 387 */
Chris@0 388 public function save() {
Chris@14 389 $storage = $this->entityTypeManager()->getStorage($this->entityTypeId);
Chris@14 390 return $storage->save($this);
Chris@0 391 }
Chris@0 392
Chris@0 393 /**
Chris@0 394 * {@inheritdoc}
Chris@0 395 */
Chris@0 396 public function delete() {
Chris@0 397 if (!$this->isNew()) {
Chris@14 398 $this->entityTypeManager()->getStorage($this->entityTypeId)->delete([$this->id() => $this]);
Chris@0 399 }
Chris@0 400 }
Chris@0 401
Chris@0 402 /**
Chris@0 403 * {@inheritdoc}
Chris@0 404 */
Chris@0 405 public function createDuplicate() {
Chris@0 406 $duplicate = clone $this;
Chris@0 407 $entity_type = $this->getEntityType();
Chris@0 408 // Reset the entity ID and indicate that this is a new entity.
Chris@0 409 $duplicate->{$entity_type->getKey('id')} = NULL;
Chris@0 410 $duplicate->enforceIsNew();
Chris@0 411
Chris@0 412 // Check if the entity type supports UUIDs and generate a new one if so.
Chris@0 413 if ($entity_type->hasKey('uuid')) {
Chris@0 414 $duplicate->{$entity_type->getKey('uuid')} = $this->uuidGenerator()->generate();
Chris@0 415 }
Chris@0 416 return $duplicate;
Chris@0 417 }
Chris@0 418
Chris@0 419 /**
Chris@0 420 * {@inheritdoc}
Chris@0 421 */
Chris@0 422 public function getEntityType() {
Chris@14 423 return $this->entityTypeManager()->getDefinition($this->getEntityTypeId());
Chris@0 424 }
Chris@0 425
Chris@0 426 /**
Chris@0 427 * {@inheritdoc}
Chris@0 428 */
Chris@0 429 public function preSave(EntityStorageInterface $storage) {
Chris@0 430 // Check if this is an entity bundle.
Chris@0 431 if ($this->getEntityType()->getBundleOf()) {
Chris@0 432 // Throw an exception if the bundle ID is longer than 32 characters.
Chris@17 433 if (mb_strlen($this->id()) > EntityTypeInterface::BUNDLE_MAX_LENGTH) {
Chris@0 434 throw new ConfigEntityIdLengthException("Attempt to create a bundle with an ID longer than " . EntityTypeInterface::BUNDLE_MAX_LENGTH . " characters: $this->id().");
Chris@0 435 }
Chris@0 436 }
Chris@0 437 }
Chris@0 438
Chris@0 439 /**
Chris@0 440 * {@inheritdoc}
Chris@0 441 */
Chris@0 442 public function postSave(EntityStorageInterface $storage, $update = TRUE) {
Chris@0 443 $this->invalidateTagsOnSave($update);
Chris@0 444 }
Chris@0 445
Chris@0 446 /**
Chris@0 447 * {@inheritdoc}
Chris@0 448 */
Chris@0 449 public static function preCreate(EntityStorageInterface $storage, array &$values) {
Chris@0 450 }
Chris@0 451
Chris@0 452 /**
Chris@0 453 * {@inheritdoc}
Chris@0 454 */
Chris@0 455 public function postCreate(EntityStorageInterface $storage) {
Chris@0 456 }
Chris@0 457
Chris@0 458 /**
Chris@0 459 * {@inheritdoc}
Chris@0 460 */
Chris@0 461 public static function preDelete(EntityStorageInterface $storage, array $entities) {
Chris@0 462 }
Chris@0 463
Chris@0 464 /**
Chris@0 465 * {@inheritdoc}
Chris@0 466 */
Chris@0 467 public static function postDelete(EntityStorageInterface $storage, array $entities) {
Chris@0 468 static::invalidateTagsOnDelete($storage->getEntityType(), $entities);
Chris@0 469 }
Chris@0 470
Chris@0 471 /**
Chris@0 472 * {@inheritdoc}
Chris@0 473 */
Chris@0 474 public static function postLoad(EntityStorageInterface $storage, array &$entities) {
Chris@0 475 }
Chris@0 476
Chris@0 477 /**
Chris@0 478 * {@inheritdoc}
Chris@0 479 */
Chris@0 480 public function referencedEntities() {
Chris@0 481 return [];
Chris@0 482 }
Chris@0 483
Chris@0 484 /**
Chris@0 485 * {@inheritdoc}
Chris@0 486 */
Chris@0 487 public function getCacheContexts() {
Chris@0 488 return $this->cacheContexts;
Chris@0 489 }
Chris@0 490
Chris@0 491 /**
Chris@0 492 * {@inheritdoc}
Chris@0 493 */
Chris@0 494 public function getCacheTagsToInvalidate() {
Chris@0 495 // @todo Add bundle-specific listing cache tag?
Chris@0 496 // https://www.drupal.org/node/2145751
Chris@0 497 if ($this->isNew()) {
Chris@0 498 return [];
Chris@0 499 }
Chris@0 500 return [$this->entityTypeId . ':' . $this->id()];
Chris@0 501 }
Chris@0 502
Chris@0 503 /**
Chris@0 504 * {@inheritdoc}
Chris@0 505 */
Chris@0 506 public function getCacheTags() {
Chris@0 507 if ($this->cacheTags) {
Chris@0 508 return Cache::mergeTags($this->getCacheTagsToInvalidate(), $this->cacheTags);
Chris@0 509 }
Chris@0 510 return $this->getCacheTagsToInvalidate();
Chris@0 511 }
Chris@0 512
Chris@0 513 /**
Chris@0 514 * {@inheritdoc}
Chris@0 515 */
Chris@0 516 public function getCacheMaxAge() {
Chris@0 517 return $this->cacheMaxAge;
Chris@0 518 }
Chris@0 519
Chris@0 520 /**
Chris@0 521 * {@inheritdoc}
Chris@0 522 */
Chris@0 523 public static function load($id) {
Chris@14 524 $entity_type_repository = \Drupal::service('entity_type.repository');
Chris@14 525 $entity_type_manager = \Drupal::entityTypeManager();
Chris@14 526 $storage = $entity_type_manager->getStorage($entity_type_repository->getEntityTypeFromClass(get_called_class()));
Chris@14 527 return $storage->load($id);
Chris@0 528 }
Chris@0 529
Chris@0 530 /**
Chris@0 531 * {@inheritdoc}
Chris@0 532 */
Chris@0 533 public static function loadMultiple(array $ids = NULL) {
Chris@14 534 $entity_type_repository = \Drupal::service('entity_type.repository');
Chris@14 535 $entity_type_manager = \Drupal::entityTypeManager();
Chris@14 536 $storage = $entity_type_manager->getStorage($entity_type_repository->getEntityTypeFromClass(get_called_class()));
Chris@14 537 return $storage->loadMultiple($ids);
Chris@0 538 }
Chris@0 539
Chris@0 540 /**
Chris@0 541 * {@inheritdoc}
Chris@0 542 */
Chris@0 543 public static function create(array $values = []) {
Chris@14 544 $entity_type_repository = \Drupal::service('entity_type.repository');
Chris@14 545 $entity_type_manager = \Drupal::entityTypeManager();
Chris@14 546 $storage = $entity_type_manager->getStorage($entity_type_repository->getEntityTypeFromClass(get_called_class()));
Chris@14 547 return $storage->create($values);
Chris@0 548 }
Chris@0 549
Chris@0 550 /**
Chris@0 551 * Invalidates an entity's cache tags upon save.
Chris@0 552 *
Chris@0 553 * @param bool $update
Chris@0 554 * TRUE if the entity has been updated, or FALSE if it has been inserted.
Chris@0 555 */
Chris@0 556 protected function invalidateTagsOnSave($update) {
Chris@0 557 // An entity was created or updated: invalidate its list cache tags. (An
Chris@0 558 // updated entity may start to appear in a listing because it now meets that
Chris@0 559 // listing's filtering requirements. A newly created entity may start to
Chris@0 560 // appear in listings because it did not exist before.)
Chris@0 561 $tags = $this->getEntityType()->getListCacheTags();
Chris@0 562 if ($this->hasLinkTemplate('canonical')) {
Chris@0 563 // Creating or updating an entity may change a cached 403 or 404 response.
Chris@0 564 $tags = Cache::mergeTags($tags, ['4xx-response']);
Chris@0 565 }
Chris@0 566 if ($update) {
Chris@0 567 // An existing entity was updated, also invalidate its unique cache tag.
Chris@0 568 $tags = Cache::mergeTags($tags, $this->getCacheTagsToInvalidate());
Chris@0 569 }
Chris@0 570 Cache::invalidateTags($tags);
Chris@0 571 }
Chris@0 572
Chris@0 573 /**
Chris@0 574 * Invalidates an entity's cache tags upon delete.
Chris@0 575 *
Chris@0 576 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
Chris@0 577 * The entity type definition.
Chris@0 578 * @param \Drupal\Core\Entity\EntityInterface[] $entities
Chris@0 579 * An array of entities.
Chris@0 580 */
Chris@0 581 protected static function invalidateTagsOnDelete(EntityTypeInterface $entity_type, array $entities) {
Chris@0 582 $tags = $entity_type->getListCacheTags();
Chris@0 583 foreach ($entities as $entity) {
Chris@0 584 // An entity was deleted: invalidate its own cache tag, but also its list
Chris@0 585 // cache tags. (A deleted entity may cause changes in a paged list on
Chris@0 586 // other pages than the one it's on. The one it's on is handled by its own
Chris@0 587 // cache tag, but subsequent list pages would not be invalidated, hence we
Chris@0 588 // must invalidate its list cache tags as well.)
Chris@0 589 $tags = Cache::mergeTags($tags, $entity->getCacheTagsToInvalidate());
Chris@0 590 }
Chris@0 591 Cache::invalidateTags($tags);
Chris@0 592 }
Chris@0 593
Chris@0 594 /**
Chris@0 595 * {@inheritdoc}
Chris@0 596 */
Chris@0 597 public function getOriginalId() {
Chris@0 598 // By default, entities do not support renames and do not have original IDs.
Chris@0 599 return NULL;
Chris@0 600 }
Chris@0 601
Chris@0 602 /**
Chris@0 603 * {@inheritdoc}
Chris@0 604 */
Chris@0 605 public function setOriginalId($id) {
Chris@0 606 // By default, entities do not support renames and do not have original IDs.
Chris@0 607 // If the specified ID is anything except NULL, this should mark this entity
Chris@0 608 // as no longer new.
Chris@0 609 if ($id !== NULL) {
Chris@0 610 $this->enforceIsNew(FALSE);
Chris@0 611 }
Chris@0 612
Chris@0 613 return $this;
Chris@0 614 }
Chris@0 615
Chris@0 616 /**
Chris@0 617 * {@inheritdoc}
Chris@0 618 */
Chris@0 619 public function toArray() {
Chris@0 620 return [];
Chris@0 621 }
Chris@0 622
Chris@0 623 /**
Chris@0 624 * {@inheritdoc}
Chris@0 625 */
Chris@0 626 public function getTypedData() {
Chris@0 627 if (!isset($this->typedData)) {
Chris@0 628 $class = \Drupal::typedDataManager()->getDefinition('entity')['class'];
Chris@0 629 $this->typedData = $class::createFromEntity($this);
Chris@0 630 }
Chris@0 631 return $this->typedData;
Chris@0 632 }
Chris@0 633
Chris@0 634 /**
Chris@0 635 * {@inheritdoc}
Chris@0 636 */
Chris@0 637 public function __sleep() {
Chris@0 638 $this->typedData = NULL;
Chris@0 639 return $this->traitSleep();
Chris@0 640 }
Chris@0 641
Chris@0 642 /**
Chris@0 643 * {@inheritdoc}
Chris@0 644 */
Chris@0 645 public function getConfigDependencyKey() {
Chris@0 646 return $this->getEntityType()->getConfigDependencyKey();
Chris@0 647 }
Chris@0 648
Chris@0 649 /**
Chris@0 650 * {@inheritdoc}
Chris@0 651 */
Chris@0 652 public function getConfigDependencyName() {
Chris@0 653 return $this->getEntityTypeId() . ':' . $this->bundle() . ':' . $this->uuid();
Chris@0 654 }
Chris@0 655
Chris@0 656 /**
Chris@0 657 * {@inheritdoc}
Chris@0 658 */
Chris@0 659 public function getConfigTarget() {
Chris@0 660 // For content entities, use the UUID for the config target identifier.
Chris@0 661 // This ensures that references to the target can be deployed reliably.
Chris@0 662 return $this->uuid();
Chris@0 663 }
Chris@0 664
Chris@0 665 }