Returns the entity_display object used to render an entity.
Depending on the configuration of the view mode for the bundle, this can be either the display object associated to the view mode, or the 'default' display.
This function should only be used internally when rendering an entity. When assigning suggested display options for a component in a given view mode, entity_get_display() should be used instead, in order to avoid inadvertently modifying the output of other view modes that might happen to use the 'default' display too. Those options will then be effectively applied only if the view mode is configured to use them.
\Drupal\Core\Entity\EntityInterface $entity: The entity being rendered.
string $view_mode: The view mode being rendered.
\Drupal\entity\Plugin\Core\Entity\EntityDisplay The display object that should be used to render the entity.
function entity_get_render_display(EntityInterface $entity, $view_mode) {
$entity_type = $entity
->entityType();
$bundle = $entity
->bundle();
// Determine the display to use for rendering this entity. Depending on the
// configuration of the view mode for this bundle, this will be either the
// display associated to the view mode, or the 'default' display.
$view_mode_settings = field_view_mode_settings($entity_type, $bundle);
$render_view_mode = !empty($view_mode_settings[$view_mode]['status']) ? $view_mode : 'default';
$display = entity_get_display($entity_type, $bundle, $render_view_mode);
$display->originalMode = $view_mode;
return $display;
}