Member-only story

How to Override a Module Template In Drupal

Alter the template without having to modify the module

Sergio Guardiola Herrador
2 min readSep 18, 2024
Photo by Luca Bravo on Unsplash

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' =>…

--

--

Sergio Guardiola Herrador
Sergio Guardiola Herrador

Written by Sergio Guardiola Herrador

I write articles in English and Spanish, mostly about programming, technology, travel, money, investing. You can find me here: https://sergioguardiola.net

No responses yet