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()->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()->getViewBuilder('block_content')->view($block);
Trying to print or render $render
either doesn't show anything or literally prints out an array.
block
view builder instead ofblock_content
.THEME_preprocess_node
. Just add it to $variables as$variables['mycustomblock'] = $render
and in node.twig.html print it out with{{ mycustomblock }}