class EntityFieldManager implements EntityFieldManagerInterface (View source)

Manages the discovery of entity fields.

This includes field definitions, base field definitions, and field storage definitions.

Traits

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

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

Properties

protected CacheBackendInterface $cacheBackend

Cache backend instance.

from  UseCacheBackendTrait
protected bool $useCaches

Flag whether caches should be used or skipped.

from  UseCacheBackendTrait
protected TranslationInterface $stringTranslation

The string translation service.

from  StringTranslationTrait
protected array $extraFields

Extra fields by bundle.

protected array $baseFieldDefinitions

Static cache of base field definitions.

protected array $fieldDefinitions

Static cache of field definitions per bundle and entity type.

protected array $fieldStorageDefinitions

Static cache of field storage definitions per entity type.

protected array $activeFieldStorageDefinitions

Static cache of active field storage definitions per entity type.

protected array $fieldMap

An array keyed by entity type. Each value is an array whose keys are field names and whose value is an array with two entries:

  • type: The field type.
protected array $fieldMapByFieldType

An array keyed by field type. Each value is an array whose key are entity types including arrays in the same form that $fieldMap.

protected TypedDataManagerInterface $typedDataManager

The typed data manager.

protected LanguageManagerInterface $languageManager

The language manager.

protected KeyValueFactoryInterface $keyValueFactory

The key-value factory.

protected ModuleHandlerInterface $moduleHandler

The module handler.

protected EntityTypeManagerInterface $entityTypeManager

The entity type manager.

protected EntityTypeBundleInfoInterface $entityTypeBundleInfo

The entity type bundle info.

protected EntityDisplayRepositoryInterface $entityDisplayRepository

The entity display repository.

Methods

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.

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.

__construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityDisplayRepositoryInterface $entity_display_repository, TypedDataManagerInterface $typed_data_manager, LanguageManagerInterface $language_manager, KeyValueFactoryInterface $key_value_factory, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend)

Constructs a new EntityFieldManager.

getBaseFieldDefinitions(string $entity_type_id)

Gets the base field definitions for a content entity type.

buildBaseFieldDefinitions(string $entity_type_id)

Builds base field definitions for an entity type.

getFieldDefinitions(string $entity_type_id, string $bundle)

Gets the field definitions for a specific bundle.

buildBundleFieldDefinitions(string $entity_type_id, string $bundle, array $base_field_definitions)

Builds field definitions for a specific bundle within an entity type.

getFieldStorageDefinitions(string $entity_type_id)

Gets the field storage definitions for a content entity type.

getActiveFieldStorageDefinitions(string $entity_type_id)

Gets the active field storage definitions for a content entity type.

$this
setFieldMap(array $field_map)

Sets a lightweight map of fields across bundles.

array
getFieldMap()

Gets a lightweight map of fields across bundles.

array
getFieldMapByFieldType(string $field_type)

Gets a lightweight map of fields across bundles filtered by field type.

buildFieldStorageDefinitions(string $entity_type_id)

Builds field storage definitions for an entity type.

clearCachedFieldDefinitions()

Clears static and persistent field definition caches.

useCaches(bool $use_caches = FALSE)

Disable the use of caches.

array
getExtraFields(string $entity_type_id, string $bundle)

Gets the "extra fields" for a bundle.

Details

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

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

__construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityDisplayRepositoryInterface $entity_display_repository, TypedDataManagerInterface $typed_data_manager, LanguageManagerInterface $language_manager, KeyValueFactoryInterface $key_value_factory, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend)

Constructs a new EntityFieldManager.

Parameters

EntityTypeManagerInterface $entity_type_manager

The entity type manager.

EntityTypeBundleInfoInterface $entity_type_bundle_info

The entity type bundle info.

EntityDisplayRepositoryInterface $entity_display_repository

The entity display repository.

TypedDataManagerInterface $typed_data_manager

The typed data manager.

LanguageManagerInterface $language_manager

The language manager.

KeyValueFactoryInterface $key_value_factory

The key-value factory.

ModuleHandlerInterface $module_handler

The module handler.

CacheBackendInterface $cache_backend

The cache backend.

FieldDefinitionInterface[] getBaseFieldDefinitions(string $entity_type_id)

Gets the base field definitions for a content entity type.

Only fields that are not specific to a given bundle or set of bundles are returned. This excludes configurable fields, as they are always attached to a specific bundle.

Parameters

string $entity_type_id

The entity type ID. Only entity types that implement \Drupal\Core\Entity\FieldableEntityInterface are supported.

Return Value

FieldDefinitionInterface[]

The array of base field definitions for the entity type, keyed by field name.

Exceptions

LogicException

protected FieldDefinitionInterface[] buildBaseFieldDefinitions(string $entity_type_id)

Builds base field definitions for an entity type.

Parameters

string $entity_type_id

The entity type ID. Only entity types that implement \Drupal\Core\Entity\FieldableEntityInterface are supported.

Return Value

FieldDefinitionInterface[]

An array of field definitions, keyed by field name.

Exceptions

LogicException

FieldDefinitionInterface[] getFieldDefinitions(string $entity_type_id, string $bundle)

Gets the field definitions for a specific bundle.

Parameters

string $entity_type_id

The entity type ID. Only entity types that implement \Drupal\Core\Entity\FieldableEntityInterface are supported.

string $bundle

The bundle.

Return Value

FieldDefinitionInterface[]

The array of field definitions for the bundle, keyed by field name.

protected FieldDefinitionInterface[] buildBundleFieldDefinitions(string $entity_type_id, string $bundle, array $base_field_definitions)

Builds field definitions for a specific bundle within an entity type.

Parameters

string $entity_type_id

The entity type ID. Only entity types that implement \Drupal\Core\Entity\FieldableEntityInterface are supported.

string $bundle

The bundle.

array $base_field_definitions

The list of base field definitions.

Return Value

FieldDefinitionInterface[]

An array of bundle field definitions, keyed by field name. Does not include base fields.

FieldStorageDefinitionInterface[] getFieldStorageDefinitions(string $entity_type_id)

Gets the field storage definitions for a content entity type.

This returns all field storage definitions for base fields and bundle fields of an entity type. Note that field storage definitions of a base field equal the full base field definition (i.e. they implement FieldDefinitionInterface), while the storage definitions for bundle fields may implement FieldStorageDefinitionInterface only.

Parameters

string $entity_type_id

The entity type ID. Only content entities are supported.

Return Value

FieldStorageDefinitionInterface[]

The array of field storage definitions for the entity type, keyed by field name.

FieldStorageDefinitionInterface[] getActiveFieldStorageDefinitions(string $entity_type_id)

internal  
 

Gets the active field storage definitions for a content entity type.

Parameters

string $entity_type_id

The entity type ID. Only content entities are supported.

Return Value

FieldStorageDefinitionInterface[]

An array of field storage definitions that are active in the current request, keyed by field name.

$this setFieldMap(array $field_map)

Sets a lightweight map of fields across bundles.

Parameters

array $field_map

See the return value of self::getFieldMap().

Return Value

$this

array getFieldMap()

Gets a lightweight map of fields across bundles.

Return Value

array

An array keyed by entity type. Each value is an array which keys are field names and value is an array with two entries:

  • type: The field type.
  • bundles: An associative array of the bundles in which the field appears, where the keys and values are both the bundle's machine name.

array getFieldMapByFieldType(string $field_type)

Gets a lightweight map of fields across bundles filtered by field type.

Parameters

string $field_type

The field type to filter by.

Return Value

array

An array keyed by entity type. Each value is an array which keys are field names and value is an array with two entries:

  • type: The field type.
  • bundles: An associative array of the bundles in which the field appears, where the keys and values are both the bundle's machine name.

protected FieldStorageDefinitionInterface[] buildFieldStorageDefinitions(string $entity_type_id)

Builds field storage definitions for an entity type.

Parameters

string $entity_type_id

The entity type ID. Only entity types that implement \Drupal\Core\Entity\FieldableEntityInterface are supported

Return Value

FieldStorageDefinitionInterface[]

An array of field storage definitions, keyed by field name.

clearCachedFieldDefinitions()

Clears static and persistent field definition caches.

useCaches(bool $use_caches = FALSE)

Disable the use of caches.

Parameters

bool $use_caches

FALSE to not use any caches.

array getExtraFields(string $entity_type_id, string $bundle)

Gets the "extra fields" for a bundle.

Parameters

string $entity_type_id

The entity type ID.

string $bundle

The bundle name.

Return Value

array

A nested array of 'pseudo-field' elements. Each list is nested within the following keys: entity type, bundle name, context (either 'form' or 'display'). The keys are the name of the elements as appearing in the renderable array (either the entity form or the displayed entity). The value is an associative array:

  • label: The human readable name of the element. Make sure you sanitize this appropriately.
  • description: A short description of the element contents.
  • weight: The default weight of the element.
  • visible: (optional) The default visibility of the element. Defaults to TRUE.
  • edit: (optional) String containing markup (normally a link) used as the element's 'edit' operation in the administration interface. Only for 'form' context.
  • delete: (optional) String containing markup (normally a link) used as the element's 'delete' operation in the administration interface. Only for 'form' context.