3 namespace Drupal\Core\Entity;
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Cache\RefinableCacheableDependencyTrait;
7 use Drupal\Core\DependencyInjection\DependencySerializationTrait;
8 use Drupal\Component\Utility\Unicode;
9 use Drupal\Core\Config\Entity\Exception\ConfigEntityIdLengthException;
10 use Drupal\Core\Entity\Exception\UndefinedLinkTemplateException;
11 use Drupal\Core\Language\Language;
12 use Drupal\Core\Language\LanguageInterface;
14 use Drupal\Core\Session\AccountInterface;
18 * Defines a base entity class.
20 abstract class Entity implements EntityInterface {
22 use RefinableCacheableDependencyTrait;
24 use DependencySerializationTrait {
25 __sleep as traitSleep;
33 protected $entityTypeId;
36 * Boolean indicating whether the entity should be forced to be new.
40 protected $enforceIsNew;
43 * A typed data object wrapping this entity.
45 * @var \Drupal\Core\TypedData\ComplexDataInterface
50 * Constructs an Entity object.
52 * @param array $values
53 * An array of values to set, keyed by property name. If the entity type
54 * has bundles, the bundle key has to be specified.
55 * @param string $entity_type
56 * The type of the entity to create.
58 public function __construct(array $values, $entity_type) {
59 $this->entityTypeId = $entity_type;
60 // Set initial values.
61 foreach ($values as $key => $value) {
67 * Gets the entity manager.
69 * @return \Drupal\Core\Entity\EntityManagerInterface
71 * @deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0.
72 * Use \Drupal::entityTypeManager() instead in most cases. If the needed
73 * method is not on \Drupal\Core\Entity\EntityTypeManagerInterface, see the
74 * deprecated \Drupal\Core\Entity\EntityManager to find the
75 * correct interface or service.
77 protected function entityManager() {
78 return \Drupal::entityManager();
82 * Gets the entity type manager.
84 * @return \Drupal\Core\Entity\EntityTypeManagerInterface
86 protected function entityTypeManager() {
87 return \Drupal::entityTypeManager();
91 * Gets the language manager.
93 * @return \Drupal\Core\Language\LanguageManagerInterface
95 protected function languageManager() {
96 return \Drupal::languageManager();
100 * Gets the UUID generator.
102 * @return \Drupal\Component\Uuid\UuidInterface
104 protected function uuidGenerator() {
105 return \Drupal::service('uuid');
111 public function id() {
112 return isset($this->id) ? $this->id : NULL;
118 public function uuid() {
119 return isset($this->uuid) ? $this->uuid : NULL;
125 public function isNew() {
126 return !empty($this->enforceIsNew) || !$this->id();
132 public function enforceIsNew($value = TRUE) {
133 $this->enforceIsNew = $value;
141 public function getEntityTypeId() {
142 return $this->entityTypeId;
148 public function bundle() {
149 return $this->entityTypeId;
155 public function label() {
157 $entity_type = $this->getEntityType();
158 if (($label_callback = $entity_type->getLabelCallback()) && is_callable($label_callback)) {
159 $label = call_user_func($label_callback, $this);
161 elseif (($label_key = $entity_type->getKey('label')) && isset($this->{$label_key})) {
162 $label = $this->{$label_key};
170 public function urlInfo($rel = 'canonical', array $options = []) {
171 return $this->toUrl($rel, $options);
177 public function toUrl($rel = 'canonical', array $options = []) {
178 if ($this->id() === NULL) {
179 throw new EntityMalformedException(sprintf('The "%s" entity cannot have a URI as it does not have an ID', $this->getEntityTypeId()));
182 // The links array might contain URI templates set in annotations.
183 $link_templates = $this->linkTemplates();
185 // Links pointing to the current revision point to the actual entity. So
186 // instead of using the 'revision' link, use the 'canonical' link.
187 if ($rel === 'revision' && $this instanceof RevisionableInterface && $this->isDefaultRevision()) {
191 if (isset($link_templates[$rel])) {
192 $route_parameters = $this->urlRouteParameters($rel);
193 $route_name = "entity.{$this->entityTypeId}." . str_replace(['-', 'drupal:'], ['_', ''], $rel);
194 $uri = new Url($route_name, $route_parameters);
197 $bundle = $this->bundle();
198 // A bundle-specific callback takes precedence over the generic one for
200 $bundles = $this->entityManager()->getBundleInfo($this->getEntityTypeId());
201 if (isset($bundles[$bundle]['uri_callback'])) {
202 $uri_callback = $bundles[$bundle]['uri_callback'];
204 elseif ($entity_uri_callback = $this->getEntityType()->getUriCallback()) {
205 $uri_callback = $entity_uri_callback;
208 // Invoke the callback to get the URI. If there is no callback, use the
209 // default URI format.
210 if (isset($uri_callback) && is_callable($uri_callback)) {
211 $uri = call_user_func($uri_callback, $this);
214 throw new UndefinedLinkTemplateException("No link template '$rel' found for the '{$this->getEntityTypeId()}' entity type");
218 // Pass the entity data through as options, so that alter functions do not
219 // need to look up this entity again.
221 ->setOption('entity_type', $this->getEntityTypeId())
222 ->setOption('entity', $this);
224 // Display links by default based on the current language.
225 // Link relations that do not require an existing entity should not be
226 // affected by this entity's language, however.
227 if (!in_array($rel, ['collection', 'add-page', 'add-form'], TRUE)) {
228 $options += ['language' => $this->language()];
231 $uri_options = $uri->getOptions();
232 $uri_options += $options;
234 return $uri->setOptions($uri_options);
240 public function hasLinkTemplate($rel) {
241 $link_templates = $this->linkTemplates();
242 return isset($link_templates[$rel]);
246 * Gets an array link templates.
249 * An array of link templates containing paths.
251 protected function linkTemplates() {
252 return $this->getEntityType()->getLinkTemplates();
258 public function link($text = NULL, $rel = 'canonical', array $options = []) {
259 return $this->toLink($text, $rel, $options)->toString();
265 public function toLink($text = NULL, $rel = 'canonical', array $options = []) {
267 $text = $this->label();
269 $url = $this->toUrl($rel);
270 $options += $url->getOptions();
271 $url->setOptions($options);
272 return new Link($text, $url);
278 public function url($rel = 'canonical', $options = []) {
279 // While self::toUrl() will throw an exception if the entity has no id,
280 // the expected result for a URL is always a string.
281 if ($this->id() === NULL || !$this->hasLinkTemplate($rel)) {
285 $uri = $this->toUrl($rel);
286 $options += $uri->getOptions();
287 $uri->setOptions($options);
288 return $uri->toString();
292 * Gets an array of placeholders for this entity.
294 * Individual entity classes may override this method to add additional
295 * placeholders if desired. If so, they should be sure to replicate the
296 * property caching logic.
299 * The link relationship type, for example: canonical or edit-form.
302 * An array of URI placeholders.
304 protected function urlRouteParameters($rel) {
305 $uri_route_parameters = [];
307 if (!in_array($rel, ['collection', 'add-page', 'add-form'], TRUE)) {
308 // The entity ID is needed as a route parameter.
309 $uri_route_parameters[$this->getEntityTypeId()] = $this->id();
311 if ($rel === 'add-form' && ($this->getEntityType()->hasKey('bundle'))) {
312 $parameter_name = $this->getEntityType()->getBundleEntityType() ?: $this->getEntityType()->getKey('bundle');
313 $uri_route_parameters[$parameter_name] = $this->bundle();
315 if ($rel === 'revision' && $this instanceof RevisionableInterface) {
316 $uri_route_parameters[$this->getEntityTypeId() . '_revision'] = $this->getRevisionId();
319 return $uri_route_parameters;
325 public function uriRelationships() {
326 return array_keys($this->linkTemplates());
332 public function access($operation, AccountInterface $account = NULL, $return_as_object = FALSE) {
333 if ($operation == 'create') {
334 return $this->entityManager()
335 ->getAccessControlHandler($this->entityTypeId)
336 ->createAccess($this->bundle(), $account, [], $return_as_object);
338 return $this->entityManager()
339 ->getAccessControlHandler($this->entityTypeId)
340 ->access($this, $operation, $account, $return_as_object);
346 public function language() {
347 if ($key = $this->getEntityType()->getKey('langcode')) {
348 $langcode = $this->$key;
349 $language = $this->languageManager()->getLanguage($langcode);
354 // Make sure we return a proper language object.
355 $langcode = !empty($this->langcode) ? $this->langcode : LanguageInterface::LANGCODE_NOT_SPECIFIED;
356 $language = new Language(['id' => $langcode]);
363 public function save() {
364 return $this->entityManager()->getStorage($this->entityTypeId)->save($this);
370 public function delete() {
371 if (!$this->isNew()) {
372 $this->entityManager()->getStorage($this->entityTypeId)->delete([$this->id() => $this]);
379 public function createDuplicate() {
380 $duplicate = clone $this;
381 $entity_type = $this->getEntityType();
382 // Reset the entity ID and indicate that this is a new entity.
383 $duplicate->{$entity_type->getKey('id')} = NULL;
384 $duplicate->enforceIsNew();
386 // Check if the entity type supports UUIDs and generate a new one if so.
387 if ($entity_type->hasKey('uuid')) {
388 $duplicate->{$entity_type->getKey('uuid')} = $this->uuidGenerator()->generate();
396 public function getEntityType() {
397 return $this->entityManager()->getDefinition($this->getEntityTypeId());
403 public function preSave(EntityStorageInterface $storage) {
404 // Check if this is an entity bundle.
405 if ($this->getEntityType()->getBundleOf()) {
406 // Throw an exception if the bundle ID is longer than 32 characters.
407 if (Unicode::strlen($this->id()) > EntityTypeInterface::BUNDLE_MAX_LENGTH) {
408 throw new ConfigEntityIdLengthException("Attempt to create a bundle with an ID longer than " . EntityTypeInterface::BUNDLE_MAX_LENGTH . " characters: $this->id().");
416 public function postSave(EntityStorageInterface $storage, $update = TRUE) {
417 $this->invalidateTagsOnSave($update);
423 public static function preCreate(EntityStorageInterface $storage, array &$values) {
429 public function postCreate(EntityStorageInterface $storage) {
435 public static function preDelete(EntityStorageInterface $storage, array $entities) {
441 public static function postDelete(EntityStorageInterface $storage, array $entities) {
442 static::invalidateTagsOnDelete($storage->getEntityType(), $entities);
448 public static function postLoad(EntityStorageInterface $storage, array &$entities) {
454 public function referencedEntities() {
461 public function getCacheContexts() {
462 return $this->cacheContexts;
468 public function getCacheTagsToInvalidate() {
469 // @todo Add bundle-specific listing cache tag?
470 // https://www.drupal.org/node/2145751
471 if ($this->isNew()) {
474 return [$this->entityTypeId . ':' . $this->id()];
480 public function getCacheTags() {
481 if ($this->cacheTags) {
482 return Cache::mergeTags($this->getCacheTagsToInvalidate(), $this->cacheTags);
484 return $this->getCacheTagsToInvalidate();
490 public function getCacheMaxAge() {
491 return $this->cacheMaxAge;
497 public static function load($id) {
498 $entity_manager = \Drupal::entityManager();
499 return $entity_manager->getStorage($entity_manager->getEntityTypeFromClass(get_called_class()))->load($id);
505 public static function loadMultiple(array $ids = NULL) {
506 $entity_manager = \Drupal::entityManager();
507 return $entity_manager->getStorage($entity_manager->getEntityTypeFromClass(get_called_class()))->loadMultiple($ids);
513 public static function create(array $values = []) {
514 $entity_manager = \Drupal::entityManager();
515 return $entity_manager->getStorage($entity_manager->getEntityTypeFromClass(get_called_class()))->create($values);
519 * Invalidates an entity's cache tags upon save.
521 * @param bool $update
522 * TRUE if the entity has been updated, or FALSE if it has been inserted.
524 protected function invalidateTagsOnSave($update) {
525 // An entity was created or updated: invalidate its list cache tags. (An
526 // updated entity may start to appear in a listing because it now meets that
527 // listing's filtering requirements. A newly created entity may start to
528 // appear in listings because it did not exist before.)
529 $tags = $this->getEntityType()->getListCacheTags();
530 if ($this->hasLinkTemplate('canonical')) {
531 // Creating or updating an entity may change a cached 403 or 404 response.
532 $tags = Cache::mergeTags($tags, ['4xx-response']);
535 // An existing entity was updated, also invalidate its unique cache tag.
536 $tags = Cache::mergeTags($tags, $this->getCacheTagsToInvalidate());
538 Cache::invalidateTags($tags);
542 * Invalidates an entity's cache tags upon delete.
544 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
545 * The entity type definition.
546 * @param \Drupal\Core\Entity\EntityInterface[] $entities
547 * An array of entities.
549 protected static function invalidateTagsOnDelete(EntityTypeInterface $entity_type, array $entities) {
550 $tags = $entity_type->getListCacheTags();
551 foreach ($entities as $entity) {
552 // An entity was deleted: invalidate its own cache tag, but also its list
553 // cache tags. (A deleted entity may cause changes in a paged list on
554 // other pages than the one it's on. The one it's on is handled by its own
555 // cache tag, but subsequent list pages would not be invalidated, hence we
556 // must invalidate its list cache tags as well.)
557 $tags = Cache::mergeTags($tags, $entity->getCacheTagsToInvalidate());
559 Cache::invalidateTags($tags);
565 public function getOriginalId() {
566 // By default, entities do not support renames and do not have original IDs.
573 public function setOriginalId($id) {
574 // By default, entities do not support renames and do not have original IDs.
575 // If the specified ID is anything except NULL, this should mark this entity
578 $this->enforceIsNew(FALSE);
587 public function toArray() {
594 public function getTypedData() {
595 if (!isset($this->typedData)) {
596 $class = \Drupal::typedDataManager()->getDefinition('entity')['class'];
597 $this->typedData = $class::createFromEntity($this);
599 return $this->typedData;
605 public function __sleep() {
606 $this->typedData = NULL;
607 return $this->traitSleep();
613 public function getConfigDependencyKey() {
614 return $this->getEntityType()->getConfigDependencyKey();
620 public function getConfigDependencyName() {
621 return $this->getEntityTypeId() . ':' . $this->bundle() . ':' . $this->uuid();
627 public function getConfigTarget() {
628 // For content entities, use the UUID for the config target identifier.
629 // This ensures that references to the target can be deployed reliably.
630 return $this->uuid();