Can't render a block due to recoverable fatal error I had code to programmatically render a block.

$block = \Drupal\block\Entity\Block::load('bartik_search');
$block_content = \Drupal::entityManager()
  -&gt;getViewBuilder('block')
  -&gt;view($block);
return array('#markup' =&gt; drupal_render($block_content));


After updating to Drupal 8.2.x, I get the following error:


  Recoverable fatal error: Argument 1 passed to Drupal\block\BlockViewBuilder::view() must implement interface


How do I fix this error?
The error:


  Recoverable fatal error: Argument 1 passed to Drupal\block\BlockViewBuilder::view() must implement interface Drupal\Core\Entity\EntityInterface


usually is caused by viewing the invalid block (NULL). So for some reason Block::load() fails to load your block (bartik_search). So clear your caches and check whether the block exists at Block Layout page, theme is enabled and permissions are configured properly. If this still happening and the block is definitely there, please raise a relevant issue ticket.

You can always add the condition or try/catch block whether the code loads the block correctly.

Please also note that drupal_render() is deprecated and you should return $block_content instead.@googletorp Or use \Drupal::service('renderer')-&gt;renderRoot() instead.

See also: How can I programmatically display a block?