Can you override a tpl file in a module? For example, I want to override user-picture.tpl.php in a module to change the user icon display based on a user-entered selection on nodes, comments, and the user profile page.

Comments

span’s picture

You should be able to copy the tpl.php file to your theme's folder and empty the cache so Drupal knows about it and then make changes.

socketwench’s picture

Wonderful! Thanks!!!

bradpeabody’s picture

I just spent several hours debugging an odd theming issue where I was trying to use this technique (Drupal 6.16). I had a .tpl.php file in my custom module which worked properly, but copying this same .tpl.php file to the theme folder did nothing. I flushed the cache many times, tried running the module in another Drupal instance, various things, nothing worked - it was consistently busted.

The issue ended up being that both the name of theme hook and the tpl file had dashes in them, as opposed to what I assume to be the correct convention - using underscores in the theme hook name and dashes in the file name.

So this did NOT work (meaning the .tpl.php file was only found in the module folder):

/** implement hook_theme */
function my_module_theme() {
    
    return array(
            'example-theme-hook' => array(
                    'arguments' => array(
                        'some_data' => NULL,
                        ),
                    'template' => 'example-theme-hook',
            ),
	);
}

...
    theme('example-theme-hook', /* some data */);
...

But this DID work (copying the .tpl.php file to the theme folder automatically made that override the one in my module folder):

/** implement hook_theme */
function my_module_theme() {
    
    return array(
            'example_theme_hook' => array(
                    'arguments' => array(
                        'some_data' => NULL,
                        ),
                    'template' => 'example-theme-hook',
            ),
	);
}

...
    theme('example_theme_hook', /* some data */);
...

This appears to be due to some code in includes/theme.inc starting on line 909 where it is replacing underscores and dashes as part of the drupal_find_theme_templates() function.

Just a note for any other poor souls who run into this same issue... hopefully this'll show up in response to your googling.

vensires’s picture

It did show up! Thanx for the hours I earned and I would have missed if you hadn't posted this here :D