I have a custom module named abc
to simplify explaining this issue:
modules/custom/abc/abc.module:
function abc_theme($existing, $type, $theme, $path) {
return [
'abc' => [
'variables' => [
'data' => [],
],
],
];
}
modules/custom/abc/src/Plugin/Block/AbcBlock.php:
/**
* Provides a 'ABC' Block.
*
* @Block(
* id = "abc_block",
* admin_label = @Translation("Abc Block"),
* category = @Translation("Content"),
* )
*/
class AbcBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build(): array {
return [
'#theme' => 'abc',
'#data' => ['foo' => 'bar'],
'#cache' => [
'tags' => ['dehs'],
],
];
}
}
modules/custom/abc/templates/abc.html.twig:
{{ foo }}
When I run drush cr
, abc.html.twig renders on the page. However, when I click Rebuild Cache [/rebuild-cache-access/rebuild-cache?token=] from admin toolbar, the block doesn't render. Error logs: Theme hook abc not found.
After that I run drush cr
again and the template appears again. Why does it lose reference to template after in-browser Rebuild Cache, and then works again when I run 'drush cr'?
One more thing: If I move the abc_theme() from abc.module to my default .theme (along with abc.html.twig), Rebuild Cache link doesn't cause the error. Simple solution would be to keep abc.html.twig in the theme, however, I have a specific functionality that is generated with abc module and I would like to keep all relating hooks inside the abc module. What gives?