annotate core/lib/Drupal/Core/Entity/EntityBase.php @ 5:12f9dff5fda9 tip

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