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. 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? Commented Nov 3, 2016 at 11:36
  • Hi @Jonh , I check block bartik_search exited.. Commented Nov 4, 2016 at 2:56
  • Hi @mradcliffe ..so what's solution ? Could you show code ? 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. 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?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.