How do I render nodes, now that node_view() has been deprecated? In Drupal 7 I frequently use node_view() for rendering nodes (using view modes) in blocks or pages as follows:

$nids = array(123,456,789);
$nodes = node_load_multiple($nids);
foreach ($nodes as $node) {
  $node_view = node_view($node, 'teaser');
  $output .= drupal_render($node_view);
}
$build['nodes']['#markup'] = $output;
return $build;


node_view()/entity_view() has been deprecated and replaced by a view builder as explained in change record Entities are now rendered by a view builder . The information is not detailed enough for me to be able to figure out how to achieve the same result.

How can you render nodes in Drupal 8 so that the output can be used in the render array of a block or page?
The answer given by Berdir uses entityManager, which has been deprecated in favor of more specific services. This is the code I currently use.

$nid = 1;
$entity_type = 'node';
$view_mode = 'teaser';

$view_builder = \Drupal::entityTypeManager()-&gt;getViewBuilder($entity_type);
$storage = \Drupal::entityTypeManager()-&gt;getStorage($entity_type);
$node = $storage-&gt;load($nid);
$build = $view_builder-&gt;view($node, $view_mode);
$output = render($build);


This code is a little more verbose than some might like; you could reduce it down to a couple lines if you really want more brevity by chaining together some methods.

$nid = 1;
$entity_type = 'node';
$view_mode = 'teaser';

$node = \Drupal::entityTypeManager()-&gt;getStorage($entity_type)-&gt;load($nid);
$output = render(\Drupal::entityTypeManager()-&gt;getViewBuilder($entity_type)-&gt;view($node, $view_mode));