Chris@18: entityTypeId = $entity_type; Chris@18: // Set initial values. Chris@18: foreach ($values as $key => $value) { Chris@18: $this->$key = $value; Chris@18: } Chris@18: } Chris@18: Chris@18: /** Chris@18: * Gets the entity manager. Chris@18: * Chris@18: * @return \Drupal\Core\Entity\EntityManagerInterface Chris@18: * Chris@18: * @deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Chris@18: * Use \Drupal::entityTypeManager() instead in most cases. If the needed Chris@18: * method is not on \Drupal\Core\Entity\EntityTypeManagerInterface, see the Chris@18: * deprecated \Drupal\Core\Entity\EntityManager to find the Chris@18: * correct interface or service. Chris@18: */ Chris@18: protected function entityManager() { Chris@18: @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@18: return \Drupal::entityManager(); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Gets the entity type manager. Chris@18: * Chris@18: * @return \Drupal\Core\Entity\EntityTypeManagerInterface Chris@18: */ Chris@18: protected function entityTypeManager() { Chris@18: return \Drupal::entityTypeManager(); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Gets the entity type bundle info service. Chris@18: * Chris@18: * @return \Drupal\Core\Entity\EntityTypeBundleInfoInterface Chris@18: */ Chris@18: protected function entityTypeBundleInfo() { Chris@18: return \Drupal::service('entity_type.bundle.info'); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Gets the language manager. Chris@18: * Chris@18: * @return \Drupal\Core\Language\LanguageManagerInterface Chris@18: */ Chris@18: protected function languageManager() { Chris@18: return \Drupal::languageManager(); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Gets the UUID generator. Chris@18: * Chris@18: * @return \Drupal\Component\Uuid\UuidInterface Chris@18: */ Chris@18: protected function uuidGenerator() { Chris@18: return \Drupal::service('uuid'); Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function id() { Chris@18: return isset($this->id) ? $this->id : NULL; Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function uuid() { Chris@18: return isset($this->uuid) ? $this->uuid : NULL; Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function isNew() { Chris@18: return !empty($this->enforceIsNew) || !$this->id(); Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function enforceIsNew($value = TRUE) { Chris@18: $this->enforceIsNew = $value; Chris@18: Chris@18: return $this; Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function getEntityTypeId() { Chris@18: return $this->entityTypeId; Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function bundle() { Chris@18: return $this->entityTypeId; Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function label() { Chris@18: $label = NULL; Chris@18: $entity_type = $this->getEntityType(); Chris@18: if (($label_callback = $entity_type->getLabelCallback()) && is_callable($label_callback)) { Chris@18: $label = call_user_func($label_callback, $this); Chris@18: } Chris@18: elseif (($label_key = $entity_type->getKey('label')) && isset($this->{$label_key})) { Chris@18: $label = $this->{$label_key}; Chris@18: } Chris@18: return $label; Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function urlInfo($rel = 'canonical', array $options = []) { Chris@18: @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@18: return $this->toUrl($rel, $options); Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function toUrl($rel = 'canonical', array $options = []) { Chris@18: if ($this->id() === NULL) { Chris@18: throw new EntityMalformedException(sprintf('The "%s" entity cannot have a URI as it does not have an ID', $this->getEntityTypeId())); Chris@18: } Chris@18: Chris@18: // The links array might contain URI templates set in annotations. Chris@18: $link_templates = $this->linkTemplates(); Chris@18: Chris@18: // Links pointing to the current revision point to the actual entity. So Chris@18: // instead of using the 'revision' link, use the 'canonical' link. Chris@18: if ($rel === 'revision' && $this instanceof RevisionableInterface && $this->isDefaultRevision()) { Chris@18: $rel = 'canonical'; Chris@18: } Chris@18: Chris@18: if (isset($link_templates[$rel])) { Chris@18: $route_parameters = $this->urlRouteParameters($rel); Chris@18: $route_name = "entity.{$this->entityTypeId}." . str_replace(['-', 'drupal:'], ['_', ''], $rel); Chris@18: $uri = new Url($route_name, $route_parameters); Chris@18: } Chris@18: else { Chris@18: $bundle = $this->bundle(); Chris@18: // A bundle-specific callback takes precedence over the generic one for Chris@18: // the entity type. Chris@18: $bundles = $this->entityTypeBundleInfo()->getBundleInfo($this->getEntityTypeId()); Chris@18: if (isset($bundles[$bundle]['uri_callback'])) { Chris@18: $uri_callback = $bundles[$bundle]['uri_callback']; Chris@18: } Chris@18: elseif ($entity_uri_callback = $this->getEntityType()->getUriCallback()) { Chris@18: $uri_callback = $entity_uri_callback; Chris@18: } Chris@18: Chris@18: // Invoke the callback to get the URI. If there is no callback, use the Chris@18: // default URI format. Chris@18: if (isset($uri_callback) && is_callable($uri_callback)) { Chris@18: $uri = call_user_func($uri_callback, $this); Chris@18: } Chris@18: else { Chris@18: throw new UndefinedLinkTemplateException("No link template '$rel' found for the '{$this->getEntityTypeId()}' entity type"); Chris@18: } Chris@18: } Chris@18: Chris@18: // Pass the entity data through as options, so that alter functions do not Chris@18: // need to look up this entity again. Chris@18: $uri Chris@18: ->setOption('entity_type', $this->getEntityTypeId()) Chris@18: ->setOption('entity', $this); Chris@18: Chris@18: // Display links by default based on the current language. Chris@18: // Link relations that do not require an existing entity should not be Chris@18: // affected by this entity's language, however. Chris@18: if (!in_array($rel, ['collection', 'add-page', 'add-form'], TRUE)) { Chris@18: $options += ['language' => $this->language()]; Chris@18: } Chris@18: Chris@18: $uri_options = $uri->getOptions(); Chris@18: $uri_options += $options; Chris@18: Chris@18: return $uri->setOptions($uri_options); Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function hasLinkTemplate($rel) { Chris@18: $link_templates = $this->linkTemplates(); Chris@18: return isset($link_templates[$rel]); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Gets an array link templates. Chris@18: * Chris@18: * @return array Chris@18: * An array of link templates containing paths. Chris@18: */ Chris@18: protected function linkTemplates() { Chris@18: return $this->getEntityType()->getLinkTemplates(); Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function link($text = NULL, $rel = 'canonical', array $options = []) { Chris@18: @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@18: return $this->toLink($text, $rel, $options)->toString(); Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function toLink($text = NULL, $rel = 'canonical', array $options = []) { Chris@18: if (!isset($text)) { Chris@18: $text = $this->label(); Chris@18: } Chris@18: $url = $this->toUrl($rel); Chris@18: $options += $url->getOptions(); Chris@18: $url->setOptions($options); Chris@18: return new Link($text, $url); Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function url($rel = 'canonical', $options = []) { Chris@18: @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@18: // While self::toUrl() will throw an exception if the entity has no id, Chris@18: // the expected result for a URL is always a string. Chris@18: if ($this->id() === NULL || !$this->hasLinkTemplate($rel)) { Chris@18: return ''; Chris@18: } Chris@18: Chris@18: $uri = $this->toUrl($rel); Chris@18: $options += $uri->getOptions(); Chris@18: $uri->setOptions($options); Chris@18: return $uri->toString(); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Gets an array of placeholders for this entity. Chris@18: * Chris@18: * Individual entity classes may override this method to add additional Chris@18: * placeholders if desired. If so, they should be sure to replicate the Chris@18: * property caching logic. Chris@18: * Chris@18: * @param string $rel Chris@18: * The link relationship type, for example: canonical or edit-form. Chris@18: * Chris@18: * @return array Chris@18: * An array of URI placeholders. Chris@18: */ Chris@18: protected function urlRouteParameters($rel) { Chris@18: $uri_route_parameters = []; Chris@18: Chris@18: if (!in_array($rel, ['collection', 'add-page', 'add-form'], TRUE)) { Chris@18: // The entity ID is needed as a route parameter. Chris@18: $uri_route_parameters[$this->getEntityTypeId()] = $this->id(); Chris@18: } Chris@18: if ($rel === 'add-form' && ($this->getEntityType()->hasKey('bundle'))) { Chris@18: $parameter_name = $this->getEntityType()->getBundleEntityType() ?: $this->getEntityType()->getKey('bundle'); Chris@18: $uri_route_parameters[$parameter_name] = $this->bundle(); Chris@18: } Chris@18: if ($rel === 'revision' && $this instanceof RevisionableInterface) { Chris@18: $uri_route_parameters[$this->getEntityTypeId() . '_revision'] = $this->getRevisionId(); Chris@18: } Chris@18: Chris@18: return $uri_route_parameters; Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function uriRelationships() { Chris@18: return array_filter(array_keys($this->linkTemplates()), function ($link_relation_type) { Chris@18: // It's not guaranteed that every link relation type also has a Chris@18: // corresponding route. For some, additional modules or configuration may Chris@18: // be necessary. The interface demands that we only return supported URI Chris@18: // relationships. Chris@18: try { Chris@18: $this->toUrl($link_relation_type)->toString(TRUE)->getGeneratedUrl(); Chris@18: } Chris@18: catch (RouteNotFoundException $e) { Chris@18: return FALSE; Chris@18: } Chris@18: catch (MissingMandatoryParametersException $e) { Chris@18: return FALSE; Chris@18: } Chris@18: return TRUE; Chris@18: }); Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function access($operation, AccountInterface $account = NULL, $return_as_object = FALSE) { Chris@18: if ($operation == 'create') { Chris@18: return $this->entityTypeManager() Chris@18: ->getAccessControlHandler($this->entityTypeId) Chris@18: ->createAccess($this->bundle(), $account, [], $return_as_object); Chris@18: } Chris@18: return $this->entityTypeManager() Chris@18: ->getAccessControlHandler($this->entityTypeId) Chris@18: ->access($this, $operation, $account, $return_as_object); Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function language() { Chris@18: if ($key = $this->getEntityType()->getKey('langcode')) { Chris@18: $langcode = $this->$key; Chris@18: $language = $this->languageManager()->getLanguage($langcode); Chris@18: if ($language) { Chris@18: return $language; Chris@18: } Chris@18: } Chris@18: // Make sure we return a proper language object. Chris@18: $langcode = !empty($this->langcode) ? $this->langcode : LanguageInterface::LANGCODE_NOT_SPECIFIED; Chris@18: $language = new Language(['id' => $langcode]); Chris@18: return $language; Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function save() { Chris@18: $storage = $this->entityTypeManager()->getStorage($this->entityTypeId); Chris@18: return $storage->save($this); Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function delete() { Chris@18: if (!$this->isNew()) { Chris@18: $this->entityTypeManager()->getStorage($this->entityTypeId)->delete([$this->id() => $this]); Chris@18: } Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function createDuplicate() { Chris@18: $duplicate = clone $this; Chris@18: $entity_type = $this->getEntityType(); Chris@18: // Reset the entity ID and indicate that this is a new entity. Chris@18: $duplicate->{$entity_type->getKey('id')} = NULL; Chris@18: $duplicate->enforceIsNew(); Chris@18: Chris@18: // Check if the entity type supports UUIDs and generate a new one if so. Chris@18: if ($entity_type->hasKey('uuid')) { Chris@18: $duplicate->{$entity_type->getKey('uuid')} = $this->uuidGenerator()->generate(); Chris@18: } Chris@18: return $duplicate; Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function getEntityType() { Chris@18: return $this->entityTypeManager()->getDefinition($this->getEntityTypeId()); Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function preSave(EntityStorageInterface $storage) { Chris@18: // Check if this is an entity bundle. Chris@18: if ($this->getEntityType()->getBundleOf()) { Chris@18: // Throw an exception if the bundle ID is longer than 32 characters. Chris@18: if (mb_strlen($this->id()) > EntityTypeInterface::BUNDLE_MAX_LENGTH) { Chris@18: throw new ConfigEntityIdLengthException("Attempt to create a bundle with an ID longer than " . EntityTypeInterface::BUNDLE_MAX_LENGTH . " characters: $this->id()."); Chris@18: } Chris@18: } Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function postSave(EntityStorageInterface $storage, $update = TRUE) { Chris@18: $this->invalidateTagsOnSave($update); Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public static function preCreate(EntityStorageInterface $storage, array &$values) { Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function postCreate(EntityStorageInterface $storage) { Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public static function preDelete(EntityStorageInterface $storage, array $entities) { Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public static function postDelete(EntityStorageInterface $storage, array $entities) { Chris@18: static::invalidateTagsOnDelete($storage->getEntityType(), $entities); Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public static function postLoad(EntityStorageInterface $storage, array &$entities) { Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function referencedEntities() { Chris@18: return []; Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function getCacheContexts() { Chris@18: return $this->cacheContexts; Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function getCacheTagsToInvalidate() { Chris@18: // @todo Add bundle-specific listing cache tag? Chris@18: // https://www.drupal.org/node/2145751 Chris@18: if ($this->isNew()) { Chris@18: return []; Chris@18: } Chris@18: return [$this->entityTypeId . ':' . $this->id()]; Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function getCacheTags() { Chris@18: if ($this->cacheTags) { Chris@18: return Cache::mergeTags($this->getCacheTagsToInvalidate(), $this->cacheTags); Chris@18: } Chris@18: return $this->getCacheTagsToInvalidate(); Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function getCacheMaxAge() { Chris@18: return $this->cacheMaxAge; Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public static function load($id) { Chris@18: $entity_type_repository = \Drupal::service('entity_type.repository'); Chris@18: $entity_type_manager = \Drupal::entityTypeManager(); Chris@18: $storage = $entity_type_manager->getStorage($entity_type_repository->getEntityTypeFromClass(get_called_class())); Chris@18: return $storage->load($id); Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public static function loadMultiple(array $ids = NULL) { Chris@18: $entity_type_repository = \Drupal::service('entity_type.repository'); Chris@18: $entity_type_manager = \Drupal::entityTypeManager(); Chris@18: $storage = $entity_type_manager->getStorage($entity_type_repository->getEntityTypeFromClass(get_called_class())); Chris@18: return $storage->loadMultiple($ids); Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public static function create(array $values = []) { Chris@18: $entity_type_repository = \Drupal::service('entity_type.repository'); Chris@18: $entity_type_manager = \Drupal::entityTypeManager(); Chris@18: $storage = $entity_type_manager->getStorage($entity_type_repository->getEntityTypeFromClass(get_called_class())); Chris@18: return $storage->create($values); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Invalidates an entity's cache tags upon save. Chris@18: * Chris@18: * @param bool $update Chris@18: * TRUE if the entity has been updated, or FALSE if it has been inserted. Chris@18: */ Chris@18: protected function invalidateTagsOnSave($update) { Chris@18: // An entity was created or updated: invalidate its list cache tags. (An Chris@18: // updated entity may start to appear in a listing because it now meets that Chris@18: // listing's filtering requirements. A newly created entity may start to Chris@18: // appear in listings because it did not exist before.) Chris@18: $tags = $this->getEntityType()->getListCacheTags(); Chris@18: if ($this->hasLinkTemplate('canonical')) { Chris@18: // Creating or updating an entity may change a cached 403 or 404 response. Chris@18: $tags = Cache::mergeTags($tags, ['4xx-response']); Chris@18: } Chris@18: if ($update) { Chris@18: // An existing entity was updated, also invalidate its unique cache tag. Chris@18: $tags = Cache::mergeTags($tags, $this->getCacheTagsToInvalidate()); Chris@18: } Chris@18: Cache::invalidateTags($tags); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Invalidates an entity's cache tags upon delete. Chris@18: * Chris@18: * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type Chris@18: * The entity type definition. Chris@18: * @param \Drupal\Core\Entity\EntityInterface[] $entities Chris@18: * An array of entities. Chris@18: */ Chris@18: protected static function invalidateTagsOnDelete(EntityTypeInterface $entity_type, array $entities) { Chris@18: $tags = $entity_type->getListCacheTags(); Chris@18: foreach ($entities as $entity) { Chris@18: // An entity was deleted: invalidate its own cache tag, but also its list Chris@18: // cache tags. (A deleted entity may cause changes in a paged list on Chris@18: // other pages than the one it's on. The one it's on is handled by its own Chris@18: // cache tag, but subsequent list pages would not be invalidated, hence we Chris@18: // must invalidate its list cache tags as well.) Chris@18: $tags = Cache::mergeTags($tags, $entity->getCacheTagsToInvalidate()); Chris@18: } Chris@18: Cache::invalidateTags($tags); Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function getOriginalId() { Chris@18: // By default, entities do not support renames and do not have original IDs. Chris@18: return NULL; Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function setOriginalId($id) { Chris@18: // By default, entities do not support renames and do not have original IDs. Chris@18: // If the specified ID is anything except NULL, this should mark this entity Chris@18: // as no longer new. Chris@18: if ($id !== NULL) { Chris@18: $this->enforceIsNew(FALSE); Chris@18: } Chris@18: Chris@18: return $this; Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function toArray() { Chris@18: return []; Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function getTypedData() { Chris@18: if (!isset($this->typedData)) { Chris@18: $class = \Drupal::typedDataManager()->getDefinition('entity')['class']; Chris@18: $this->typedData = $class::createFromEntity($this); Chris@18: } Chris@18: return $this->typedData; Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function __sleep() { Chris@18: $this->typedData = NULL; Chris@18: return $this->traitSleep(); Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function getConfigDependencyKey() { Chris@18: return $this->getEntityType()->getConfigDependencyKey(); Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function getConfigDependencyName() { Chris@18: return $this->getEntityTypeId() . ':' . $this->bundle() . ':' . $this->uuid(); Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function getConfigTarget() { Chris@18: // For content entities, use the UUID for the config target identifier. Chris@18: // This ensures that references to the target can be deployed reliably. Chris@18: return $this->uuid(); Chris@18: } Chris@18: Chris@18: }