0

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?

1
  • 1
    Check the file permissions of abc.module, is it readable by the webserver?
    – 4uk4
    Commented Jun 29 at 7:10

1 Answer 1

0

The project is running on Lando Docker local setup. I ended up running lando rebuild and that seemed to resolved the problem. It could have been file permission as @4uk4 suggested.

Your Answer

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

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