1

I want my module (events) to have its own template files but extend the base theme currently enabled.

hook_theme():

function events_theme($existing, $type, $theme, $path) {
    return array(
        'test_events' => array(
            'variables' => array('testingvar' => 123),
            'template' => 'test_events',
            'path' => drupal_get_path('module', 'events') . '/templates',
            'base hook' => 'mytheme'
        )
    );
}

In my menu callback I have: print theme('test_events');

This outputs the contents of the template file, but it doesn't recognize the current theme that is enabled. Is there anyway I can extend the themes layout?


Edit: current situation:

Abstract of my directory structure:

Modules\
-custom\
--events\
---templates\
----test_events.tpl.php

Themes\
-mytheme\
--templates\
---test_events.tpl.php

events.module:

function events_theme($existing, $type, $theme, $path) {
    return array(
        'different_event_hook' => array(
            'path' => $path . '/templates',
            'template' => 'test_events'
        )
    );
}

menu callback:

print theme('different_event_hook');

Result renders from module template file, if I change different_event_hook to test_events it renders from the theme template file.

1 Answer 1

1

The Drupal process is Core -> Module -> Theme. Trying to work backwards on that route is bad design, and should be avoided at all costs. Modules should not be based on themes, nor should they have any dependencies on themes, as themes are entirely dependent on modules, and downstream of modules.

If you need to make changes based on theme, it should happen in the theme.

Example. In your module, define hook_theme()

function hook_theme($existing, $type, $theme, $path)
{
  return array
  (
    'my_theme_hook' => array
    (
      // the template will be in [MODULE_ROOT]/templates
      'path' => $path . '/templates',
      'template' => 'my-theme-hook', // leave off the .tpl.php
    ),
  );
}

The above defines a new theme hook called my_theme_hook, and the output of that theme hook is built in the template [MODULE_ROOT]/templates/my-theme-hook.tpl.php

Now you can use [MODULE_ROOT]/my-theme-hook.tpl.php, or even [MODULE_ROOT]/templates/my-theme-hook.tpl.php (this location works automatically for any templates). Clear your cache, and now the template in your theme will be used instead of the template in the module. Again, don't forget to clear your cache - the template will not be picked up until then.

18
  • Thanks for your response Jaypan, I will do some research and try work out how to achieve it in the order you have listed.
    – Kiee
    Commented Apr 18, 2017 at 13:10
  • To override a module template, you copy the template into your theme to start overriding. You cannot go in the other direction.
    – Kevin
    Commented Apr 18, 2017 at 13:37
  • So do I define the templates in the modules hook_theme, and then create the template files inside the theme?? If so that seems counter productive as I would have to specify my themes path in the module hook_theme so to know where the templates are?
    – Kiee
    Commented Apr 18, 2017 at 14:08
  • No, you define templates in hook_theme() in your module, provide default templates in your module, then you provide overrides of those templates in your theme if they need to have some sort of theme specific templating.
    – Jaypan
    Commented Apr 18, 2017 at 14:15
  • 1
    I just added an example.
    – Jaypan
    Commented Apr 18, 2017 at 14:54

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.