interface EntityInterface implements AccessibleInterface, CacheableDependencyInterface, RefinableCacheableDependencyInterface (View source)

Defines a common interface for all entity objects.

Methods

access(string $operation, AccountInterface $account = NULL, bool $return_as_object = FALSE)

Checks data value access.

string[]
getCacheContexts()

The cache contexts associated with this object.

string[]
getCacheTags()

The cache tags associated with this object.

int
getCacheMaxAge()

The maximum age for which this object may be cached.

$this
addCacheContexts(array $cache_contexts)

Adds cache contexts.

$this
addCacheTags(array $cache_tags)

Adds cache tags.

$this
mergeCacheMaxAge(int $max_age)

Merges the maximum age (in seconds) with the existing maximum age.

$this
addCacheableDependency(CacheableDependencyInterface|object $other_object)

Adds a dependency on an object: merges its cacheability metadata.

string|null
uuid()

Gets the entity UUID (Universally Unique Identifier).

string|int|null
id()

Gets the identifier.

language()

Gets the language of the entity.

bool
isNew()

Determines whether the entity is new.

$this
enforceIsNew(bool $value = TRUE)

Enforces an entity to be new.

string
getEntityTypeId()

Gets the ID of the type of the entity.

string
bundle()

Gets the bundle of the entity.

string|TranslatableMarkup|null
label()

Gets the label of the entity.

Url
toUrl(string $rel = 'canonical', array $options = [])

Gets the URL object for the entity.

toLink(string|null $text = NULL, string $rel = 'canonical', array $options = [])

Generates the HTML for a link to this entity.

bool
hasLinkTemplate(string $key)

Indicates if a link template exists for a given key.

string[]
uriRelationships()

Gets a list of URI relationships supported by this entity.

static EntityInterface|null
load(mixed $id)

Loads an entity.

static EntityInterface[]
loadMultiple(array $ids = NULL)

Loads one or more entities.

static EntityInterface
create(array $values = [])

Constructs a new entity object, without permanently saving it.

int
save()

Saves an entity permanently.

delete()

Deletes an entity permanently.

preSave(EntityStorageInterface $storage)

Acts on an entity before the presave hook is invoked.

postSave(EntityStorageInterface $storage, bool $update = TRUE)

Acts on a saved entity before the insert or update hook is invoked.

static 
preCreate(EntityStorageInterface $storage, array $values)

Changes the values of an entity before it is created.

postCreate(EntityStorageInterface $storage)

Acts on a created entity before hooks are invoked.

static 
preDelete(EntityStorageInterface $storage, array $entities)

Acts on entities before they are deleted and before hooks are invoked.

static 
postDelete(EntityStorageInterface $storage, array $entities)

Acts on deleted entities before the delete hook is invoked.

static 
postLoad(EntityStorageInterface $storage, array $entities)

Acts on loaded entities.

createDuplicate()

Creates a duplicate of the entity.

getEntityType()

Gets the entity type definition.

referencedEntities()

Gets a list of entities referenced by this entity.

int|string|null
getOriginalId()

Gets the original ID.

string[]
getCacheTagsToInvalidate()

Returns the cache tags that should be used to invalidate caches.

$this
setOriginalId(int|string|null $id)

Sets the original ID.

array
toArray()

Gets an array of all property values.

getTypedData()

Gets a typed data object for this entity object.

string
getConfigDependencyKey()

Gets the key that is used to store configuration dependencies.

string
getConfigDependencyName()

Gets the configuration dependency name.

string
getConfigTarget()

Gets the configuration target identifier for the entity.

Details

bool|AccessResultInterface access(string $operation, AccountInterface $account = NULL, bool $return_as_object = FALSE)

Checks data value access.

Parameters

string $operation

The operation to be performed.

AccountInterface $account

(optional) The user for which to check access, or NULL to check access for the current user. Defaults to NULL.

bool $return_as_object

(optional) Defaults to FALSE.

Return Value

bool|AccessResultInterface

The access result. Returns a boolean if $return_as_object is FALSE (this is the default) and otherwise an AccessResultInterface object. When a boolean is returned, the result of AccessInterface::isAllowed() is returned, i.e. TRUE means access is explicitly allowed, FALSE means access is either explicitly forbidden or "no opinion".

string[] getCacheContexts()

The cache contexts associated with this object.

These identify a specific variation/representation of the object.

Cache contexts are tokens: placeholders that are converted to cache keys by the @cache_contexts_manager service. The replacement value depends on the request context (the current URL, language, and so on). They're converted before storing an object in cache.

Return Value

string[]

An array of cache context tokens, used to generate a cache ID.

See also

CacheContextsManager::convertTokensToKeys

string[] getCacheTags()

The cache tags associated with this object.

When this object is modified, these cache tags will be invalidated.

Return Value

string[]

A set of cache tags.

int getCacheMaxAge()

The maximum age for which this object may be cached.

Return Value

int

The maximum time in seconds that this object may be cached.

$this addCacheContexts(array $cache_contexts)

Adds cache contexts.

Parameters

array $cache_contexts

The cache contexts to be added.

Return Value

$this

$this addCacheTags(array $cache_tags)

Adds cache tags.

Parameters

array $cache_tags

The cache tags to be added.

Return Value

$this

$this mergeCacheMaxAge(int $max_age)

Merges the maximum age (in seconds) with the existing maximum age.

The max age will be set to the given value if it is lower than the existing value.

Parameters

int $max_age

The max age to associate.

Return Value

$this

Exceptions

InvalidArgumentException

$this addCacheableDependency(CacheableDependencyInterface|object $other_object)

Adds a dependency on an object: merges its cacheability metadata.

Parameters

CacheableDependencyInterface|object $other_object

The dependency. If the object implements CacheableDependencyInterface, then its cacheability metadata will be used. Otherwise, the passed in object must be assumed to be uncacheable, so max-age 0 is set.

Return Value

$this

See also

CacheableMetadata::createFromObject

string|null uuid()

Gets the entity UUID (Universally Unique Identifier).

The UUID is guaranteed to be unique and can be used to identify an entity across multiple systems.

Return Value

string|null

The UUID of the entity, or NULL if the entity does not have one.

string|int|null id()

Gets the identifier.

Return Value

string|int|null

The entity identifier, or NULL if the object does not yet have an identifier.

LanguageInterface language()

Gets the language of the entity.

Return Value

LanguageInterface

The language object.

bool isNew()

Determines whether the entity is new.

Usually an entity is new if no ID exists for it yet. However, entities may be enforced to be new with existing IDs too.

Return Value

bool

TRUE if the entity is new, or FALSE if the entity has already been saved.

See also

EntityInterface::enforceIsNew

$this enforceIsNew(bool $value = TRUE)

Enforces an entity to be new.

Allows migrations to create entities with pre-defined IDs by forcing the entity to be new before saving.

Parameters

bool $value

(optional) Whether the entity should be forced to be new. Defaults to TRUE.

Return Value

$this

See also

EntityInterface::isNew

string getEntityTypeId()

Gets the ID of the type of the entity.

Return Value

string

The entity type ID.

string bundle()

Gets the bundle of the entity.

Return Value

string

The bundle of the entity. Defaults to the entity type ID if the entity type does not make use of different bundles.

string|TranslatableMarkup|null label()

Gets the label of the entity.

Return Value

string|TranslatableMarkup|null

The label of the entity, or NULL if there is no label defined.

Url toUrl(string $rel = 'canonical', array $options = [])

Gets the URL object for the entity.

The entity must have an id already. Content entities usually get their IDs by saving them.

URI templates might be set in the links array in an annotation, for example:

Parameters

string $rel

The link relationship type, for example: canonical or edit-form.

array $options

See \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute() for the available options.

Return Value

Url

The URL object.

Exceptions

EntityMalformedException
UndefinedLinkTemplateException

Generates the HTML for a link to this entity.

Parameters

string|null $text

(optional) The link text for the anchor tag as a translated string. If NULL, it will use the entity's label. Defaults to NULL.

string $rel

(optional) The link relationship type. Defaults to 'canonical'.

array $options

See \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute() for the available options.

Return Value

Link

A Link to the entity.

Exceptions

EntityMalformedException
UndefinedLinkTemplateException

bool hasLinkTemplate(string $key)

Indicates if a link template exists for a given key.

Parameters

string $key

The link type.

Return Value

bool

TRUE if the link template exists, FALSE otherwise.

string[] uriRelationships()

Gets a list of URI relationships supported by this entity.

Return Value

string[]

An array of link relationships supported by this entity.

static EntityInterface|null load(mixed $id)

Loads an entity.

Parameters

mixed $id

The id of the entity to load.

Return Value

EntityInterface|null

The entity object or NULL if there is no entity with the given ID.

static EntityInterface[] loadMultiple(array $ids = NULL)

Loads one or more entities.

Parameters

array $ids

An array of entity IDs, or NULL to load all entities.

Return Value

EntityInterface[]

An array of entity objects indexed by their IDs.

static EntityInterface create(array $values = [])

Constructs a new entity object, without permanently saving it.

Parameters

array $values

(optional) An array of values to set, keyed by property name. If the entity type has bundles, the bundle key has to be specified.

Return Value

EntityInterface

The entity object.

int save()

Saves an entity permanently.

When saving existing entities, the entity is assumed to be complete, partial updates of entities are not supported.

Return Value

int

Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

Exceptions

EntityStorageException

delete()

Deletes an entity permanently.

preSave(EntityStorageInterface $storage)

Acts on an entity before the presave hook is invoked.

Used before the entity is saved and before invoking the presave hook. Note that in case of translatable content entities this callback is only fired on their current translation. It is up to the developer to iterate over all translations if needed. This is different from its counterpart in the Field API, FieldItemListInterface::preSave(), which is fired on all field translations automatically.

Adjust existing implementations and the documentation according to https://www.drupal.org/node/2577609 to have a consistent API.

Parameters

EntityStorageInterface $storage

The entity storage object.

Exceptions

Exception

See also

FieldItemListInterface::preSave

postSave(EntityStorageInterface $storage, bool $update = TRUE)

Acts on a saved entity before the insert or update hook is invoked.

Used after the entity is saved, but before invoking the insert or update hook. Note that in case of translatable content entities this callback is only fired on their current translation. It is up to the developer to iterate over all translations if needed.

Parameters

EntityStorageInterface $storage

The entity storage object.

bool $update

TRUE if the entity has been updated, or FALSE if it has been inserted.

static preCreate(EntityStorageInterface $storage, array $values)

Changes the values of an entity before it is created.

Load defaults for example.

Parameters

EntityStorageInterface $storage

The entity storage object.

array $values

An array of values to set, keyed by property name. If the entity type has bundles the bundle key has to be specified.

postCreate(EntityStorageInterface $storage)

Acts on a created entity before hooks are invoked.

Used after the entity is created, but before saving the entity and before any of the presave hooks are invoked.

See the @link entity_crud Entity CRUD topic @endlink for more information.

Parameters

EntityStorageInterface $storage

The entity storage object.

See also

EntityInterface::create

static preDelete(EntityStorageInterface $storage, array $entities)

Acts on entities before they are deleted and before hooks are invoked.

Used before the entities are deleted and before invoking the delete hook.

Parameters

EntityStorageInterface $storage

The entity storage object.

array $entities

An array of entities.

static postDelete(EntityStorageInterface $storage, array $entities)

Acts on deleted entities before the delete hook is invoked.

Used after the entities are deleted but before invoking the delete hook.

Parameters

EntityStorageInterface $storage

The entity storage object.

array $entities

An array of entities.

static postLoad(EntityStorageInterface $storage, array $entities)

Acts on loaded entities.

Parameters

EntityStorageInterface $storage

The entity storage object.

array $entities

An array of entities.

EntityInterface createDuplicate()

Creates a duplicate of the entity.

Return Value

EntityInterface

A clone of $this with all identifiers unset, so saving it inserts a new entity into the storage system.

EntityTypeInterface getEntityType()

Gets the entity type definition.

Return Value

EntityTypeInterface

The entity type definition.

EntityInterface[] referencedEntities()

Gets a list of entities referenced by this entity.

Return Value

EntityInterface[]

An array of entities.

int|string|null getOriginalId()

Gets the original ID.

Return Value

int|string|null

The original ID, or NULL if no ID was set or for entity types that do not support renames.

string[] getCacheTagsToInvalidate()

Returns the cache tags that should be used to invalidate caches.

This will not return additional cache tags added through addCacheTags().

$this setOriginalId(int|string|null $id)

Sets the original ID.

Parameters

int|string|null $id

The new ID to set as original ID. If the entity supports renames, setting NULL will prevent an update from being considered a rename.

Return Value

$this

array toArray()

Gets an array of all property values.

Return Value

array

An array of property values, keyed by property name.

ComplexDataInterface getTypedData()

Gets a typed data object for this entity object.

The returned typed data object wraps this entity and allows dealing with entities based on the generic typed data API.

Return Value

ComplexDataInterface

The typed data object for this entity.

See also

TypedDataInterface

string getConfigDependencyKey()

Gets the key that is used to store configuration dependencies.

Return Value

string

The key to be used in configuration dependencies when storing dependencies on entities of this type.

See also

EntityTypeInterface::getConfigDependencyKey

string getConfigDependencyName()

Gets the configuration dependency name.

Configuration entities can depend on content and configuration entities. They store an array of content and config dependency names in their "dependencies" key.

Return Value

string

The configuration dependency name.

See also

ConfigDependencyManager

string getConfigTarget()

Gets the configuration target identifier for the entity.

Used to supply the correct format for storing a reference targeting this entity in configuration.

Return Value

string

The configuration target identifier.