0

I am loading a block programmatically in a theme preprocess function, and trying to render that block in twig using without to remove some fields. However, the block is still printed in full.

This is the code I am using.

function my_preprocess_node(&$variables) {
    $footer_narrow = \Drupal\block\Entity\Block::load('my_block');
    $variables['my_block'] = \Drupal::entityManager()
      ->getViewBuilder('block')
      ->view($my_block);
}

The template code is the following one.

{{ my_block|without('field_1', 'field_2') }}

I also noticed while debugging, that while the result in twig of my_block is a render array, it does not include the keys I'm expecting (the problem, I assume?).

This is the dump() output.

array(3) {
  '#cache' =>
  array(4) {
    'keys' =>
    array(3) {
      [0] =>
      string(11) "entity_view"
      [1] =>
      string(5) "block"
      [2] =>
      string(17) "my_site_footer"
    }
    'contexts' =>
    array(3) {
      [0] =>
      string(28) "languages:language_interface"
      [1] =>
      string(17) "my_theme_section"
      [2] =>
      string(14) "my_theme_site"
    }
    'tags' =>
    array(2) {
      [0] =>
      string(10) "block_view"
      [1] =>
      string(36) "config:block.block.my_block"
    }
    'max-age' =>
    int(-1)
  }
  '#weight' =>
  int(0)
  '#lazy_builder' =>
  array(2) {
    [0] =>
    string(42) "Drupal\block\BlockViewBuilder::lazyBuilder"
    [1] =>
    array(3) {
      [0] =>
      string(17) "my_site_footer"
      [1] =>
      string(4) "full"
      [2] =>
      NULL
    }
  }
}
  • The block is not rendered in the node template. You can theme the block only in a block template, which you'll find in the twig debug output. – 4k4 Apr 26 '18 at 22:09
2

One way to do this is with a new view mode for blocks. You can add one at /admin/structure/display-modes/view/add (Manage > Structure > Display Modes > View Modes > Add new view mode). Then, you have two options, 1. go to your custom block type and configure this view mode display to only have the fields you want, or 2. create a template file with a name like block--BLOCK-TYPE--MY-NEW-VIEW-MODE.html.twig, then remove the fields you don't want to print like you were trying to do in the node template. (Replace BLOCK-TYPE with your block type's machine name, and MY-NEW-VIEW-MODE with the machine name of the new view mode you created. Convert all underscores in these machine names to dashes.) After you've done this you can update your preprocess hook's view() call like ->view($my_block, 'MY_NEW_VIEW_MODE')(keep the underscores in the machine name of your view mode here). This will make your preprocess hook look like:

function my_preprocess_node(&$variables) {
  $footer_narrow = \Drupal\block\Entity\Block::load('my_block');
  $variables['my_block'] = \Drupal::entityManager()
    ->getViewBuilder('block')
    ->view($my_block, 'MY_NEW_VIEW_MODE);
}

Lastly, because you add your block to the variables array with the my_block key, update your node's twig template to simply be:

{{ my_block }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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