public function EntityViewBuilder::viewField in Drupal 10
Same name and namespace in other branches
- 8 core/lib/Drupal/Core/Entity/EntityViewBuilder.php \Drupal\Core\Entity\EntityViewBuilder::viewField()
- 9 core/lib/Drupal/Core/Entity/EntityViewBuilder.php \Drupal\Core\Entity\EntityViewBuilder::viewField()
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
\Drupal\Core\Field\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.
Overrides EntityViewBuilderInterface::viewField
See also
\Drupal\Core\Entity\EntityViewBuilderInterface::viewFieldItem()
1 call to EntityViewBuilder::viewField()
- EntityViewBuilder::viewFieldItem in core/
lib/ Drupal/ Core/ Entity/ EntityViewBuilder.php - Builds a renderable array for a single field item.
File
- core/
lib/ Drupal/ Core/ Entity/ EntityViewBuilder.php, line 446
Class
- EntityViewBuilder
- Base class for entity view builders.
Namespace
Drupal\Core\EntityCode
public function viewField(FieldItemListInterface $items, $display_options = []) {
/** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */
$entity = $items
->getEntity();
// If the field is not translatable and the entity is, then the field item
// list always points to the default translation of the entity. Attempt to
// fetch it in the current content language.
if (!$items
->getFieldDefinition()
->isTranslatable() && $entity
->isTranslatable()) {
$entity = $this->entityRepository
->getTranslationFromContext($entity);
}
$field_name = $items
->getFieldDefinition()
->getName();
$display = $this
->getSingleFieldDisplay($entity, $field_name, $display_options);
$output = [];
$build = $display
->build($entity);
if (isset($build[$field_name])) {
$output = $build[$field_name];
}
return $output;
}