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?

4 Answers 4


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()->getViewBuilder($entity_type);
$storage = \Drupal::entityTypeManager()->getStorage($entity_type);
$node = $storage->load($nid);
$build = $view_builder->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()->getStorage($entity_type)->load($nid);
$output = render(\Drupal::entityTypeManager()->getViewBuilder($entity_type)->view($node, $view_mode));
  • Couldn't you just use $node = Node::load($nid) ?
    – Nic
    Jun 12, 2016 at 16:50
  • 7
    Why use render() in the last line? getViewBuilder($entity_type)->view returns a render array that will be passed to the twig view.
    – Tim
    Sep 15, 2016 at 0:28
  • 1
    @Tim I assume that depends on the context you are using this within, this is a wiki, so you are welcome to add your input. For some of my use cases I did not have the luxury of passing this directly to a twig template as a render array, rather I needed to perform the render myself. Best of luck. Sep 16, 2016 at 16:11
  • If performing the render manually, for some cases, you may also like to consider the renderer service renderRoot or renderPlain, or via drupal_render_root($build); Mar 1, 2019 at 23:42
  • +1 Confirming this answer works on Drupal 9, 9.3.7. And useful to know that In this answer $output is a Markup object, which has function/method __toString() which returns the html markup as a string. Why render? some ask. Answer: In my case I am displaying node markupcontent using a standard endpoint url/controller function, called by javascript on the page. I want to update that part of the page, "in place". The javascript needs the node content in finished rendered html string therefore. So with that example, there are some cases where rendering is needed. Apr 13 at 14:25

The most important part is to stop rendering yourself. You can return render arrays almost everywhere and you should do it. Combining strings like that together doesn't work anymore.

All you need is this:

$nodes = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple($nids);
// Or a use the static loadMultiple method on the entity class:
$nodes = \Drupal\node\Entity\Node::loadMultiple($nids);

// And then you can view/build them all together:
$build = \Drupal::entityTypeManager()->getViewBuilder('node')->viewMultiple($nodes, 'teaser');

entity_view() will be removed from Drupal 9.0.0. The following code works also in Drupal 9.

$render_controller = \Drupal::entityTypeManager()->getViewBuilder($entity->getEntityTypeId());
$render_output = $render_controller->view($entity, $view_mode, $langcode);

Essentially, that is the code used from entity_view(), after I removed the part handling the entity render cache, and replaced a deprecated method that function uses.


This did work for me (perfect for PHPUnit tests with Drupal test traits - https://gitlab.com/weitzman/drupal-test-traits):

$nid = 3139;
$entity_type = 'node';
$view_mode = 'bs_full_guest';

$view_builder = \Drupal::entityTypeManager()->getViewBuilder($entity_type);
$storage = \Drupal::entityTypeManager()->getStorage($entity_type);
$node = $storage->load($nid);
$build = $view_builder->view($node, $view_mode);
$output = \Drupal::service('renderer')->renderRoot($build);
$nodeHtml = $output->__toString();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.