public function viewFieldItem(FieldItemInterface
$item,
$display =
[]) { $entity =
$item->
getEntity();
$field_name =
$item->
getFieldDefinition()->
getName();
// Clone the entity since we are going to modify field values.
$clone =
clone $entity;
// Push the item as the single value for the field, and defer to viewField()
// to build the render array for the whole list.
$clone->
{$field_name}->
setValue([$item->
getValue()]);
$elements =
$this->
viewField($clone->
{$field_name},
$display);
// Extract the part of the render array we need.
$output =
$elements[0
] ??
[];
if (isset($elements['#access'
])) { $output['#access'
] =
$elements['#access'
];
} return $output;
} /**
* Gets an EntityViewDisplay for rendering an individual field.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity.
* @param string $field_name
* The field name.
* @param string|array $display_options
* The display options passed to the viewField() method.
*
* @return \Drupal\Core\Entity\Display\EntityViewDisplayInterface
*/