6

I'd like to have a custom twig template for custom block plugins from my custom module.

My custom block HeaderText contains

public function build() {
    return array(
        '#title' => 'Test',
        '#color' => $this->configuration['color'],
        );
}

It is located in myplugin/src/Plugin/Block/HeaderText.php

I have a template block--HeaderText.html.twig located in myplugin/templates which only contains test code

<h1>{{ title }}</h1><p>{{ color }}</p>

However the template isn't used. Following a guide I also added the following to myplugin/myplugin.module

function myplugin_theme($existing, $type, $theme, $path){
    return array('myplugin' =>
        array(
            'variables' => array(
                'title' => 'Default title',
                'color' => null
                ), 
                'template' => 'block--HeaderText'
            )
        );
}

Which did not work either. And even if the previous code did work, I have multiple plugins within that module and that code seems to apply to every block which I don't want to do. I'd like each block to have a unique template.

11

This issue I ran into was I did not specify '#theme' in the Block Plugin.

I added

public function build() {
    return array(
        '#theme' => 'myplugin',
        '#title' => 'Test',
        '#color' => $this->configuration['color'],
        );
}

and it worked

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.