How can I programmatically render a node's field respecting the view mode settings? I want to render a node's field inside a block. It works like this:

&lt;?php

if ($node) {
  if (isset($node-&gt;field_body_secondary) &amp;&amp; $field = $node-&gt;field_body_secondary-&gt;value) {
    $markup = render($field);
    $build = array(
      '#type' =&gt; 'markup',
      '#markup' =&gt; $markup,
    );
  }
}

return $build;


But this is not 100% as if I would just render the field normally, making it visible in the view mode settings.
To render a single field with the display setting of a view mode you can use the view() method of the field:

Example for rendering the image in teaser format:

$build['image'] = $node-&gt;field_image-&gt;view('teaser');


Or the body in full:

$build['body'] = $node-&gt;body-&gt;view('full');