Skip to content

EntityType

Provides helpers for working with entity types.


  • Full name: \Drupal\helper\EntityType
  • This class implements: LoggerAwareInterface

Properties

entityTypeManager

protected \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager

entityTypeBundleInfo

protected \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entityTypeBundleInfo

selectionPluginManager

protected \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface $selectionPluginManager

entityFieldManager

protected \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager

entityDisplayRepository

protected \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entityDisplayRepository

accessHelper

protected \Drupal\helper\AccessHelper $accessHelper

memoryCache

protected \Drupal\Core\Cache\CacheBackendInterface $memoryCache

Methods

__construct

EntityType constructor.

public __construct(\Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager, \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entityTypeBundleInfo, \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface $selectionPluginManager, \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager, \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entityDisplayRepository, \Drupal\helper\AccessHelper $accessHelper, \Drupal\Core\Cache\CacheBackendInterface $memoryCache): mixed

Parameters:

Parameter Type Description
$entityTypeManager \Drupal\Core\Entity\EntityTypeManagerInterface
The entity type manager.
$entityTypeBundleInfo \Drupal\Core\Entity\EntityTypeBundleInfoInterface
The entity type bundle info.
$selectionPluginManager \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface
The entity reference selection plugin manager.
$entityFieldManager \Drupal\Core\Entity\EntityFieldManagerInterface
The entity field manager.
$entityDisplayRepository \Drupal\Core\Entity\EntityDisplayRepositoryInterface
The entity display repository.
$accessHelper \Drupal\helper\AccessHelper
The access helper.
$memoryCache \Drupal\Core\Cache\CacheBackendInterface
The memory cache.

getEntityOptions

Get a list of entity options for a select field.

public getEntityOptions(string $entity_type_id, array|null $properties = null): array|\Drupal\Core\StringTranslation\TranslatableMarkup[]|string[]

Parameters:

Parameter Type Description
$entity_type_id string
The entity type ID.
$properties array|null
An optional array of properties to use when loading the entities.

Return Value:

The list of entities keyed by entity ID.


getBundleLabel

Get the label for an entity type's bundle.

public getBundleLabel(string $entity_type_id, string $bundle): string

Parameters:

Parameter Type Description
$entity_type_id string
The entity type ID.
$bundle string
The bundle from the entity.

Return Value:

The label of the bundle.


getBundles

Gets the bundle IDs of an entity type.

public getBundles(string $entity_type_id): string[]

Unlike the keys of the bundle info, bundle IDs made only of digits, like "1984", are strings, not integers.

Parameters:

Parameter Type Description
$entity_type_id string
The entity type ID.

Return Value:

The bundle IDs, in the order of the bundle info. Empty if the entity type has no bundle info.


getBundleOptions

Gets an array of bundle labels for an entity type.

public getBundleOptions(string $entity_type_id): array<string|int,string>

Parameters:

Parameter Type Description
$entity_type_id string
The entity type ID.

Return Value:

An array of bundle labels, keyed by bundle ID. As with any PHP array, a bundle ID made only of digits, like "1984", while it might be rare, could still be an integer key instead of a string.


getEntityReferenceSelection

Get an entity reference selection handler.

public getEntityReferenceSelection(string $entity_type_id, string|null $handler = null, array $settings = [], \Drupal\Core\Entity\EntityInterface|null $entity = null): \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface

Parameters:

Parameter Type Description
$entity_type_id string
The entity type ID.
$handler string|null
The selection handler plugin ID.
$settings array
The selection handler plugin settings.
$entity \Drupal\Core\Entity\EntityInterface|null
The current entity context.

Return Value:

The entity reference selection handler.


getEntityReferenceFieldSelection

Get an entity reference selection handler for an entity reference field.

public getEntityReferenceFieldSelection(\Drupal\Core\Field\FieldDefinitionInterface $fieldDefinition, \Drupal\Core\Entity\EntityInterface|null $entity = null): \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface

Parameters:

Parameter Type Description
$fieldDefinition \Drupal\Core\Field\FieldDefinitionInterface
The entity reference field definition.
$entity \Drupal\Core\Entity\EntityInterface|null
The current entity context.

Return Value:

The entity reference selection handler.

Throws:

If the field is not an entity reference field. - InvalidArgumentException


getBundlesReferencingBundle

Returns the bundles that have a field able to reference a target bundle.

public getBundlesReferencingBundle(string $entity_type_id, string $target_type, string $target_bundle): string[]

For example, the node types with a field that can reference terms in the "tags" vocabulary. Fields of any type built on core's entity reference item are checked, such as entity_reference, entity_reference_revisions or file, and base fields count too. A field whose selection handler has no target bundles, so it can reference any bundle of the target entity type, counts.

Parameters:

Parameter Type Description
$entity_type_id string
The entity type ID of the bundles, e.g. 'node'.
$target_type string
The referenced entity type ID, e.g. 'taxonomy_term'.
$target_bundle string
The referenced bundle, e.g. 'tags'.

Return Value:

The bundles, in the order of the field map.


accessBundles

Attempt to determine if an entity type can be accessed via any bundles.

public accessBundles(\Drupal\Core\Entity\EntityTypeInterface $entityType, (int|string)[]|null $bundles = null, string $operation = 'view', \Drupal\Core\Session\AccountInterface|null $account = null, bool $returnAsObject = false): \Drupal\Core\Access\AccessResultInterface|bool

Access is allowed if any of the bundles can be accessed, even if access to another bundle is forbidden.

Parameters:

Parameter Type Description
$entityType \Drupal\Core\Entity\EntityTypeInterface
The entity type.
$bundles (int|string)[]|null
(optional) A list of the bundle IDs to check access for. Defaults to all
bundles of the entity type. Integers are accepted for bundle IDs made
only of digits, such as the keys of getBundleOptions().
$operation string
(optional) The operation to be performed. Defaults to view.
$account \Drupal\Core\Session\AccountInterface|null
(optional) The user for which to check access, or NULL to check access
for the current user. Defaults to NULL.
$returnAsObject bool
(optional) Defaults to FALSE.

Return Value:

TRUE if any of the bundles can be accessed, otherwise FALSE. When $returnAsObject is TRUE, the access result of the first bundle that can be accessed, with the cacheability of the bundles checked before it, or the combined access result of all the bundles if none can be accessed.


accessBundle

Attempt to determine if an entity bundle can be accessed.

public accessBundle(\Drupal\Core\Entity\EntityTypeInterface $entityType, string $bundle, string $operation = 'view', \Drupal\Core\Session\AccountInterface|null $account = null, bool $returnAsObject = false, array $values = []): \Drupal\Core\Access\AccessResultInterface|bool

This creates a stub entity and does not operate on a real entity. Unless set in $values, the stub entity is published and is owned by the user for which access is checked.

Results are cached in memory for the request, varied by their cache contexts, unless the stub values aren't scalars.

Parameters:

Parameter Type Description
$entityType \Drupal\Core\Entity\EntityTypeInterface
The entity type.
$bundle string
The entity bundle.
$operation string
The operation to be performed.
$account \Drupal\Core\Session\AccountInterface|null
(optional) The user for which to check access, or NULL to check access
for the current user. Defaults to NULL.
$returnAsObject bool
(optional) Defaults to FALSE.
$values array
The entity values to use when creating a pseudo-entity.

Return Value:

TRUE if the entity bundle can be accessed, otherwise FALSE.


createBundleStub

Creates a stub entity of a bundle for access checks.

protected createBundleStub(\Drupal\Core\Entity\EntityTypeInterface $entityType, string $bundle, \Drupal\Core\Session\AccountInterface|null $account, array $values): \Drupal\Core\Entity\EntityInterface

Parameters:

Parameter Type Description
$entityType \Drupal\Core\Entity\EntityTypeInterface
The entity type.
$bundle string
The entity bundle.
$account \Drupal\Core\Session\AccountInterface|null
The user access is checked for, or NULL for the current user.
$values array
The entity values to use.

Return Value:

The stub entity: published and owned by the user access is checked for, unless set in $values.


loadBundle

Load the bundle entity type.

public loadBundle(\Drupal\Core\Entity\EntityTypeInterface $entityType, string $bundle): \Drupal\Core\Entity\EntityInterface|null

Parameters:

Parameter Type Description
$entityType \Drupal\Core\Entity\EntityTypeInterface
The entity type.
$bundle string
The entity type bundle.

Return Value:

The bundle entity if available.


getQuery

Get an entity query from a simple parameter value.

public getQuery(string|\Drupal\Core\Entity\Query\QueryInterface $entityTypeIdOrQuery): \Drupal\Core\Entity\Query\QueryInterface

This does not call the required accessCheck() method on the generated query object, you are responsible for doing it yourself.

Parameters:

Parameter Type Description
$entityTypeIdOrQuery string|\Drupal\Core\Entity\Query\QueryInterface
The content entity type ID. For example, 'node' or 'taxonomy_term'.
The calling code should ensure that the entity type exists beforehand
(i.e., by checking that the entity type is defined or that the module
that provides it is installed).
This can also be an entity query.
This can also be a string in the format of 'node:bundle' to only load
a specific bundle of entities. Multiple bundles can be used by using
the 'node:bundle1:bundle2' string or passing an entity query object.

Return Value:

The entity query object.

Throws:

Thrown when the provided entity type ID is not a content entity type. - InvalidArgumentException


isFormFieldVisible

Checks whether a field is shown in a form mode of any of the bundles.

public isFormFieldVisible(string $entityTypeId, string $fieldName, string|string[]|null $bundles = null, string $formMode = \Drupal\Core\Entity\EntityDisplayRepositoryInterface::DEFAULT_DISPLAY_MODE): bool

A form display that isn't saved, such as the default form mode of a new bundle, is built from the fields' default display options, as on the entity form itself.

Parameters:

Parameter Type Description
$entityTypeId string
The entity type ID.
$fieldName string
The field name.
$bundles string|string[]|null
A bundle or list of bundles, or NULL for all of the entity type's
bundles.
$formMode string
The form mode.

Return Value:

TRUE if the field is in the form display of at least one bundle, or FALSE if it's hidden in all of them, or no bundles are given.


isFieldVisible

Checks whether a field is shown in a view mode of any of the bundles.

public isFieldVisible(string $entityTypeId, string $fieldName, string|string[]|null $bundles = null, string $viewMode = \Drupal\Core\Entity\EntityDisplayRepositoryInterface::DEFAULT_DISPLAY_MODE): bool

A view display that isn't saved is built from the fields' default display options, as when the entity is viewed.

Parameters:

Parameter Type Description
$entityTypeId string
The entity type ID.
$fieldName string
The field name.
$bundles string|string[]|null
A bundle or list of bundles, or NULL for all of the entity type's
bundles.
$viewMode string
The view mode.

Return Value:

TRUE if the field is in the view display of at least one bundle, or FALSE if it's hidden in all of them, or no bundles are given.


isDisplayComponentVisible

Checks whether a component is shown in a display of any of the bundles.

protected isDisplayComponentVisible(string $context, string $entityTypeId, string $name, string|string[]|null $bundles, string $mode): bool

The component names of each display are cached in memory, tagged with the display's and the field definitions' cache tags, so sites with many bundles don't build every display again on each call.

Parameters:

Parameter Type Description
$context string
The display context: "form" or "view".
$entityTypeId string
The entity type ID.
$name string
The component name.
$bundles string|string[]|null
A bundle or list of bundles, or NULL for all of the entity type's
bundles.
$mode string
The form or view mode.

Return Value:

TRUE if the component is in the display of at least one bundle.