class EntityTypeManager extends DefaultPluginManager implements EntityTypeManagerInterface, ContainerAwareInterface (View source)

Manages entity type plugin definitions.

Each entity type definition array is set in the entity type's annotation and altered by hook_entity_type_alter().

Do not use hook_entity_type_alter() hook to add information to entity types, unless one of the following is true:

  • You are filling in default values.
  • You need to dynamically add information only in certain circumstances.
  • Your hook needs to run after hook_entity_type_build() implementations. Use hook_entity_type_build() instead in all other cases.

Traits

ContainerAwareTrait

Provides methods to use a cache backend while respecting a 'use caches' flag.

Properties

protected DiscoveryInterface $discovery

The object that discovers plugins managed by this manager.

from  PluginManagerBase
protected FactoryInterface $factory

The object that instantiates plugins managed by this manager.

from  PluginManagerBase
protected MapperInterface|null $mapper

The object that returns the preconfigured plugin instance appropriate for a particular runtime condition.

from  PluginManagerBase
protected array $definitions

Cached definitions array.

from  DiscoveryCachedTrait
protected CacheBackendInterface $cacheBackend

Cache backend instance.

from  UseCacheBackendTrait
protected bool $useCaches

Flag whether caches should be used or skipped.

from  UseCacheBackendTrait
protected string $cacheKey

The cache key.

from  DefaultPluginManager
protected array $cacheTags

An array of cache tags to use for the cached definitions.

from  DefaultPluginManager
protected string $alterHook

Name of the alter hook if one should be invoked.

from  DefaultPluginManager
protected string|bool $subdir

The subdirectory within a namespace to look for plugins, or FALSE if the plugins are in the top level of the namespace.

from  DefaultPluginManager
protected ModuleHandlerInterface $moduleHandler

The module handler to invoke the alter hook.

from  DefaultPluginManager
protected array $defaults

A set of defaults to be referenced by $this->processDefinition() if additional processing of plugins is necessary or helpful for development purposes.

from  DefaultPluginManager
protected string $pluginDefinitionAnnotationName

The name of the annotation that contains the plugin definition.

from  DefaultPluginManager
protected string|null $pluginInterface

The interface each plugin should implement.

from  DefaultPluginManager
protected Traversable $namespaces

An object that implements \Traversable which contains the root paths keyed by the corresponding namespace to look for plugin implementations.

from  DefaultPluginManager
protected string[] $additionalAnnotationNamespaces

Additional namespaces the annotation discovery mechanism should scan for annotation definitions.

from  DefaultPluginManager
protected array $handlers

Contains instantiated handlers keyed by handler type and entity type.

protected TranslationInterface $stringTranslation

The string translation service.

protected ClassResolverInterface $classResolver

The class resolver.

protected EntityLastInstalledSchemaRepositoryInterface $entityLastInstalledSchemaRepository

The entity last installed schema repository.

Methods

getDefinitions()

{@inheritdoc}

getDefinition($entity_type_id, $exception_on_invalid = TRUE)

{@inheritdoc}

array|null
doGetDefinition(array $definitions, string $plugin_id, bool $exception_on_invalid)

Gets a specific plugin definition.

hasDefinition($plugin_id)

{@inheritdoc}

getDiscovery()

Gets the plugin discovery.

getFactory()

Gets the plugin factory.

object
createInstance(string $plugin_id, array $configuration = [])

Creates a pre-configured instance of a plugin.

object
handlePluginNotFound(string $plugin_id, array $configuration)

Allows plugin managers to specify custom behavior if a plugin is not found.

object|false
getInstance(array $options)

Gets a preconfigured instance of a plugin.

object|false
cacheGet(string $cid)

Fetches from the cache backend, respecting the use caches flag.

cacheSet(string $cid, mixed $data, int $expire = Cache::PERMANENT, array $tags = [])

Stores data in the persistent cache, respecting the use caches flag.

__construct(Traversable $namespaces, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache, TranslationInterface $string_translation, ClassResolverInterface $class_resolver, EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository)

Constructs a new Entity plugin manager.

setCacheBackend(CacheBackendInterface $cache_backend, string $cache_key, array $cache_tags = [])

Initialize the cache backend.

alterInfo(string $alter_hook)

Sets the alter hook name.

clearCachedDefinitions()

Clears static and persistent plugin definition caches.

array|null
getCachedDefinitions()

Returns the cached plugin definitions of the decorated discovery class.

setCachedDefinitions(array $definitions)

Sets a cache of plugin definitions for the decorated discovery class.

useCaches(bool $use_caches = FALSE)

Disable the use of caches.

processDefinition($definition, $plugin_id)

Performs extra processing on plugin definitions.

array
findDefinitions()

Finds plugin definitions.

string|null
extractProviderFromDefinition(mixed $plugin_definition)

Extracts the provider from a plugin definition.

alterDefinitions($definitions)

Invokes the hook to alter the definitions if the alter hook is set.

bool
providerExists($provider)

Determines if the provider of a definition exists.

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.

getActiveDefinition(string $entity_type_id)

Gets the active definition for a content entity type.

bool
hasHandler(string $entity_type_id, string $handler_type)

Checks whether a certain entity type has a certain handler.

getStorage(string $entity_type_id)

Creates a new storage instance.

getListBuilder(string $entity_type_id)

Creates a new entity list builder.

getFormObject(string $entity_type_id, string $operation)

Creates a new form instance.

getRouteProviders(string $entity_type_id)

Gets all route provider instances.

getViewBuilder(string $entity_type_id)

Creates a new view builder instance.

getAccessControlHandler(string $entity_type_id)

Creates a new access control handler instance.

object
getHandler(string $entity_type_id, string $handler_type)

Returns a handler instance for the given entity type and handler.

object
createHandlerInstance(mixed $class, EntityTypeInterface $definition = NULL)

Creates new handler instance.

Details

abstract getDefinitions()

{@inheritdoc}

getDefinition($entity_type_id, $exception_on_invalid = TRUE)

{@inheritdoc}

Parameters

$entity_type_id
$exception_on_invalid

protected array|null doGetDefinition(array $definitions, string $plugin_id, bool $exception_on_invalid)

Gets a specific plugin definition.

Parameters

array $definitions

An array of the available plugin definitions.

string $plugin_id

A plugin id.

bool $exception_on_invalid

If TRUE, an invalid plugin ID will cause an exception to be thrown; if FALSE, NULL will be returned.

Return Value

array|null

A plugin definition, or NULL if the plugin ID is invalid and $exception_on_invalid is TRUE.

Exceptions

PluginNotFoundException

hasDefinition($plugin_id)

{@inheritdoc}

Parameters

$plugin_id

protected DiscoveryInterface getDiscovery()

Gets the plugin discovery.

Return Value

DiscoveryInterface

protected FactoryInterface getFactory()

Gets the plugin factory.

Return Value

FactoryInterface

object createInstance(string $plugin_id, array $configuration = [])

Creates a pre-configured instance of a plugin.

Parameters

string $plugin_id

The ID of the plugin being instantiated.

array $configuration

An array of configuration relevant to the plugin instance.

Return Value

object

A fully configured plugin instance.

Exceptions

PluginException

protected object handlePluginNotFound(string $plugin_id, array $configuration)

Allows plugin managers to specify custom behavior if a plugin is not found.

Parameters

string $plugin_id

The ID of the missing requested plugin.

array $configuration

An array of configuration relevant to the plugin instance.

Return Value

object

A fallback plugin instance.

object|false getInstance(array $options)

Gets a preconfigured instance of a plugin.

Parameters

array $options

An array of options that can be used to determine a suitable plugin to instantiate and how to configure it.

Return Value

object|false

A fully configured plugin instance. The interface of the plugin instance will depend on the plugin type. If no instance can be retrieved, FALSE will be returned.

protected object|false cacheGet(string $cid)

Fetches from the cache backend, respecting the use caches flag.

Parameters

string $cid

The cache ID of the data to retrieve.

Return Value

object|false

The cache item or FALSE on failure.

See also

CacheBackendInterface::get

protected cacheSet(string $cid, mixed $data, int $expire = Cache::PERMANENT, array $tags = [])

Stores data in the persistent cache, respecting the use caches flag.

Parameters

string $cid

The cache ID of the data to store.

mixed $data

The data to store in the cache. Some storage engines only allow objects up to a maximum of 1MB in size to be stored by default. When caching large arrays or similar, take care to ensure $data does not exceed this size.

int $expire

One of the following values:

  • CacheBackendInterface::CACHE_PERMANENT: Indicates that the item should not be removed unless it is deleted explicitly.
  • A Unix timestamp: Indicates that the item will be considered invalid after this time, i.e. it will not be returned by get() unless $allow_invalid has been set to TRUE. When the item has expired, it may be permanently deleted by the garbage collector at any time.
array $tags

An array of tags to be stored with the cache item. These should normally identify objects used to build the cache item, which should trigger cache invalidation when updated. For example if a cached item represents a node, both the node ID and the author's user ID might be passed in as tags. For example array('node' => array(123), 'user' => array(92)).

See also

CacheBackendInterface::set

__construct(Traversable $namespaces, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache, TranslationInterface $string_translation, ClassResolverInterface $class_resolver, EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository)

Constructs a new Entity plugin manager.

Parameters

Traversable $namespaces

An object that implements \Traversable which contains the root paths keyed by the corresponding namespace to look for plugin implementations.

ModuleHandlerInterface $module_handler

The module handler.

CacheBackendInterface $cache

The cache backend to use.

TranslationInterface $string_translation

The string translation.

ClassResolverInterface $class_resolver

The class resolver.

EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository

The entity last installed schema repository.

setCacheBackend(CacheBackendInterface $cache_backend, string $cache_key, array $cache_tags = [])

Initialize the cache backend.

Plugin definitions are cached using the provided cache backend.

Parameters

CacheBackendInterface $cache_backend

Cache backend instance to use.

string $cache_key

Cache key prefix to use.

array $cache_tags

(optional) When providing a list of cache tags, the cached plugin definitions are tagged with the provided cache tags. These cache tags can then be used to clear the corresponding cached plugin definitions. Note that this should be used with care! For clearing all cached plugin definitions of a plugin manager, call that plugin manager's clearCachedDefinitions() method. Only use cache tags when cached plugin definitions should be cleared along with other, related cache entries.

protected alterInfo(string $alter_hook)

Sets the alter hook name.

Parameters

string $alter_hook

Name of the alter hook; for example, to invoke hook_mymodule_data_alter() pass in "mymodule_data".

clearCachedDefinitions()

Clears static and persistent plugin definition caches.

Don't resort to calling \Drupal::cache()->delete() and friends to make Drupal detect new or updated plugin definitions. Always use this method on the appropriate plugin type's plugin manager!

protected array|null getCachedDefinitions()

Returns the cached plugin definitions of the decorated discovery class.

Return Value

array|null

On success this will return an array of plugin definitions. On failure this should return NULL, indicating to other methods that this has not yet been defined. Success with no values should return as an empty array and would actually be returned by the getDefinitions() method.

protected setCachedDefinitions(array $definitions)

Sets a cache of plugin definitions for the decorated discovery class.

Parameters

array $definitions

List of definitions to store in cache.

useCaches(bool $use_caches = FALSE)

Disable the use of caches.

Can be used to ensure that uncached plugin definitions are returned, without invalidating all cached information.

This will also remove all local/static caches.

Parameters

bool $use_caches

FALSE to not use any caches.

processDefinition($definition, $plugin_id)

Performs extra processing on plugin definitions.

By default we add defaults for the type to the definition. If a type has additional processing logic they can do that by replacing or extending the method.

Parameters

$definition
$plugin_id

protected array findDefinitions()

Finds plugin definitions.

Return Value

array

List of definitions to store in cache.

protected string|null extractProviderFromDefinition(mixed $plugin_definition)

Extracts the provider from a plugin definition.

Parameters

mixed $plugin_definition

The plugin definition. Usually either an array or an instance of \Drupal\Component\Plugin\Definition\PluginDefinitionInterface

Return Value

string|null

The provider string, if it exists. NULL otherwise.

protected alterDefinitions($definitions)

Invokes the hook to alter the definitions if the alter hook is set.

Parameters

$definitions

The discovered plugin definitions.

protected bool providerExists($provider)

Determines if the provider of a definition exists.

Parameters

$provider

Return Value

bool

TRUE if provider exists, FALSE otherwise.

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.

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.

EntityTypeInterface getActiveDefinition(string $entity_type_id)

internal  
 

Gets the active definition for a content entity type.

Parameters

string $entity_type_id

The entity type ID.

Return Value

EntityTypeInterface

The active entity type definition.

bool hasHandler(string $entity_type_id, string $handler_type)

Checks whether a certain entity type has a certain handler.

Parameters

string $entity_type_id

The ID of the entity type.

string $handler_type

The name of the handler.

Return Value

bool

Returns TRUE if the entity type has the handler, else FALSE.

EntityStorageInterface getStorage(string $entity_type_id)

Creates a new storage instance.

Parameters

string $entity_type_id

The entity type ID for this storage.

Return Value

EntityStorageInterface

A storage instance.

Exceptions

PluginNotFoundException
InvalidPluginDefinitionException

EntityListBuilderInterface getListBuilder(string $entity_type_id)

Creates a new entity list builder.

Parameters

string $entity_type_id

The entity type ID for this list builder.

Return Value

EntityListBuilderInterface

An entity list builder instance.

EntityFormInterface getFormObject(string $entity_type_id, string $operation)

Creates a new form instance.

Parameters

string $entity_type_id

The entity type ID for this form.

string $operation

The name of the operation to use, e.g., 'default'.

Return Value

EntityFormInterface

A form instance.

EntityRouteProviderInterface[] getRouteProviders(string $entity_type_id)

Gets all route provider instances.

Parameters

string $entity_type_id

The entity type ID for the route providers.

Return Value

EntityRouteProviderInterface[]

EntityViewBuilderInterface getViewBuilder(string $entity_type_id)

Creates a new view builder instance.

Parameters

string $entity_type_id

The entity type ID for this view builder.

Return Value

EntityViewBuilderInterface

A view builder instance.

EntityAccessControlHandlerInterface getAccessControlHandler(string $entity_type_id)

Creates a new access control handler instance.

Parameters

string $entity_type_id

The entity type ID for this access control handler.

Return Value

EntityAccessControlHandlerInterface

An access control handler instance.

object getHandler(string $entity_type_id, string $handler_type)

Returns a handler instance for the given entity type and handler.

Entity handlers are instantiated once per entity type and then cached in the entity type manager, and so subsequent calls to getHandler() for a particular entity type and handler type will return the same object. This means that properties on a handler may be used as a static cache, although as the handler is common to all entities of the same type, any data that is per-entity should be keyed by the entity ID.

Parameters

string $entity_type_id

The entity type ID for this handler.

string $handler_type

The handler type to create an instance for.

Return Value

object

A handler instance.

Exceptions

InvalidPluginDefinitionException

object createHandlerInstance(mixed $class, EntityTypeInterface $definition = NULL)

Creates new handler instance.

Usually \Drupal\Core\Entity\EntityTypeManagerInterface::getHandler() is preferred since that method has additional checking that the class exists and has static caches.

Parameters

mixed $class

The handler class to instantiate.

EntityTypeInterface $definition

The entity type definition.

Return Value

object

A handler instance.