0
votes

I've created a custom module in Drupal 8. I want the custom module to display a custom header instead of the standard one shown on all other pages.

I've created a custom twig template using the documentation found here: https://www.drupal.org/docs/8/theming/twig/create-custom-twig-templates-for-custom-module. I've also used this as a reference: https://www.drupal.org/forum/support/module-development-and-code-questions/2017-05-15/drupal-8-custom-module-template.

I was able to create the template, however the template seems to be called and rendered after the header template and before the footer template. So with the documentation I'm not able to alter anything in the header file.

Any idea on how I can create a custom twig file for the header for my custom module? Here is snippets of my code below:

module file:

function calendar_theme($existing, $type, $theme, $path) {
    return array(
        'calendar_display' => array(
            'variables' => array('test' => NULL),
            'template' => 'calendar-template',
        ),
    );
}

In my controller file:

namespace Drupal\calendar\Controller;


class CivilCController {

    public function calendar(){

        $content[] = [
            //stuff here
        ];

        $content[] = [
            //stuff here
        ];

        return [
            '#theme' => 'calendar_display',
            '#test' => $content,
        ];

    }

}

Now I have a twig file called calendar-template.html.twig. However this file get rendered below the header.

1
You will have to clarify what you mean by "header". If header is just a region in your theme and is the same on every page, I would personally just change one of the template files of the theme, no need for a module. Another way I may do it is to create a custom module that provides a block, then just place that block in the header region.2pha
You have not said how you are getting your theme code on to the page.2pha

1 Answers

0
votes

In your theme you should have region called "header" and inside it you should place blocks which are giving content to that region. For each block you can define (in block prefs) on what pages it should appear and on what not.

For even more complex use case you can use Context module:

You don't need custom module for that feature.