I recently tried a pattern I had read about somewhere that allows you to use views templates from a module.

The advantage of this is that you can switch themes while having your Views templates all stored in a common place from a module directory.

Basically the idea is that you define this hook_views_api call in a custom module :


function MYMODULE_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'MYMODULE'),
    'template path' => drupal_get_path('module', 'MYMODULE') . '/templates',
  );
}

And then your Views templates are available for any theme when stored in that templates directory in the module.

However this pattern does not work with Bootstrap 3.x sub themes in my experience. It does work however with the other themes I tried.

The other issue I found, is that if this hook_views_api call is enabled, then templates placed in the bootstrap sub themes templates directory do not work at all either, and while they do not give a true WSOD, no output from the view is rendered. You just end up with nicely formed <html> and <body> tags but nothing else.

I didn't have the time find out why this is exactly, but I post this here for others who may have tried this and failed.

The only solution I found was not to use this pattern at all when working with Bootstrap sub themes and Views templates.

If anyone has any ideas one why this is, I am curious.

Comments

psy’s picture

Issue summary: View changes
markhalliwell’s picture

Category: Bug report » Support request
Status: Active » Closed (works as designed)

That is because themes will always take precedence over a module's template suggestion (that's just how the theme system works).

The pattern mentioned sounds like an anti-pattern more than anything: "store common theme templates in a module"... that just sounds like a recipe for disaster.

I'm really not all that surprised it doesn't work well with a base-theme (like this) that does some pretty hardcore theme registry alters out-of-the-box. That being said, this base-theme only has one template override, for views tables: http://cgit.drupalcode.org/bootstrap/tree/templates/views.

It shouldn't affect anything else provided by a module though, so I'm not sure what else is going on there. Views, in general, can be a bit tricky as it also does some pretty interesting things in how it provides "templates" and "suggestions".

If anything, I would strongly recommend using a grandchild theme model for your themes:
1. Create a sub-theme of Bootstrap as your own custom "base-theme".
2. Then create additional sub-themes of your newly created custom "base-theme".

That way all your sub-sub-themes have inherited your custom base-theme's templates overrides. This should work with any theme, regardless if it's something as complex as this base-theme or as simple as Bartik.

FWIW, I have overridden views templates in my sub-themes very easily. Are you using 7.x-3.0 or the actual latest code? The last release did have some issues with detecting theme sub-theme templates: https://www.drupal.org/node/2224003