Override admin template defined in a contrib module

Components

Let's say that you want to override the template for the order that is defined in the Commerce Order module. If you don't have a custom Admin theme then you have to override it in a custom module.

You can't do this in the custom user-facing theme, because that theme is not used for admin pages. So, you have to find out the template name by enabling theme debug and inspecting the HTML code.

Image

Now you can implement the hook_theme() hook like this:

/**
 * Implements hook_theme().
 */
function MY_MODULE_theme($existing, $type, $theme, $path) {
  return [
    'commerce_order__admin' => [
      'template' => 'commerce-order--admin-custom',
    ],
  ];
}

And the last step is to create a new Twig template in your custom module: MY_MODULE/templates/commerce-order--admin-custom.html.twig

About the Author

Goran Nikolovski is a creator, speaker, open-source contributor, web developer specializing in Drupal and DevOps enthusiast. He is the founder of this website and he enjoys sharing his knowledge. Need help with anything related to Drupal? Get in touch.