Member-only story
How to Override a Module Template In Drupal
Alter the template without having to modify the module
Sometimes we need to be able to edit a module template, but if it’s a contrib module, that would involve creating a patch and in most cases, the changes we need to make to the template are unique to our project, not really for others.
What can we do in this case then? We can create a template outside of the module that will override the module’s one.
For this example, I’m overriding the template of module dropzonejs in a custom module (administration), but you can do it as well from the theme. We just need to add two functions and create our custom template. These are the steps:
- First, declare the new template:
/**
* Implements hook_theme_suggestions_alter().
*/
function administration_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
// Use switch so we can easily add another hook in the future.
switch ($hook) {
case 'dropzonejs':
$suggestions[] = $hook . '__' . 'custom';
break;
}
}
- Then, declare the path to the template:
/**
* Implements hook_theme().
*/
function administration_theme($existing, $type, $theme, $path) {
return [
'dropzonejs__custom' => [
'base hook' =>…