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