annotate core/lib/Drupal/Core/Entity/Entity.php @ 16:c2387f117808

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