0

My module now has a lot of TWIG template files in the /custom/MYMODULE/template folder. I tried creating some sub-folders because I thought Drupal 8was supposed to search all sub-folders automatically but I got an error about a missing template file. Should it work?

I've tried setting the subfolder name in '#theme' => 'SUBFOLDER/mytemplate' and I don't get an error, but the block contents are just blank (although the title displays).

I have my template defined in MYMODULE_theme(), but it feels wrong to specify a sub-folder in there.

4
  • Or maybe I am supposed to set the subfolder name in '#theme' => 'SUBFOLDER/mytemplate' - You are asking a question which you could try by yourself. Why do you want to make others to try?
    – ssibal
    Commented Jun 10, 2016 at 9:49
  • I've tried several things, but there are so many permutations to try, I was hoping there was an obvious answer.
    – Nic
    Commented Jun 10, 2016 at 10:03
  • Now your question looks like much more you care!: )
    – ssibal
    Commented Jun 10, 2016 at 10:07
  • I guess the better question is where have you seen it written that Drupal will automatically scan all sub-folders of './templates'? If it's somewhere in official docs and it doesn't work, it's a bug that needs fixing. If it's not in the official docs, you might just be reading incorrect/obsolete information.
    – Clive
    Commented Jun 10, 2016 at 10:23

1 Answer 1

0

This is what the docs for hook_theme say about the template property:

If specified, the theme implementation is a template file, and this is the template name. Do not add 'html.twig' on the end of the template name. The extension will be added automatically by the default rendering engine (which is Twig.) If 'path' is specified, 'template' should also be specified. If neither 'template' nor 'function' are specified, a default template name will be assumed. For example, if a module registers the 'search_result' theme hook, 'search-result' will be assigned as its template name.

Unless you've found a bug of omission, I would expect these docs to tell you if there was any auto-scanning of sub-folders going on. The fact that you've tried it and it doesn't work might be seen to back that up.

If you've seen other official docs that reference an auto-scanning feature, you may have hit a bug.

As an aside, I'm not sure what the intent of this code is:

'#theme' => 'SUBFOLDER/mytemplate'

You would never write that; you would instead use hook_theme to register your template with the system, and put the path to the file itself in the template property. Then you'd use:

'#theme' => 'theme_key'

in the render array

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