I have a custom module and placed comment.tpl.php here:
/sites/all/modules/mymodule/comment.tpl.php
But Drupal is not looking for that file in my custom module directory. How can i make it work?
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityYou need to use hook_theme:
/**
* Implements hook_theme().
*/
function MY_MODULE_theme($existing, $type, $theme, $path) {
if($type != 'module') {
return [];
}
// If you want to put your templates in a templates directory
// in your module directory add .'/templates' to the end.
$module_path = drupal_get_path('module', 'MY_MODULE');
return [
'comment' => [
'template' => 'comment',
'path' => $module_path,
],
'other_template' => [
'template' => 'comment',
'path' => '/my/custom/path/here',
],
];
}
This has set up our custom theme, then when you want to use it you can:
$tpl_data = [
'param1' => $foo,
'param2' => $bar,
];
return theme('comment', $tpl_data);
Now $param1 and $param2 will be available in your template file too.
If you don't need any custom data in your template then $tpl_data can be omitted.
Happy days.
You can do this by using hook_theme() as described on api.drupal.org. It allows you to register your module's theme implementation.