D8: render block in node? We're migrating some code to D8 and in some of our nodes' body field we used to call PHP code directly with the now defunct module_invoke to render a block in the node before the content. 

All the examples I can find explain how to do that in D8 with themes or templates, but I need to render the block inside the node body using PHP code, is there a way to do that?

We already tried:

\Drupal::moduleHandler()-&gt;invoke($module, $hook, $args = array())


but it doesn't show the block, presumably because it only takes modules?

From this answer we also tried:

$block = \Drupal\block_content\Entity\BlockContent::load($bid); 
$render = \Drupal::entityTypeManager()-&gt;getViewBuilder('block_content')-&gt;view($block);


Trying to print or render $render either doesn't show anything or literally prints out an array.
You can do the following:

In hook_preprocess_page() add the following code.

$block_entity = Drupal\block_content\Entity\BlockContent::load(BLOCK_ID);// BLOCK_ID
$block_view = \Drupal::entityTypeManager()-&gt;getViewBuilder('block_content')-&gt;view($block_entity, 'DISPLAY_VIEW_NAME');
if ($block_view) {
    $variables['page']['my_block'] = $block_view;
}


In your Twig template, print the block with the following code.

{% if page.my_block %}
  {{ page.my_block }}
{% endif %}