1

In my page.html.twig I have

<h2>{{ 'Block title'|t }}</h2>
<div class="">
  {{ drupal_block('leftblock') }}
  {{ drupal_block('rightblock') }}
</div>

I want to display Block title only if my custom leftblock and rightblock are filled.

Thanks.


I tried

If I do in my my_module.module

function my_module_preprocess_page(&$variables) {
  $block = Block::load('leftblock');
  if ($block) {
    $variables['leftblock'] = \Drupal::entityTypeManager()
      ->getViewBuilder('block')
      ->view($block);
  }
  $block = Block::load('rightblock');
  if ($block) {
    $variables['rightblock'] = \Drupal::entityTypeManager()
      ->getViewBuilder('block')
      ->view($block);
  }
}

and in my page.html.twig

{% if leftblock or rightblock %}
  <h2>{{ 'Block title'|t }}</h2>
{% endif %}
<div class="my-class">
  {{ leftblock }}
  {{ rightblock }}
</div>

This {{ rightblock }} display the real content of the block in twig( when is filled ), but It always display Block title because the block have always something

if I do {{ dump(rightblock) }} i got this when the block is empty or filled.

array(3) { ["#cache"]=> array(4) { ["keys"]=> array(3) { [0]=> string(11) "entity_view" [1]=> string(5) "block" [2]=> string(22) "rightblock" } ["contexts"]=> array(2) { [0]=> string(28) "languages:language_interface" [1]=> string(8) "url.site" } ["tags"]=> array(2) { [0]=> string(10) "block_view" [1]=> string(41) "config:block.block.rightblock" } ["max-age"]=> int(-1) } ["#weight"]=> int(0) ["#lazy_builder"]=> array(2) { [0]=> string(42) "Drupal\block\BlockViewBuilder::lazyBuilder" [1]=> array(3) { [0]=> string(22) "rightblock" [1]=> string(4) "full" [2]=> NULL } } }

I have two themes :

block-myLeft.html.twig

block-myRight.html.twig

  • How I should name my my_module_preprocess_page() ? – Carlos Feb 20 '18 at 2:19
0

You could preprocess your template

function mytheme_preprocess_page(&$variables) {
  $block = \Drupal\block\Entity\Block::load('leftblock');
  if ($block) {
    $variables['leftblock'] = \Drupal::entityTypeManager()
      ->getViewBuilder('block')
      ->view($block);
  }
  $block = \Drupal\block\Entity\Block::load('rightblock');
  if ($block) {
    $variables['rightblock'] = \Drupal::entityTypeManager()
      ->getViewBuilder('block')
      ->view($block);
  }
}

Than you can do this in your template

{% if rightblock or leftblock %}
  <h2>{{ 'Block title'|t }}</h2>
{% endif %}
<div class="">
  {{ leftblock }}
  {{ rightblock }}
</div>
  • Insasse I tried but I git same result – Carlos Feb 19 '18 at 20:06
0

It seems this is an issue discussed elsewhere. Perhaps one of those proposals will help you?

Without knowing exactly what a "full" lazybuilder array is supposed to look like, I can only offer a speculative suggestion based on the assumption regarding what the lazybuilder array's null value might imply. If it means there's no data, then perhaps you could try:

{% if leftblock['#lazy_builder'][1][2] is not null or rightblock['#lazy_builder'][1][2] is not null %}
    <h2>{{ 'Block title'|t }}</h2>
{% endif %}

For clarity, here is the variable dump formatted:

array(3) { 
    ["#cache"]=> array(4) { 
        ["keys"]=> array(3) { 
            [0]=> string(11) "entity_view" 
            [1]=> string(5) "block" 
            [2]=> string(22) "rightblock" 
        } 
        ["contexts"]=> array(2) { 
            [0]=> string(28) "languages:language_interface" 
            [1]=> string(8) "url.site" 
        } 
        ["tags"]=> array(2) { 
            [0]=> string(10) "block_view" 
            [1]=> string(41) "config:block.block.rightblock" 
        } 
        ["max-age"]=> int(-1) 
    } 
    ["#weight"]=> int(0) 
    ["#lazy_builder"]=> array(2) { 
        [0]=> string(42) "Drupal\block\BlockViewBuilder::lazyBuilder" 
        [1]=> array(3) { 
            [0]=> string(22) "rightblock" 
            [1]=> string(4) "full" 
            [2]=> NULL 
        } 
    } 
}
0

I couldn't solve my issue like that. What I did was to use only one block and pass 2 variables with a list of content in each. And then in twig I check if there any elements in each variable. So I display the title in that moment.

Block/myBlock.php

$build[] = [
  '#theme' => 'my_block',
  '#left_content' => $itemsL,
  '#right_content' => $itemsR,
];

My my-block.html.twig

{% if left_content is not empty or right_content is not empty %}
  <h2>{{ 'Block title'|t }}</h2>
{% endif %}

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.