0

I have a custom block module. I want to override core/themes/bartik/templates/block.html.twig in my-module directory.

In my-module directory I have a folder templates/ and the twig file block--my-module.html.twig.

In modules/custom/my-module/my-module.module i have:

function my_module_theme($existing, $type, $theme, $path) {
   return array(
     'my_module' => array(
     'variables' => array('message' => NULL),
     'template' => 'block--my-module'
    ),
  );
}

If I insert in my build() function in modules/custom/my-module/src/Plugin/Block/MyModule.php e.g:

'#theme' => 'my_module',

my twig template is loaded from modules/custom/my-module/templates/block--my-module.html.twig, but as a child inside the default block.html.twig core/themes/bartik/templates/block.html.twig?

What I am doing wrong here?

  • @leymannx thanks so what I understand is I have to make a theme_suggestions for my block function MYMODULE_theme_suggestions_block_alter(array &$suggestions, array $variables), but how exactly? – meez Oct 21 '18 at 7:23
  • Yes, exactly! :-) – leymannx Oct 21 '18 at 7:31
  • 1
    Try to add ‘base hook’ => ‘block’ and clear the cache – user72672 Oct 21 '18 at 7:57
  • 1
    Try to take a look here drupal.stackexchange.com/questions/200085/… – user72672 Oct 21 '18 at 8:15
  • 2
    I got it {{ content['#test_var'] }} – meez Oct 21 '18 at 8:58

Browse other questions tagged or ask your own question.