I am using hook_theme_suggestions_HOOK() to implement a custom template file in my drupal 8 website like below.

 * Implements hook_theme_suggestions_HOOK().
function mymodule_theme_suggestions_page(array $variables) {
  $template_suggestions = array();
  // This is used to display only $content.
  if (isset($_GET['ajax']) && $_GET['ajax'] == 1) {
    $template_suggestions[] = 'page__ajax';
  return $template_suggestions;

Above code is working fine and I can add template page--ajax.html.twig inside my current active theme and changes in template is reflecting in page.

But when I put that file inside my module itself file is not detecting and changes not reflecting in page and taking default page.html.twg template file.

How can I provide default template file within my module itself?


1 Answer 1


This issue got resolved by adding hook_theme(). Normally theme suggestion templates are only picked up when they are in themes. So We need explicitly define theme suggestions using hook_theme() inside a module. Then only it takes templeate from your modules templates folder.

 * Implements hook_theme().
function mymodule_theme() {
  return array(
    'page__ajax' => array(
      'render element' => 'elements',
      'base hook' => 'page',
  • You should specifiy 'base hook', see system_theme(). Otherwise some things will not run they way they do with a normal template override.
    – Berdir
    Jul 20, 2016 at 21:06
  • which is base hook for page template? html?
    – Cool
    Jul 21, 2016 at 11:31
  • no, that is not what base hook means. It is just 'page'.
    – Berdir
    Jul 21, 2016 at 11:55
  • I updated in answer. Is that correct?
    – Cool
    Jul 22, 2016 at 6:54
  • Yes, that looks correct to me.
    – Berdir
    Jul 22, 2016 at 8:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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