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