There are use cases where you want the rendered output programmatically. This is different in drupal 9. This snippet shows you how to get it.

// Make sure you have loaded the $entity object
$view_mode = 'full';
$langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
$render_controller = \Drupal::entityTypeManager()->getViewBuilder($entity->getEntityTypeId());
$render_output = $render_controller->view($entity, $view_mode, $langcode);

 


Saved you some valuable time?

Donate and get more free time-saving code on this website in the future.