22

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?

31

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 '16 at 16:50
  • 3
    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 '16 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. – nicholas.alipaz Sep 16 '16 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); – David Thomas Mar 1 at 23:42
15

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::entityManager()->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');
  • I have not been able to get this working. Is there an example in Drupal core that one can follow? (preferably a block) – batigolix Nov 18 '15 at 21:56
  • 2
    EntityManager is deprecated. Use EntityTypeManager instead. – Tim Mar 14 '17 at 15:48
4

entity_view() will be removed before Drupal 9.0.0. You could use it in Drupal 8, but if you want to write since now code that (for that part) will not change for Drupal 9, you can use the following code instead of entity_view().

$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 replaced the reference to another deprecated method that function was using. In fact, the documentation for Drupal::entityManager() says:

In Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager() instead in most cases. If the needed method is not on \Drupal\Core\Entity\EntityTypeManagerInterface, see the deprecated \Drupal\Core\Entity\EntityManager to find the correct interface or service.

If entity_view() will be changed before being deprecated, you can still visit its documentation page to see the actual (and updated) code the function uses.

  • Should this be Drupal::entityTypeManager() now that Drupal::entityManager() is deprecated? – Nic Jun 12 '16 at 16:49
  • Yes, that is correct. – kiamlaluno Jun 12 '16 at 17:11

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.