8

I created a module that should be able to display the content of a certain content type. The displaying is kinda complex and to be able to turn it off and to group everything together i created a module for this.

But in the end it should just override the normal node.tpl.php with a custom .tpl.php file. When i put the file in my template folder it gets picked up and the pages shows as intended, but i can't get it to work when the file is in module folder.

I read a lot of things about regsiter the .tpl file and preprocess but nothing that combines the pieces i need.

This is what i have so far

function apps_theme($existing, $type, $theme, $path) {
    $items = array(
        'node--app' => array(
            'template' =>  drupal_get_path('module', 'app') . '/node--app',
            'variables' => array('node' => (object)array())
        )
    );
    return $items;
}

function apps_preprocess_node(&$vars) {
    $variables['theme_hook_suggestions'][] = 'node__app';
}
  • 1
    I may be called out on this, but in my (not so) humble opinion, a module shouldn't care about what a node looks like when displayed, all it should care about is passing that data to the theme, which then does the display portion. Now, if you wanted your module to create a custom object that is displayed in a node, that's another story, then you could create a template for that. But, if you want to short circuit that step, then putting your node.tpl.php in the theme directory is the only 'right' way of doing it. – Jimajamma Dec 12 '11 at 17:38
  • If anybody is looking to do this specifically in Drupal 6, check out this answer: stackoverflow.com/a/6153043/1154642 – bryanbraun Nov 22 '13 at 18:51
3

It is not clear in your question if you are placing your template in sites/all/custom/modules, or in sites/all/custom/modules/the_name_of_mymodule. It needs to be the later.

However, if you:

  • Have your template in the proper place.
  • Visited the admin/appearance path to force a theme registry refresh.
  • Cleared all the caches aftewards.

...but still no dice, you can always specify in your hook_theme() implementation the theme_path key:

<?php
function apps_theme($existing, $type, $theme, $path) {
    $items = array(
        'node_app' => array(
            // Don't specify the path in the template name.
            // Unless you have your template inside a directory within this module.
            'template' =>  'node--app',
            'variables' => array('node' => (object)array()),
            // If you want to put the tpl in another location, you can use this key.
            'theme path' => drupal_get_path('module', 'another_module'),
        ),
    );
    return $items;
}
  • still doesnt work, do i need the theme_hook_suggestions or should pick it pick up the template on his own? 'App' is content type, 'Apps' the module name – dazz Dec 13 '11 at 9:30
  • Maybe the problem that you are having is the name of the theme function node--app. A better name would be node_app, and you would call the theming function by doing theme('node_app', [$vars]); I'm not sure theme('node--app') is something I've ever seen, and almost sure it would not work. – amateur barista Feb 1 '12 at 8:14
  • Also, the $path variable is provided so you don't have to do the drupal_get_path() dance. $path is the location of the module that is implementing the hook_theme, in this case the module named apps. – amateur barista Feb 1 '12 at 8:18
  • 1
    theme name needs to be 'node__app' (double underscore) not 'node_app' – Junaid Feb 19 '13 at 8:32
3

Thought I would post the solution that I found:

function MYMODULE_theme($existing, $type, $theme, $path) {
  return array(
    'node__NODETYPE' => array(
      'render element' => 'content',
      'base hook' => 'node',
      'template' => 'node--NODETYPE',
      'path' => drupal_get_path('module', 'MYMODULE') . '/templates',
    ),
  );
}
2

I got it working by following this:

http://www.metachunk.com/blog/adding-module-path-drupal-7-theme-registry

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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