class EntityDisplayRepository implements EntityDisplayRepositoryInterface (View source)

Provides a repository for entity display objects (view modes and form modes).

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 $displayModeInfo

Static cache of display modes information.

protected LanguageManagerInterface $languageManager

The language manager.

protected EntityTypeManagerInterface $entityTypeManager

The entity type manager.

protected ModuleHandlerInterface $moduleHandler

The module handler.

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, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend, LanguageManagerInterface $language_manager)

Constructs a new EntityDisplayRepository.

array
getAllViewModes()

Gets the entity view mode info for all entity types.

array
getViewModes(string $entity_type_id)

Gets the entity view mode info for a specific entity type.

array
getAllFormModes()

Gets the entity form mode info for all entity types.

array
getFormModes(string $entity_type_id)

Gets the entity form mode info for a specific entity type.

array
getAllDisplayModesByEntityType(string $display_type)

Gets the entity display mode info for all entity types.

array
getDisplayModesByEntityType(string $display_type, string $entity_type_id)

Gets the entity display mode info for a specific entity type.

array
getViewModeOptions($entity_type)

Gets an array of view mode options.

array
getFormModeOptions(string $entity_type_id)

Gets an array of form mode options.

array
getViewModeOptionsByBundle(string $entity_type_id, string $bundle)

Returns an array of enabled view mode options by bundle.

array
getFormModeOptionsByBundle(string $entity_type_id, string $bundle)

Returns an array of enabled form mode options by bundle.

array
getDisplayModeOptions(string $display_type, string $entity_type_id)

Gets an array of display mode options.

array
getDisplayModeOptionsByBundle($display_type, string $entity_type_id, string $bundle)

Returns an array of enabled display mode options by bundle.

$this
clearDisplayModeInfo()

Clears the gathered display mode info.

getViewDisplay(string $entity_type, string $bundle, string $view_mode = self::DEFAULT_DISPLAY_MODE)

Returns the entity view display associated with a bundle and view mode.

getFormDisplay(string $entity_type, string $bundle, string $form_mode = self::DEFAULT_DISPLAY_MODE)

Returns the entity form display associated with a bundle and form mode.

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, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend, LanguageManagerInterface $language_manager)

Constructs a new EntityDisplayRepository.

Parameters

EntityTypeManagerInterface $entity_type_manager

The entity type manager.

ModuleHandlerInterface $module_handler

The module handler.

CacheBackendInterface $cache_backend

The cache backend.

LanguageManagerInterface $language_manager

The language manager.

array getAllViewModes()

Gets the entity view mode info for all entity types.

Return Value

array

The view mode info for all entity types.

array getViewModes(string $entity_type_id)

Gets the entity view mode info for a specific entity type.

Parameters

string $entity_type_id

The entity type whose view mode info should be returned.

Return Value

array

The view mode info for a specific entity type.

array getAllFormModes()

Gets the entity form mode info for all entity types.

Return Value

array

The form mode info for all entity types.

array getFormModes(string $entity_type_id)

Gets the entity form mode info for a specific entity type.

Parameters

string $entity_type_id

The entity type whose form mode info should be returned.

Return Value

array

The form mode info for a specific entity type.

protected array getAllDisplayModesByEntityType(string $display_type)

Gets the entity display mode info for all entity types.

Parameters

string $display_type

The display type to be retrieved. It can be "view_mode" or "form_mode".

Return Value

array

The display mode info for all entity types.

protected array getDisplayModesByEntityType(string $display_type, string $entity_type_id)

Gets the entity display mode info for a specific entity type.

Parameters

string $display_type

The display type to be retrieved. It can be "view_mode" or "form_mode".

string $entity_type_id

The entity type whose display mode info should be returned.

Return Value

array

The display mode info for a specific entity type.

array getViewModeOptions($entity_type)

Gets an array of view mode options.

Parameters

$entity_type

Return Value

array

An array of view mode labels, keyed by the display mode ID.

array getFormModeOptions(string $entity_type_id)

Gets an array of form mode options.

Parameters

string $entity_type_id

The entity type whose form mode options should be returned.

Return Value

array

An array of form mode labels, keyed by the display mode ID.

array getViewModeOptionsByBundle(string $entity_type_id, string $bundle)

Returns an array of enabled view mode options by bundle.

Parameters

string $entity_type_id

The entity type whose view mode options should be returned.

string $bundle

The name of the bundle.

Return Value

array

An array of view mode labels, keyed by the display mode ID.

array getFormModeOptionsByBundle(string $entity_type_id, string $bundle)

Returns an array of enabled form mode options by bundle.

Parameters

string $entity_type_id

The entity type whose form mode options should be returned.

string $bundle

The name of the bundle.

Return Value

array

An array of form mode labels, keyed by the display mode ID.

protected array getDisplayModeOptions(string $display_type, string $entity_type_id)

Gets an array of display mode options.

Parameters

string $display_type

The display type to be retrieved. It can be "view_mode" or "form_mode".

string $entity_type_id

The entity type whose display mode options should be returned.

Return Value

array

An array of display mode labels, keyed by the display mode ID.

protected array getDisplayModeOptionsByBundle($display_type, string $entity_type_id, string $bundle)

Returns an array of enabled display mode options by bundle.

Parameters

$display_type

The display type to be retrieved. It can be "view_mode" or "form_mode".

string $entity_type_id

The entity type whose display mode options should be returned.

string $bundle

The name of the bundle.

Return Value

array

An array of display mode labels, keyed by the display mode ID.

$this clearDisplayModeInfo()

Clears the gathered display mode info.

Return Value

$this

EntityViewDisplayInterface getViewDisplay(string $entity_type, string $bundle, string $view_mode = self::DEFAULT_DISPLAY_MODE)

Returns the entity view display associated with a bundle and view mode.

Use this function when assigning suggested display options for a component in a given view mode. Note that they will only be actually used at render time if the view mode itself is configured to use dedicated display settings for the bundle; if not, the 'default' display is used instead.

The function reads the entity view display from the current configuration, or returns a ready-to-use empty one if configuration entry exists yet for this bundle and view mode. This streamlines manipulation of display objects by always returning a consistent object that reflects the current state of the configuration.

Example usage:

  • Set the 'body' field to be displayed and the 'field_image' field to be hidden on article nodes in the 'default' display.

Parameters

string $entity_type

The entity type.

string $bundle

The bundle.

string $view_mode

(optional) The view mode. Defaults to self::DEFAULT_DISPLAY_MODE.

Return Value

EntityViewDisplayInterface

The entity view display associated with the view mode.

EntityFormDisplayInterface getFormDisplay(string $entity_type, string $bundle, string $form_mode = self::DEFAULT_DISPLAY_MODE)

Returns the entity form display associated with a bundle and form mode.

The function reads the entity form display object from the current configuration, or returns a ready-to-use empty one if no configuration entry exists yet for this bundle and form mode. This streamlines manipulation of entity form displays by always returning a consistent object that reflects the current state of the configuration.

Example usage:

  • Set the 'body' field to be displayed with the 'text_textarea_with_summary' widget and the 'field_image' field to be hidden on article nodes in the 'default' form mode.

Parameters

string $entity_type

The entity type.

string $bundle

The bundle.

string $form_mode

(optional) The form mode. Defaults to self::DEFAULT_DISPLAY_MODE.

Return Value

EntityFormDisplayInterface

The entity form display associated with the given form mode.