class PathAliasStorage extends SqlContentEntityStorage (View source)

Defines the storage handler class for path_alias entities.

Traits

Wrapper methods for \Drupal\Core\StringTranslation\TranslationInterface.

Provides dependency injection friendly methods for serialization.

Properties

protected TranslationInterface $stringTranslation

The string translation service.

from  StringTranslationTrait
protected array $_serviceIds

An array of service IDs keyed by property name used for serialization.

from  DependencySerializationTrait
protected array $_entityStorages

An array of entity type IDs keyed by the property name of their storages.

from  DependencySerializationTrait
protected ModuleHandlerInterface $moduleHandler

The module handler to invoke hooks on.

from  EntityHandlerBase
protected string $entityTypeId

Entity type ID for this storage.

from  EntityStorageBase
protected EntityTypeInterface $entityType

Information about the entity type.

from  EntityStorageBase
protected string $idKey

Name of the entity's ID field in the entity database table.

from  EntityStorageBase
protected string $uuidKey

Name of entity's UUID database table field, if it supports UUIDs.

from  EntityStorageBase
protected string|bool $langcodeKey

The entity langcode key.

from  SqlContentEntityStorage
protected UuidInterface $uuidService

The UUID service.

from  EntityStorageBase
protected MemoryCacheInterface $memoryCache

The memory cache.

from  EntityStorageBase
protected string $memoryCacheTag

The memory cache cache tag.

from  EntityStorageBase
protected string|bool $bundleKey

The entity bundle key.

from  ContentEntityStorageBase
protected EntityFieldManagerInterface $entityFieldManager

The entity field manager service.

from  ContentEntityStorageBase
protected EntityTypeBundleInfoInterface $entityTypeBundleInfo

The entity bundle info.

from  ContentEntityStorageBase
protected CacheBackendInterface $cacheBackend

Cache backend.

from  ContentEntityStorageBase
protected array $latestRevisionIds

Stores the latest revision IDs for entities.

from  ContentEntityStorageBase
protected FieldStorageDefinitionInterface[] $fieldStorageDefinitions

The entity type's field storage definitions.

from  SqlContentEntityStorage
protected TableMappingInterface $tableMapping

The mapping of field columns to SQL tables.

from  SqlContentEntityStorage
protected string $revisionKey

Name of entity's revision database table field, if it supports revisions.

from  SqlContentEntityStorage
protected string $defaultLangcodeKey

The default language entity key.

from  SqlContentEntityStorage
protected string $baseTable

The base table of the entity.

from  SqlContentEntityStorage
protected string $revisionTable

The table that stores revisions, if the entity supports revisions.

from  SqlContentEntityStorage
protected string $dataTable

The table that stores properties, if the entity has multilingual support.

from  SqlContentEntityStorage
protected string $revisionDataTable

The table that stores revision field data if the entity supports revisions.

from  SqlContentEntityStorage
protected Connection $database

Active database connection.

from  SqlContentEntityStorage
protected EntityStorageSchemaInterface $storageSchema

The entity type's storage schema object.

from  SqlContentEntityStorage
protected LanguageManagerInterface $languageManager

The language manager.

from  SqlContentEntityStorage
protected EntityTypeManagerInterface $entityTypeManager

The entity type manager.

from  SqlContentEntityStorage
protected bool $temporary

Whether this storage should use the temporary table mapping.

from  SqlContentEntityStorage

Methods

t(string $string, array $args = [], array $options = [])

Translates a string to the current language or to a given language.

formatPlural($count, $singular, $plural, array $args = [], array $options = [])

Formats a string containing a count of items.

getNumberOfPlurals($langcode = NULL)

Returns the number of plurals supported by a given language.

getStringTranslation()

Gets the string translation service.

$this
setStringTranslation(TranslationInterface $translation)

Sets the string translation service to use.

__sleep()

{@inheritdoc}

__wakeup()

{@inheritdoc}

moduleHandler()

Gets the module handler.

$this
setModuleHandler(ModuleHandlerInterface $module_handler)

Sets the module handler for this handler.

__construct(EntityTypeInterface $entity_type, Connection $database, EntityFieldManagerInterface $entity_field_manager, CacheBackendInterface $cache, LanguageManagerInterface $language_manager, MemoryCacheInterface $memory_cache, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityTypeManagerInterface $entity_type_manager)

Constructs a SqlContentEntityStorage object.

string
getEntityClass(string|null $bundle = NULL)

Retrieves the class name used to create the entity.

__get(string $name)

Warns subclasses not to directly access the deprecated entityClass property.

void
__set(string $name, mixed $value)

Warns subclasses not to directly set the deprecated entityClass property.

string
getEntityTypeId()

Gets the entity type ID.

getEntityType()

Gets the entity type definition.

string
buildCacheId(int $id)

Builds the cache ID for the passed in entity ID.

loadUnchanged(mixed $id)

Loads an unchanged entity from the database.

resetCache(array $ids = NULL)

Resets the entity cache.

getFromStaticCache(array $ids)

Gets entities from the static cache.

setStaticCache(array $entities)

Stores entities in the static entity cache.

invokeHook(string $hook, EntityInterface $entity)

Invokes a hook on behalf of the entity.

create(array $values = [])

Constructs a new entity object, without permanently saving it.

doCreate(array $values)

Performs storage-specific creation of entities.

load(mixed $id)

Loads one entity.

loadMultiple(array $ids = NULL)

Loads one or more entities.

doLoadMultiple(array $ids = NULL)

Performs storage-specific loading of entities.

preLoad(array $ids = NULL)

Gathers entities from a 'preload' step.

postLoad(array $entities)

Attaches data to entities upon loading.

mapFromStorageRecords(array $records, bool $load_from_revision = FALSE)

Maps from storage records to entity objects, and attaches fields.

bool
has(int|string $id, EntityInterface $entity)

Determines if this entity already exists in storage.

delete(array $entities)

Deletes permanently saved entities.

doDelete(EntityInterface[] $entities)

Performs storage-specific entity deletion.

SAVED_NEW
save(EntityInterface $entity)

Saves the entity permanently.

int|string
doPreSave(EntityInterface $entity)

Performs presave entity processing.

bool|int
doSave(int|string $id, EntityInterface $entity)

Performs storage-specific saving of the entity.

doPostSave(EntityInterface $entity, bool $update)

Performs post save entity processing.

restore(EntityInterface $entity)

Restores a previously saved entity.

buildPropertyQuery(QueryInterface $entity_query, array $values)

Builds an entity query.

loadByProperties(array $values = [])

Load entities by their property values.

bool
hasData()

Determines if the storage contains any data.

getQuery(string $conjunction = 'AND')

Gets an entity query instance.

getAggregateQuery(string $conjunction = 'AND')

Gets an aggregated query instance.

string
getQueryServiceName()

Gets the name of the service for the query for this entity storage.

array
getEntitiesByClass(array $entities)

Indexes the given array of entities by their class name and ID.

createInstance(ContainerInterface $container, EntityTypeInterface $entity_type)

Instantiates a new instance of this entity handler.

string|null
getBundleFromClass(string $class_name)

Retrieves the bundle name for a provided class name.

string|null
getBundleFromValues(array $values)

Retrieves the bundle from an array of values.

createWithSampleValues(string|bool $bundle = FALSE, array $values = [])

Creates an entity with sample field values.

initFieldValues(ContentEntityInterface $entity, array $values = [], array $field_names = [])

Initializes field values.

bool
isAnyRevisionTranslated(TranslatableInterface $entity)

Checks whether any entity revision is translated.

bool
isAnyStoredRevisionTranslated(TranslatableInterface $entity)

Checks whether any stored entity revision is translated.

createTranslation(ContentEntityInterface $entity, string $langcode, array $values = [])

Constructs a new entity translation object, without permanently saving it.

createRevision(RevisionableInterface $entity, bool $default = TRUE, $keep_untranslatable_fields = NULL)

Creates a new revision starting off from the specified entity object.

array
getRevisionTranslationMergeSkippedFieldNames()

Returns an array of field names to skip when merging revision translations.

int|string|null
getLatestRevisionId(int|string $entity_id)

Returns the latest revision identifier for an entity.

int|string|null
getLatestTranslationAffectedRevisionId(int|string $entity_id, string $langcode)

Returns the latest revision affecting the specified translation.

onFieldStorageDefinitionCreate(FieldStorageDefinitionInterface $storage_definition)

Reacts to the creation of a field storage definition.

onFieldStorageDefinitionUpdate(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original)

Reacts to the update of a field storage definition.

onFieldStorageDefinitionDelete(FieldStorageDefinitionInterface $storage_definition)

Reacts to the deletion of a field storage definition.

onFieldDefinitionCreate(FieldDefinitionInterface $field_definition)

Reacts to the creation of a field.

onFieldDefinitionUpdate(FieldDefinitionInterface $field_definition, FieldDefinitionInterface $original)

Reacts to the update of a field.

onFieldDefinitionDelete(FieldDefinitionInterface $field_definition)

Reacts to the deletion of a field.

int
purgeFieldData(FieldDefinitionInterface $field_definition, int $batch_size)

Purges a batch of field data.

readFieldItemsToPurge(FieldDefinitionInterface $field_definition, $batch_size)

Reads values to be purged for a single field.

purgeFieldItems(ContentEntityInterface $entity, FieldDefinitionInterface $field_definition)

Removes field items from storage per entity during purge.

finalizePurge(FieldStorageDefinitionInterface $storage_definition)

Performs final cleanup after all data of a field has been purged.

loadRevision(int|string $revision_id)

Load a specific entity revision.

loadMultipleRevisions(array $revision_ids)

Loads multiple entity revisions.

doLoadMultipleRevisionsFieldItems(array $revision_ids)

Actually loads revision field item values from the storage.

doSaveFieldItems(ContentEntityInterface $entity, array $names = [])

Writes entity field values to the storage.

doDeleteFieldItems(ContentEntityInterface[] $entities)

Deletes entity field values from the storage.

deleteRevision(int $revision_id)

Delete a specific entity revision.

doDeleteRevisionFieldItems(ContentEntityInterface $revision)

Deletes field values of an entity revision from the storage.

invokeTranslationHooks(ContentEntityInterface $entity)

Checks translation statuses and invoke the related hooks if needed.

invokeStorageLoadHook(array $entities)

Invokes hook_entity_storage_load().

array
invokeFieldMethod(string $method, ContentEntityInterface $entity)

Invokes a method on the Field objects within an entity.

invokeFieldPostSave(ContentEntityInterface $entity, bool $update)

Invokes the post save method on the Field objects within an entity.

bool
hasFieldValueChanged(FieldDefinitionInterface $field_definition, ContentEntityInterface $entity, ContentEntityInterface $original)

Checks whether the field values changed compared to the original entity.

populateAffectedRevisionTranslations(ContentEntityInterface $entity)

Populates the affected flag for all the revision translations.

array
cleanIds(array $ids, string $entity_key = 'id')

Ensures integer entity key values are valid.

getFromPersistentCache(array $ids = NULL)

Gets entities from the persistent cache backend.

setPersistentCache(ContentEntityInterface[] $entities)

Stores entities in the persistent cache backend.

initTableLayout()

Initializes table name variables.

string
getBaseTable()

Gets the base table name.

string|false
getRevisionTable()

Gets the revision table name.

string|false
getDataTable()

Gets the data table name.

string|false
getRevisionDataTable()

Gets the revision data table name.

getStorageSchema()

Gets the entity type's storage schema object.

setEntityType(EntityTypeInterface $entity_type)

Updates the wrapped entity type definition.

setFieldStorageDefinitions(array $field_storage_definitions)

Updates the internal list of field storage definitions.

setTableMapping(TableMappingInterface $table_mapping)

Sets the wrapped table mapping definition.

setTemporary(bool $temporary)

Changes the temporary state of the storage.

getTableMapping(array $storage_definitions = NULL)

Gets a table mapping for the entity's SQL tables.

getCustomTableMapping(ContentEntityTypeInterface $entity_type, array $storage_definitions, string $prefix = '')

Gets a table mapping for the specified entity type and storage definitions.

getFromStorage(array $ids = NULL)

Gets entities from the storage.

loadFromSharedTables(array $values, array $translations, bool $load_from_revision)

Loads values for fields stored in the shared data tables.

buildQuery(array|null $ids, array|bool $revision_ids = FALSE)

Builds the query to load the entity.

saveToSharedTables(ContentEntityInterface $entity, string $table_name = NULL, bool $new_revision = NULL)

Saves fields that use the shared tables.

object
mapToStorageRecord(ContentEntityInterface $entity, string $table_name = NULL)

Maps from an entity object to the storage record.

bool
isColumnSerial($table_name, $schema_name)

Checks whether a field column should be treated as serial.

object
mapToDataStorageRecord(EntityInterface $entity, string $table_name = NULL)

Maps from an entity object to the storage record of the field data.

int
saveRevision(ContentEntityInterface $entity)

Saves an entity revision.

loadFromDedicatedTables(array $values, bool $load_from_revision)

Loads values of fields stored in dedicated tables for a group of entities.

saveToDedicatedTables(ContentEntityInterface $entity, bool $update = TRUE, string[] $names = [])

Saves values of fields that use dedicated tables.

deleteFromDedicatedTables(ContentEntityInterface $entity)

Deletes values of fields in dedicated tables for all revisions.

deleteRevisionFromDedicatedTables(ContentEntityInterface $entity)

Deletes values of fields in dedicated tables for all revisions.

bool
requiresEntityStorageSchemaChanges(EntityTypeInterface $entity_type, EntityTypeInterface $original)

Checks if the changes to the entity type requires storage schema changes.

bool
requiresFieldStorageSchemaChanges(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original)

Checks if the changes to the storage definition requires schema changes.

bool
requiresEntityDataMigration(EntityTypeInterface $entity_type, EntityTypeInterface $original)

Checks if existing data would be lost if the schema changes were applied.

bool
requiresFieldDataMigration(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original)

Checks if existing data would be lost if the schema changes were applied.

onEntityTypeCreate(EntityTypeInterface $entity_type)

Reacts to the creation of the entity type.

onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original)

Reacts to the update of the entity type.

onEntityTypeDelete(EntityTypeInterface $entity_type)

Reacts to the deletion of the entity type.

onFieldableEntityTypeCreate(EntityTypeInterface $entity_type, array $field_storage_definitions)

Reacts to the creation of the fieldable entity type.

onFieldableEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original, array $field_storage_definitions, array $original_field_storage_definitions, array $sandbox = NULL)

Reacts to the update of a fieldable entity type.

wrapSchemaException(callable $callback)

Wraps a database schema exception into an entity storage exception.

onBundleCreate(string $bundle, string $entity_type_id)

Reacts to a bundle being created.

onBundleDelete(string $bundle, string $entity_type_id)

Reacts to a bundle being deleted.

bool|int
countFieldData(FieldStorageDefinitionInterface $storage_definition, bool $as_bool = FALSE)

Determines the number of entities with values for a given field.

Details

protected TranslatableMarkup t(string $string, array $args = [], array $options = [])

Translates a string to the current language or to a given language.

See \Drupal\Core\StringTranslation\TranslatableMarkup::__construct() for important security information and usage guidelines.

In order for strings to be localized, make them available in one of the ways supported by the

Parameters

string $string

A string containing the English text to translate.

array $args

(optional) An associative array of replacements to make after translation. Based on the first character of the key, the value is escaped and/or themed. See \Drupal\Component\Render\FormattableMarkup::placeholderFormat() for details.

array $options

(optional) An associative array of additional options, with the following elements:

  • 'langcode' (defaults to the current language): A language code, to translate to a language other than what is used to display the page.
  • 'context' (defaults to the empty context): The context the source string belongs to. See the @link i18n Internationalization topic @endlink for more information about string contexts.

Return Value

TranslatableMarkup

An object that, when cast to a string, returns the translated string.

See also

FormattableMarkup::placeholderFormat
TranslatableMarkup::__construct

protected formatPlural($count, $singular, $plural, array $args = [], array $options = [])

Formats a string containing a count of items.

Parameters

$count
$singular
$plural
array $args
array $options

See also

TranslationInterface::formatPlural

protected getNumberOfPlurals($langcode = NULL)

Returns the number of plurals supported by a given language.

Parameters

$langcode

See also

PluralFormulaInterface::getNumberOfPlurals

protected TranslationInterface getStringTranslation()

Gets the string translation service.

Return Value

TranslationInterface

The string translation service.

$this setStringTranslation(TranslationInterface $translation)

Sets the string translation service to use.

Parameters

TranslationInterface $translation

The string translation service.

Return Value

$this

__sleep()

{@inheritdoc}

__wakeup()

{@inheritdoc}

protected ModuleHandlerInterface moduleHandler()

Gets the module handler.

Return Value

ModuleHandlerInterface

The module handler.

$this setModuleHandler(ModuleHandlerInterface $module_handler)

Sets the module handler for this handler.

Parameters

ModuleHandlerInterface $module_handler

The module handler.

Return Value

$this

__construct(EntityTypeInterface $entity_type, Connection $database, EntityFieldManagerInterface $entity_field_manager, CacheBackendInterface $cache, LanguageManagerInterface $language_manager, MemoryCacheInterface $memory_cache, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityTypeManagerInterface $entity_type_manager)

Constructs a SqlContentEntityStorage object.

Parameters

EntityTypeInterface $entity_type

The entity type definition.

Connection $database

The database connection to be used.

EntityFieldManagerInterface $entity_field_manager

The entity field manager.

CacheBackendInterface $cache

The cache backend to be used.

LanguageManagerInterface $language_manager

The language manager.

MemoryCacheInterface $memory_cache

The memory cache.

EntityTypeBundleInfoInterface $entity_type_bundle_info

The entity type bundle info.

EntityTypeManagerInterface $entity_type_manager

The entity type manager.

string getEntityClass(string|null $bundle = NULL)

Retrieves the class name used to create the entity.

Parameters

string|null $bundle

(optional) A specific entity type bundle identifier. Can be omitted in the case of entity types without bundles, like User.

Return Value

string

The entity class name.

__get(string $name)

Warns subclasses not to directly access the deprecated entityClass property.

Remove this in Drupal 10.

Parameters

string $name

The name of the property to get.

See also

https://www.drupal.org/project/drupal/issues/3244802

void __set(string $name, mixed $value)

Warns subclasses not to directly set the deprecated entityClass property.

Remove this in Drupal 10.

Parameters

string $name

The name of the property to set.

mixed $value

The value to use.

Return Value

void

See also

https://www.drupal.org/project/drupal/issues/3244802

string getEntityTypeId()

Gets the entity type ID.

Return Value

string

The entity type ID.

EntityTypeInterface getEntityType()

Gets the entity type definition.

Return Value

EntityTypeInterface

Entity type definition.

protected string buildCacheId(int $id)

Builds the cache ID for the passed in entity ID.

Parameters

int $id

Entity ID for which the cache ID should be built.

Return Value

string

Cache ID that can be passed to the cache backend.

EntityInterface|null loadUnchanged(mixed $id)

Loads an unchanged entity from the database.

Parameters

mixed $id

The ID of the entity to load.

Return Value

EntityInterface|null

The unchanged entity, or NULL if the entity cannot be loaded.

resetCache(array $ids = NULL)

Resets the entity cache.

Content entities have both an in-memory static cache and a persistent cache. Use this method to clear all caches. To clear just the in-memory cache, use the 'entity.memory_cache' service.

Parameters

array $ids

(optional) If specified, the cache is reset for the entities with the given ids only.

protected EntityInterface[] getFromStaticCache(array $ids)

Gets entities from the static cache.

Parameters

array $ids

If not empty, return entities that match these IDs.

Return Value

EntityInterface[]

Array of entities from the entity cache, keyed by entity ID.

protected setStaticCache(array $entities)

Stores entities in the static entity cache.

Parameters

array $entities

Entities to store in the cache.

protected invokeHook(string $hook, EntityInterface $entity)

Invokes a hook on behalf of the entity.

Parameters

string $hook

One of 'create', 'presave', 'insert', 'update', 'predelete', 'delete', or 'revision_delete'.

EntityInterface $entity

The entity object.

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

A new entity object.

protected EntityInterface doCreate(array $values)

Performs storage-specific creation of entities.

Parameters

array $values

An array of values to set, keyed by property name.

Return Value

EntityInterface

EntityInterface|null load(mixed $id)

Loads one entity.

Parameters

mixed $id

The ID of the entity to load.

Return Value

EntityInterface|null

An entity object. NULL if no matching entity is found.

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. Returns an empty array if no matching entities are found.

protected EntityInterface[] doLoadMultiple(array $ids = NULL)

Performs storage-specific loading of entities.

Override this method to add custom functionality directly after loading. This is always called, while self::postLoad() is only called when there are actual results.

Parameters

array $ids

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

Return Value

EntityInterface[]

Associative array of entities, keyed on the entity ID.

protected EntityInterface[] preLoad(array $ids = NULL)

Gathers entities from a 'preload' step.

Parameters

array $ids

If not empty, return entities that match these IDs. IDs that were found will be removed from the list.

Return Value

EntityInterface[]

Associative array of entities, keyed by the entity ID.

protected postLoad(array $entities)

Attaches data to entities upon loading.

If there are multiple bundle classes involved, each one gets a sub array with only the entities of the same bundle. If there's only a single bundle, the entity's postLoad() method will get a copy of the original $entities array.

Parameters

array $entities

Associative array of query results, keyed on the entity ID.

protected EntityInterface[] mapFromStorageRecords(array $records, bool $load_from_revision = FALSE)

Maps from storage records to entity objects, and attaches fields.

Parameters

array $records

Associative array of query results, keyed on the entity ID.

bool $load_from_revision

(optional) Flag to indicate whether revisions should be loaded or not. Defaults to FALSE.

Return Value

EntityInterface[]

An array of entity objects implementing the EntityInterface.

protected bool has(int|string $id, EntityInterface $entity)

Determines if this entity already exists in storage.

Parameters

int|string $id

The original entity ID.

EntityInterface $entity

The entity being saved.

Return Value

bool

TRUE if this entity exists in storage, FALSE otherwise.

delete(array $entities)

Deletes permanently saved entities.

Parameters

array $entities

An array of entity objects to delete.

Exceptions

EntityStorageException

protected doDelete(EntityInterface[] $entities)

Performs storage-specific entity deletion.

Parameters

EntityInterface[] $entities

An array of entity objects to delete.

SAVED_NEW save(EntityInterface $entity)

Saves the entity permanently.

Parameters

EntityInterface $entity

The entity to save.

Return Value

SAVED_NEW

or SAVED_UPDATED is returned depending on the operation performed.

Exceptions

EntityStorageException

protected int|string doPreSave(EntityInterface $entity)

Performs presave entity processing.

Parameters

EntityInterface $entity

The saved entity.

Return Value

int|string

The processed entity identifier.

Exceptions

EntityStorageException

protected bool|int doSave(int|string $id, EntityInterface $entity)

Performs storage-specific saving of the entity.

Parameters

int|string $id

The original entity ID.

EntityInterface $entity

The entity to save.

Return Value

bool|int

If the record insert or update failed, returns FALSE. If it succeeded, returns SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

protected doPostSave(EntityInterface $entity, bool $update)

Performs post save entity processing.

Parameters

EntityInterface $entity

The saved entity.

bool $update

Specifies whether the entity is being updated or created.

restore(EntityInterface $entity)

Restores a previously saved entity.

Note that the entity is assumed to be in a valid state for the storage, so the restore process does not invoke any hooks, nor does it perform any pre or post-save operations.

Parameters

EntityInterface $entity

The entity to restore.

Exceptions

EntityStorageException

protected buildPropertyQuery(QueryInterface $entity_query, array $values)

Builds an entity query.

Parameters

QueryInterface $entity_query

EntityQuery instance.

array $values

An associative array of properties of the entity, where the keys are the property names and the values are the values those properties must have.

EntityInterface[] loadByProperties(array $values = [])

Load entities by their property values.

Parameters

array $values

An associative array where the keys are the property names and the values are the values those properties must have.

Return Value

EntityInterface[]

An array of entity objects indexed by their ids.

bool hasData()

Determines if the storage contains any data.

Return Value

bool

TRUE if the storage contains data, FALSE if not.

QueryInterface getQuery(string $conjunction = 'AND')

Gets an entity query instance.

Parameters

string $conjunction

(optional) The logical operator for the query, either:

  • AND: all of the conditions on the query need to match.
  • OR: at least one of the conditions on the query need to match.

Return Value

QueryInterface

The query instance.

QueryAggregateInterface getAggregateQuery(string $conjunction = 'AND')

Gets an aggregated query instance.

Parameters

string $conjunction

(optional) The logical operator for the query, either:

  • AND: all of the conditions on the query need to match.
  • OR: at least one of the conditions on the query need to match.

Return Value

QueryAggregateInterface

The aggregated query object that can query the given entity type.

protected string getQueryServiceName()

Gets the name of the service for the query for this entity storage.

Return Value

string

The name of the service for the query for this entity storage.

protected array getEntitiesByClass(array $entities)

Indexes the given array of entities by their class name and ID.

Parameters

array $entities

The array of entities to index.

Return Value

array

An array of the passed-in entities, indexed by their class name and ID.

static EntityHandlerInterface createInstance(ContainerInterface $container, EntityTypeInterface $entity_type)

Instantiates a new instance of this entity handler.

This is a factory method that returns a new instance of this object. The factory should pass any needed dependencies into the constructor of this object, but not the container itself. Every call to this method must return a new instance of this object; that is, it may not implement a singleton.

Parameters

ContainerInterface $container

The service container this object should use.

EntityTypeInterface $entity_type

The entity type definition.

Return Value

EntityHandlerInterface

A new instance of the entity handler.

string|null getBundleFromClass(string $class_name)

Retrieves the bundle name for a provided class name.

Parameters

string $class_name

The class name to check.

Return Value

string|null

The bundle name of the class provided or NULL if unable to determine the bundle from the provided class.

Exceptions

AmbiguousBundleClassException

protected string|null getBundleFromValues(array $values)

Retrieves the bundle from an array of values.

Parameters

array $values

An array of values to set, keyed by field name.

Return Value

string|null

The bundle or NULL if not set.

Exceptions

EntityStorageException

FieldableEntityInterface createWithSampleValues(string|bool $bundle = FALSE, array $values = [])

Creates an entity with sample field values.

Parameters

string|bool $bundle

(optional) The entity bundle.

array $values

(optional) Any default values to use during generation.

Return Value

FieldableEntityInterface

A fieldable content entity.

Exceptions

EntityStorageException

protected initFieldValues(ContentEntityInterface $entity, array $values = [], array $field_names = [])

Initializes field values.

Parameters

ContentEntityInterface $entity

An entity object.

array $values

(optional) An associative array of initial field values keyed by field name. If none is provided default values will be applied.

array $field_names

(optional) An associative array of field names to be initialized. If none is provided all fields will be initialized.

protected bool isAnyRevisionTranslated(TranslatableInterface $entity)

Checks whether any entity revision is translated.

Parameters

TranslatableInterface $entity

The entity object to be checked.

Return Value

bool

TRUE if the entity has at least one translation in any revision, FALSE otherwise.

See also

TranslatableInterface::getTranslationLanguages
ContentEntityStorageBase::isAnyStoredRevisionTranslated

protected bool isAnyStoredRevisionTranslated(TranslatableInterface $entity)

Checks whether any stored entity revision is translated.

A revisionable entity can have translations in a pending revision, hence the default revision may appear as not translated. This determines whether the entity has any translation in the storage and thus should be considered as multilingual.

Parameters

TranslatableInterface $entity

The entity object to be checked.

Return Value

bool

TRUE if the entity has at least one translation in any revision, FALSE otherwise.

See also

TranslatableInterface::getTranslationLanguages
ContentEntityStorageBase::isAnyRevisionTranslated

ContentEntityInterface createTranslation(ContentEntityInterface $entity, string $langcode, array $values = [])

Constructs a new entity translation object, without permanently saving it.

Parameters

ContentEntityInterface $entity

The entity object being translated.

string $langcode

The translation language code.

array $values

(optional) An associative array of initial field values keyed by field name. If none is provided default values will be applied.

Return Value

ContentEntityInterface

Another instance of the specified entity object class with the specified active language and initial values.

RevisionableInterface createRevision(RevisionableInterface $entity, bool $default = TRUE, $keep_untranslatable_fields = NULL)

Creates a new revision starting off from the specified entity object.

Parameters

RevisionableInterface $entity

The revisionable entity object being modified.

bool $default

(optional) Whether the new revision should be marked as default. Defaults to TRUE.

$keep_untranslatable_fields

Return Value

RevisionableInterface

A new entity revision object.

protected array getRevisionTranslationMergeSkippedFieldNames()

Returns an array of field names to skip when merging revision translations.

Return Value

array

An array of field names.

int|string|null getLatestRevisionId(int|string $entity_id)

Returns the latest revision identifier for an entity.

Parameters

int|string $entity_id

The entity identifier.

Return Value

int|string|null

The latest revision identifier or NULL if no revision could be found.

int|string|null getLatestTranslationAffectedRevisionId(int|string $entity_id, string $langcode)

Returns the latest revision affecting the specified translation.

Parameters

int|string $entity_id

The entity identifier.

string $langcode

The language code of the translation.

Return Value

int|string|null

A revision ID or NULL if no revision affecting the specified translation could be found.

onFieldStorageDefinitionCreate(FieldStorageDefinitionInterface $storage_definition)

Reacts to the creation of a field storage definition.

Parameters

FieldStorageDefinitionInterface $storage_definition

The definition being created.

onFieldStorageDefinitionUpdate(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original)

Reacts to the update of a field storage definition.

Parameters

FieldStorageDefinitionInterface $storage_definition

The field being updated.

FieldStorageDefinitionInterface $original

The original storage definition; i.e., the definition before the update.

Exceptions

FieldStorageDefinitionUpdateForbiddenException

onFieldStorageDefinitionDelete(FieldStorageDefinitionInterface $storage_definition)

Reacts to the deletion of a field storage definition.

Parameters

FieldStorageDefinitionInterface $storage_definition

The field being deleted.

onFieldDefinitionCreate(FieldDefinitionInterface $field_definition)

Reacts to the creation of a field.

Parameters

FieldDefinitionInterface $field_definition

The field definition created.

onFieldDefinitionUpdate(FieldDefinitionInterface $field_definition, FieldDefinitionInterface $original)

Reacts to the update of a field.

Parameters

FieldDefinitionInterface $field_definition

The field definition being updated.

FieldDefinitionInterface $original

The original field definition; i.e., the definition before the update.

onFieldDefinitionDelete(FieldDefinitionInterface $field_definition)

Reacts to the deletion of a field.

Stored values should not be wiped at once, but marked as 'deleted' so that they can go through a proper purge process later on.

Parameters

FieldDefinitionInterface $field_definition

The field definition being deleted.

int purgeFieldData(FieldDefinitionInterface $field_definition, int $batch_size)

Purges a batch of field data.

Parameters

FieldDefinitionInterface $field_definition

The deleted field whose data is being purged.

int $batch_size

The maximum number of field data records to purge before returning, relating to the count of field data records returned by \Drupal\Core\Entity\FieldableEntityStorageInterface::countFieldData().

Return Value

int

The number of field data records that have been purged.

protected FieldItemListInterface[] readFieldItemsToPurge(FieldDefinitionInterface $field_definition, $batch_size)

Reads values to be purged for a single field.

This method is called during field data purge, on fields for which onFieldDefinitionDelete() has previously run.

Parameters

FieldDefinitionInterface $field_definition

The field definition.

$batch_size

The maximum number of field data records to purge before returning.

Return Value

FieldItemListInterface[]

An array of field item lists, keyed by entity revision id.

protected purgeFieldItems(ContentEntityInterface $entity, FieldDefinitionInterface $field_definition)

Removes field items from storage per entity during purge.

Parameters

ContentEntityInterface $entity

The entity revision, whose values are being purged.

FieldDefinitionInterface $field_definition

The field whose values are bing purged.

finalizePurge(FieldStorageDefinitionInterface $storage_definition)

Performs final cleanup after all data of a field has been purged.

Parameters

FieldStorageDefinitionInterface $storage_definition

The field storage being purged.

EntityInterface|null loadRevision(int|string $revision_id)

Load a specific entity revision.

Parameters

int|string $revision_id

The revision id.

Return Value

EntityInterface|null

The specified entity revision or NULL if not found.

RevisionableInterface[] loadMultipleRevisions(array $revision_ids)

Loads multiple entity revisions.

Parameters

array $revision_ids

An array of revision IDs to load.

Return Value

RevisionableInterface[]

An array of entity revisions keyed by their revision ID, or an empty array if none found.

protected EntityInterface[] doLoadMultipleRevisionsFieldItems(array $revision_ids)

Actually loads revision field item values from the storage.

Parameters

array $revision_ids

An array of revision identifiers.

Return Value

EntityInterface[]

The specified entity revisions or an empty array if none are found.

protected doSaveFieldItems(ContentEntityInterface $entity, array $names = [])

Writes entity field values to the storage.

This method is responsible for allocating entity and revision identifiers and updating the entity object with their values.

Parameters

ContentEntityInterface $entity

The entity object.

array $names

(optional) The name of the fields to be written to the storage. If an empty value is passed all field values are saved.

protected doDeleteFieldItems(ContentEntityInterface[] $entities)

Deletes entity field values from the storage.

Parameters

ContentEntityInterface[] $entities

An array of entity objects to be deleted.

deleteRevision(int $revision_id)

Delete a specific entity revision.

A revision can only be deleted if it's not the currently active one.

Parameters

int $revision_id

The revision id.

protected doDeleteRevisionFieldItems(ContentEntityInterface $revision)

Deletes field values of an entity revision from the storage.

Parameters

ContentEntityInterface $revision

An entity revision object to be deleted.

protected invokeTranslationHooks(ContentEntityInterface $entity)

Checks translation statuses and invoke the related hooks if needed.

Parameters

ContentEntityInterface $entity

The entity being saved.

protected invokeStorageLoadHook(array $entities)

Invokes hook_entity_storage_load().

Parameters

array $entities

List of entities, keyed on the entity ID.

protected array invokeFieldMethod(string $method, ContentEntityInterface $entity)

Invokes a method on the Field objects within an entity.

Any argument passed will be forwarded to the invoked method.

Parameters

string $method

The name of the method to be invoked.

ContentEntityInterface $entity

The entity object.

Return Value

array

A multidimensional associative array of results, keyed by entity translation language code and field name.

protected invokeFieldPostSave(ContentEntityInterface $entity, bool $update)

Invokes the post save method on the Field objects within an entity.

Parameters

ContentEntityInterface $entity

The entity object.

bool $update

Specifies whether the entity is being updated or created.

protected bool hasFieldValueChanged(FieldDefinitionInterface $field_definition, ContentEntityInterface $entity, ContentEntityInterface $original)

Checks whether the field values changed compared to the original entity.

Parameters

FieldDefinitionInterface $field_definition

Field definition of field to compare for changes.

ContentEntityInterface $entity

Entity to check for field changes.

ContentEntityInterface $original

Original entity to compare against.

Return Value

bool

True if the field value changed from the original entity.

protected populateAffectedRevisionTranslations(ContentEntityInterface $entity)

Populates the affected flag for all the revision translations.

Parameters

ContentEntityInterface $entity

An entity object being saved.

protected array cleanIds(array $ids, string $entity_key = 'id')

Ensures integer entity key values are valid.

The identifier sanitization provided by this method has been introduced as Drupal used to rely on the database to facilitate this, which worked correctly with MySQL but led to errors with other DBMS such as PostgreSQL.

Parameters

array $ids

The entity key values to verify.

string $entity_key

(optional) The entity key to sanitize values for. Defaults to 'id'.

Return Value

array

The sanitized list of entity key values.

protected ContentEntityInterface[] getFromPersistentCache(array $ids = NULL)

Gets entities from the persistent cache backend.

Parameters

array $ids

If not empty, return entities that match these IDs. IDs that were found will be removed from the list.

Return Value

ContentEntityInterface[]

Array of entities from the persistent cache.

protected setPersistentCache(ContentEntityInterface[] $entities)

Stores entities in the persistent cache backend.

Parameters

ContentEntityInterface[] $entities

Entities to store in the cache.

protected initTableLayout()

Initializes table name variables.

string getBaseTable()

Gets the base table name.

Return Value

string

The table name.

string|false getRevisionTable()

Gets the revision table name.

Return Value

string|false

The table name or FALSE if it is not available.

string|false getDataTable()

Gets the data table name.

Return Value

string|false

The table name or FALSE if it is not available.

string|false getRevisionDataTable()

Gets the revision data table name.

Return Value

string|false

The table name or FALSE if it is not available.

protected SqlContentEntityStorageSchema getStorageSchema()

Gets the entity type's storage schema object.

Return Value

SqlContentEntityStorageSchema

The schema object.

setEntityType(EntityTypeInterface $entity_type)

internal  Only to be used internally by Entity API. Expected to be removed by https://www.drupal.org/node/2274017.
 

Updates the wrapped entity type definition.

Parameters

EntityTypeInterface $entity_type

The update entity type.

setFieldStorageDefinitions(array $field_storage_definitions)

internal  Only to be used internally by Entity API.
 

Updates the internal list of field storage definitions.

Parameters

array $field_storage_definitions

An array of field storage definitions.

setTableMapping(TableMappingInterface $table_mapping)

internal  Only to be used internally by Entity API. Expected to be removed by https://www.drupal.org/node/2554235.
 

Sets the wrapped table mapping definition.

Parameters

TableMappingInterface $table_mapping

The table mapping.

setTemporary(bool $temporary)

internal  Only to be used internally by Entity API.
 

Changes the temporary state of the storage.

Parameters

bool $temporary

Whether to use a temporary table mapping or not.

TableMappingInterface getTableMapping(array $storage_definitions = NULL)

Gets a table mapping for the entity's SQL tables.

Parameters

array $storage_definitions

(optional) An array of field storage definitions to be used to compute the table mapping. Defaults to the ones provided by the entity field manager.

Return Value

TableMappingInterface

A table mapping object for the entity's tables.

TableMappingInterface getCustomTableMapping(ContentEntityTypeInterface $entity_type, array $storage_definitions, string $prefix = '')

internal  
 

Gets a table mapping for the specified entity type and storage definitions.

Parameters

ContentEntityTypeInterface $entity_type

An entity type definition.

array $storage_definitions

An array of field storage definitions to be used to compute the table mapping.

string $prefix

(optional) A prefix to be used by all the tables of this mapping. Defaults to an empty string.

Return Value

TableMappingInterface

A table mapping object for the entity's tables.

protected ContentEntityInterface[] getFromStorage(array $ids = NULL)

Gets entities from the storage.

Parameters

array $ids

If not empty, return entities that match these IDs. Return all entities when NULL.

Return Value

ContentEntityInterface[]

Array of entities from the storage.

protected loadFromSharedTables(array $values, array $translations, bool $load_from_revision)

Loads values for fields stored in the shared data tables.

Parameters

array $values

Associative array of entities values, keyed on the entity ID or the revision ID.

array $translations

List of translations, keyed on the entity ID.

bool $load_from_revision

Flag to indicate whether revisions should be loaded or not.

protected SelectInterface buildQuery(array|null $ids, array|bool $revision_ids = FALSE)

Builds the query to load the entity.

This has full revision support. For entities requiring special queries, the class can be extended, and the default query can be constructed by calling parent::buildQuery(). This is usually necessary when the object being loaded needs to be augmented with additional data from another table, such as loading vocabulary machine name into terms, however it can also support $conditions on different tables.

Parameters

array|null $ids

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

array|bool $revision_ids

The IDs of the revisions to load, or FALSE if this query is asking for the default revisions. Defaults to FALSE.

Return Value

SelectInterface

A SelectQuery object for loading the entity.

protected saveToSharedTables(ContentEntityInterface $entity, string $table_name = NULL, bool $new_revision = NULL)

Saves fields that use the shared tables.

Parameters

ContentEntityInterface $entity

The entity object.

string $table_name

(optional) The table name to save to. Defaults to the data table.

bool $new_revision

(optional) Whether we are dealing with a new revision. By default fetches the information from the entity object.

protected object mapToStorageRecord(ContentEntityInterface $entity, string $table_name = NULL)

Maps from an entity object to the storage record.

Parameters

ContentEntityInterface $entity

The entity object.

string $table_name

(optional) The table name to map records to. Defaults to the base table.

Return Value

object

The record to store.

protected bool isColumnSerial($table_name, $schema_name)

Checks whether a field column should be treated as serial.

Parameters

$table_name

The name of the table the field column belongs to.

$schema_name

The schema name of the field column.

Return Value

bool

TRUE if the column is serial, FALSE otherwise.

protected object mapToDataStorageRecord(EntityInterface $entity, string $table_name = NULL)

Maps from an entity object to the storage record of the field data.

Parameters

EntityInterface $entity

The entity object.

string $table_name

(optional) The table name to map records to. Defaults to the data table.

Return Value

object

The record to store.

protected int saveRevision(ContentEntityInterface $entity)

Saves an entity revision.

Parameters

ContentEntityInterface $entity

The entity object.

Return Value

int

The revision id.

protected loadFromDedicatedTables(array $values, bool $load_from_revision)

Loads values of fields stored in dedicated tables for a group of entities.

Parameters

array $values

An array of values keyed by entity ID.

bool $load_from_revision

Flag to indicate whether revisions should be loaded or not.

protected saveToDedicatedTables(ContentEntityInterface $entity, bool $update = TRUE, string[] $names = [])

Saves values of fields that use dedicated tables.

Parameters

ContentEntityInterface $entity

The entity.

bool $update

TRUE if the entity is being updated, FALSE if it is being inserted.

string[] $names

(optional) The names of the fields to be stored. Defaults to all the available fields.

protected deleteFromDedicatedTables(ContentEntityInterface $entity)

Deletes values of fields in dedicated tables for all revisions.

Parameters

ContentEntityInterface $entity

The entity.

protected deleteRevisionFromDedicatedTables(ContentEntityInterface $entity)

Deletes values of fields in dedicated tables for all revisions.

Parameters

ContentEntityInterface $entity

The entity. It must have a revision ID.

bool requiresEntityStorageSchemaChanges(EntityTypeInterface $entity_type, EntityTypeInterface $original)

Checks if the changes to the entity type requires storage schema changes.

Parameters

EntityTypeInterface $entity_type

The updated entity type definition.

EntityTypeInterface $original

The original entity type definition.

Return Value

bool

TRUE if storage schema changes are required, FALSE otherwise.

bool requiresFieldStorageSchemaChanges(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original)

Checks if the changes to the storage definition requires schema changes.

Parameters

FieldStorageDefinitionInterface $storage_definition

The updated field storage definition.

FieldStorageDefinitionInterface $original

The original field storage definition.

Return Value

bool

TRUE if storage schema changes are required, FALSE otherwise.

bool requiresEntityDataMigration(EntityTypeInterface $entity_type, EntityTypeInterface $original)

Checks if existing data would be lost if the schema changes were applied.

If there are no schema changes needed, then no data needs to be migrated, but it is not the responsibility of this function to recheck what requiresEntityStorageSchemaChanges() checks. Rather, the meaning of what this function returns when requiresEntityStorageSchemaChanges() returns FALSE is undefined. Callers are expected to only call this function when requiresEntityStorageSchemaChanges() is TRUE.

This function can return FALSE if any of these conditions apply:

  • There are no existing entities for the entity type.
  • There are existing entities, but the schema changes can be applied without losing their data (e.g., if the schema changes can be performed by altering tables rather than dropping and recreating them).
  • The only entity data that would be lost are ones that are not valid for the new definition (e.g., if changing an entity type from revisionable to non-revisionable, then it's okay to drop data for the non-default revision).

When this function returns FALSE, site administrators will be unable to perform an automated update, and will instead need to perform a site migration or invoke some custom update process.

Parameters

EntityTypeInterface $entity_type

The updated entity type definition.

EntityTypeInterface $original

The original entity type definition.

Return Value

bool

TRUE if data migration is required, FALSE otherwise.

bool requiresFieldDataMigration(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original)

Checks if existing data would be lost if the schema changes were applied.

If there are no schema changes needed, then no data needs to be migrated, but it is not the responsibility of this function to recheck what requiresFieldStorageSchemaChanges() checks. Rather, the meaning of what this function returns when requiresFieldStorageSchemaChanges() returns FALSE is undefined. Callers are expected to only call this function when requiresFieldStorageSchemaChanges() is TRUE.

This function can return FALSE if any of these conditions apply:

  • There are no existing entities for the entity type to which this field is attached.
  • There are existing entities, but none with existing values for this field.
  • There are existing field values, but the schema changes can be applied without losing them (e.g., if the schema changes can be performed by altering tables rather than dropping and recreating them).
  • The only field values that would be lost are ones that are not valid for the new definition (e.g., if changing a field from revisionable to non-revisionable, then it's okay to drop data for the non-default revision).

When this function returns FALSE, site administrators will be unable to perform an automated update, and will instead need to perform a site migration or invoke some custom update process.

Parameters

FieldStorageDefinitionInterface $storage_definition

The updated field storage definition.

FieldStorageDefinitionInterface $original

The original field storage definition.

Return Value

bool

TRUE if data migration is required, FALSE otherwise.

onEntityTypeCreate(EntityTypeInterface $entity_type)

Reacts to the creation of the entity type.

Parameters

EntityTypeInterface $entity_type

The entity type being created.

onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original)

Reacts to the update of the entity type.

Parameters

EntityTypeInterface $entity_type

The updated entity type definition.

EntityTypeInterface $original

The original entity type definition.

onEntityTypeDelete(EntityTypeInterface $entity_type)

Reacts to the deletion of the entity type.

Parameters

EntityTypeInterface $entity_type

The entity type being deleted.

onFieldableEntityTypeCreate(EntityTypeInterface $entity_type, array $field_storage_definitions)

Reacts to the creation of the fieldable entity type.

Parameters

EntityTypeInterface $entity_type

The entity type being created.

array $field_storage_definitions

The entity type's field storage definitions.

onFieldableEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original, array $field_storage_definitions, array $original_field_storage_definitions, array $sandbox = NULL)

Reacts to the update of a fieldable entity type.

Parameters

EntityTypeInterface $entity_type

The updated entity type definition.

EntityTypeInterface $original

The original entity type definition.

array $field_storage_definitions

The updated field storage definitions, including possibly new ones.

array $original_field_storage_definitions

The original field storage definitions.

array $sandbox

(optional) A sandbox array provided by a hook_update_N() implementation or a Batch API callback. If the entity schema update requires a data migration, this parameter is mandatory. Defaults to NULL.

protected wrapSchemaException(callable $callback)

Wraps a database schema exception into an entity storage exception.

Parameters

callable $callback

The callback to be executed.

Exceptions

EntityStorageException

onBundleCreate(string $bundle, string $entity_type_id)

Reacts to a bundle being created.

Parameters

string $bundle

The name of the bundle created.

string $entity_type_id

The entity type to which the bundle is bound; e.g. 'node' or 'user'.

onBundleDelete(string $bundle, string $entity_type_id)

Reacts to a bundle being deleted.

This method runs before fields are deleted.

Parameters

string $bundle

The name of the bundle being deleted.

string $entity_type_id

The entity type to which the bundle is bound; e.g. 'node' or 'user'.

bool|int countFieldData(FieldStorageDefinitionInterface $storage_definition, bool $as_bool = FALSE)

Determines the number of entities with values for a given field.

Parameters

FieldStorageDefinitionInterface $storage_definition

The field for which to count data records.

bool $as_bool

(Optional) Optimizes the query for checking whether there are any records or not. Defaults to FALSE.

Return Value

bool|int

The number of entities. If $as_bool parameter is TRUE then the value will either be TRUE or FALSE.