2

I had code to programmatically render a block.

$block = \Drupal\block\Entity\Block::load('bartik_search');
$block_content = \Drupal::entityManager()
  ->getViewBuilder('block')
  ->view($block);
return array('#markup' => 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?

6
  • Do you check block with delta bartik_search exited? You can debug with kint($block) to see it.
    – user62538
    Commented Nov 3, 2016 at 10:58
  • You should not need to call the view builder. Block plugins have a ::build method that returns a render array, which you can just return. What are you trying to do that you need to render and then place into a new render array?
    – mradcliffe
    Commented Nov 3, 2016 at 11:36
  • Hi @Jonh , I check block bartik_search exited..
    – Henry Tran
    Commented Nov 4, 2016 at 2:56
  • Hi @mradcliffe ..so what's solution ? Could you show code ?
    – Henry Tran
    Commented Nov 4, 2016 at 3:08
  • I really do not understand what you are trying to do? Are you trying to place a Search Form or a Search Block Form on the page somewhere? Where on the page? Through what mechanism? Knowing that I can provide the best answer. Thanks.
    – mradcliffe
    Commented Nov 4, 2016 at 13:05

1 Answer 1

2

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')->renderRoot() instead.

See also: How can I programmatically display a block?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.