interface EntityViewBuilderInterface (View source)

Defines an interface for entity view builders.

Methods

buildComponents(array $build, array $entities, array $displays, string $view_mode)

Builds the component fields and properties of a set of entities.

array
view(EntityInterface $entity, string $view_mode = 'full', string $langcode = NULL)

Builds the render array for the provided entity.

A
viewMultiple(array $entities = [], string $view_mode = 'full', string $langcode = NULL)

Builds the render array for the provided entities.

resetCache(array $entities = NULL)

Resets the entity render cache.

array
viewField(FieldItemListInterface $items, string|array $display_options = [])

Builds a renderable array for the value of a single field in an entity.

array
viewFieldItem(FieldItemInterface $item, string|array $display_options = [])

Builds a renderable array for a single field item.

array
getCacheTags()

The cache tag associated with this entity view builder.

Details

buildComponents(array $build, array $entities, array $displays, string $view_mode)

Builds the component fields and properties of a set of entities.

Parameters

array $build

The renderable array representing the entity content.

array $entities

The entities whose content is being built.

array $displays

The array of entity view displays holding the display options configured for the entity components, keyed by bundle name.

string $view_mode

The view mode in which the entity is being viewed.

array view(EntityInterface $entity, string $view_mode = 'full', string $langcode = NULL)

Builds the render array for the provided entity.

Parameters

EntityInterface $entity

The entity to render.

string $view_mode

(optional) The view mode that should be used to render the entity.

string $langcode

(optional) For which language the entity should be rendered, defaults to the current content language.

Return Value

array

A render array for the entity.

Exceptions

InvalidArgumentException

A viewMultiple(array $entities = [], string $view_mode = 'full', string $langcode = NULL)

Builds the render array for the provided entities.

Parameters

array $entities

An array of entities implementing EntityInterface to view.

string $view_mode

(optional) The view mode that should be used to render the entity.

string $langcode

(optional) For which language the entity should be rendered, defaults to the current content language.

Return Value

A

render array for the entities, indexed by the same keys as the entities array passed in $entities.

Exceptions

InvalidArgumentException

resetCache(array $entities = NULL)

Resets the entity render cache.

Parameters

array $entities

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

array viewField(FieldItemListInterface $items, string|array $display_options = [])

Builds a renderable array for the value of a single field in an entity.

The resulting output is a fully themed field with label and multiple values.

This function can be used by third-party modules that need to output an isolated field.

  • Do not use inside node (or any other entity) templates; use render($content[FIELD_NAME]) instead.
  • The FieldItemInterface::view() method can be used to output a single formatted field value, without label or wrapping field markup.

The function takes care of invoking the prepare_view steps. It also respects field access permissions.

Parameters

FieldItemListInterface $items

FieldItemList containing the values to be displayed.

string|array $display_options

Can be either:

  • The name of a view mode. The field will be displayed according to the display settings specified for this view mode in the $field definition for the field in the entity's bundle. If no display settings are found for the view mode, the settings for the 'default' view mode will be used.
  • An array of display options. The following key/value pairs are allowed:
    • label: (string) Position of the label. The default 'field' theme implementation supports the values 'inline', 'above' and 'hidden'. Defaults to 'above'.
    • type: (string) The formatter to use. Defaults to the 'default_formatter' for the field type. The default formatter will also be used if the requested formatter is not available.
    • settings: (array) Settings specific to the formatter. Defaults to the formatter's default settings.
    • weight: (float) The weight to assign to the renderable element. Defaults to 0.

Return Value

array

A renderable array for the field values.

See also

EntityViewBuilderInterface::viewFieldItem

array viewFieldItem(FieldItemInterface $item, string|array $display_options = [])

Builds a renderable array for a single field item.

Parameters

FieldItemInterface $item

FieldItem to be displayed.

string|array $display_options

Can be either the name of a view mode, or an array of display settings. See EntityViewBuilderInterface::viewField() for more information.

Return Value

array

A renderable array for the field item.

See also

EntityViewBuilderInterface::viewField

array getCacheTags()

The cache tag associated with this entity view builder.

An entity view builder is instantiated on a per-entity type basis, so the cache tags are also per-entity type.

Return Value

array

An array of cache tags.