0

I'm trying to render or print my custom blocks on my node twig template and currently receiving a fatal error message.

Argument 1 passed to Drupal\block\BlockViewBuilder::view()

Here is the snippet of code I'm working on my .theme file

function iom_preprocess_node(&$variables) {

    $block = \Drupal\block\Entity\Block::load(917);
    $block_content = \Drupal::entityTypeManager()
      ->getViewBuilder('block')
      ->view($block);
}

$block returns NULL when I try to do a dump($block)

$block = \Drupal\block\Entity\Block::load(917);
dump($block);

Tried using twig tweak also to resolve the issue instead of having to pre-process it but no luck

{{ drupal_block('twitterfeed') }}

enter image description here enter image description here enter image description here

4
  • 1
    Check if the variable $block returns NULL Commented Oct 16, 2018 at 2:49
  • @AnishSheela just checked and yes it returned NULL. Checked the Block ID and it was correct
    – clestcruz
    Commented Oct 16, 2018 at 3:01
  • 1
    It returned null means that there is an error loading that block. You need to check if block exists and corresponding module is enabled. Commented Oct 16, 2018 at 6:16
  • enable block in any theme region ,once block rendered using code ,disable it not remove
    – user52318
    Commented Oct 16, 2018 at 6:34

2 Answers 2

2

You can print blocks directly using Twig Tweak

  {# No need to configure this block on "admin/structure/block" page. #}
  <dt>Block:</dt>
  <dd>{{ drupal_block('system_branding_block', {label: 'Branding', use_site_name: false}) }}</dd>

  {# Bypass block.html.twig theming. #}
  <dt>Block (without wrapper):</dt>
  <dd>{{ drupal_block('system_branding_block', wrapper=false) }}</dd>

Source: Cheat sheet

Do not use the block's machine name, enable the devel and in block configuration click on Devel tab to get the Plugin ID.

For example, the Site Branding Block has machine name of sitebranding, but the plugin ID is system_branding_block

enter image description here

2
  • Tried this also using twig tweak to render the block on the node.html.twig template. Got an error that the block is missing {{ drupal_block('twitterfeed') }}. I have updated my question to provide some screenshots. Twig Tweak is installed by the way
    – clestcruz
    Commented Oct 16, 2018 at 5:00
  • @clestcruz updated the answer, most likely, you're using the wrong id.
    – No Sssweat
    Commented Oct 16, 2018 at 5:31
1

Can you try this:

$bid = ?;
$block = \Drupal\block_content\Entity\BlockContent::load($bid);
$render = \Drupal::entityTypeManager()->getViewBuilder('block_content')->view($block);
return $render;
0

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.