1

I have created a region--footer.html.twig template which only contains the static markup for the footer. Now I would like to render this region at the bottom of each page so I have added the following to the end of page.html.twig:

{{ page.footer }}

This works fine as long as I have connected a block to the footer region under admin/structure/block. If no blocks are placed in the region it is regardes as empty and is not rendered. How can I change so the region--footer.html.twig is used even if there is no blocks placed in the region?

I was thinking of using a preprocess function for the region doing something like this:

function template_preprocess_region(&$variables) {
  if ($variables['region'] == 'footer') {
    $variables['content'] = 1;
  }
}
7
  • What purpose would it serve? I guess you can put the markup in page.html.twig and just print content in the region template.
    – Kevin
    Apr 28, 2017 at 10:28
  • @Kevin - I only need to display the footer region on some specific pages. I still don't understand why a region must have a connected block to be rendered, shouldn't the template for the region be parsed anyway? Apr 28, 2017 at 12:27
  • It won't render if it's empty, there's no reason to do the work. Hence, the markup residing in page.html.twig instead will always output, with the region twig override just printing content. Then it wouldn't matter.
    – Kevin
    Apr 28, 2017 at 12:28
  • Think of it like an HTML list - it doesn't make sense to output a <ul> if it's not going to contain any <li>s, same deal with regions and blocks. Regions only exist to house blocks, so a region without blocks serves no purpose and shouldn't render. The obvious solution would be to create a block for your static footer, then you can put it in a region and control visibility in the standard fashion
    – Clive
    Apr 28, 2017 at 14:41
  • @Clive - I'm not sure I understand. I would like to separate the markup from page.html.twig by adding a specific region file with static html. The region template is not empty it conains all the markup for the actual footer - just not any connected blocks. So this can not be done - should not be done? Apr 28, 2017 at 15:02

1 Answer 1

1
/**
 * Implements hook_preprocess_page() for page.html.twig.
 */
 function hook_preprocess_page(array &$variables) {
   $variables['page']['footer'] = [     # Specify the region name
     '#theme_wrappers' => ['region'],
     '#region' => 'footer',             # Specify the region name
     'phantom_block' => [
       '#markup' => ' ',
       '#weight' => "-100",
     ],
   ];
 }

This way you are adding a block with a space to the region. Forcing Drupal to render that region. But as you will put a static HTML in that region, the content of the phantom block will not be rendered.

1
  • Could you please explain how and why this should work? Jul 20, 2018 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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