4

I am trying to render the theme logo as a block.

When I call path_to_theme() in page.tpl.php, it returns a true path, but when I call it in modulename.module, it returns "modules/system".

0

5 Answers 5

11

Whenever I'm stuck in a module, or a context that path_to_theme() doesn't return what I'd like - I use the following to access the current globally selected theme.

global $theme;
$path_to_theme = drupal_get_path('theme',$theme);

For instance if I am in a subtheme, or a slighly different flavour (mobile vs. desktop) of my theme, I use $theme that to get my active theme.

2

I had this same issue. I found that it was because I only had a page--front.tpl.php template file and not a page.tpl.php template. I added the page.tpl.php and everything worked again :)

1
  • That is totally strange! What does even a blank page.tpl.php do to correct this issue! Commented Dec 4, 2013 at 13:59
2

When inside a theming context (e.g. a theme function or a template file), path_to_theme() returns the path to the current themed element, not the current active theme. So, when you call it from a theme, it returns the theme, but when called from a module, it returns the path to the module that created that themed element.

Since you're calling it in foo.module and you're getting modules/system, it's likely you're calling it from a theming context that does not have a custom implementation of page.tpl.php. Thus, it defaults to core's path for the base templates: modules/system.

If you're trying to get the path to the active theme, use path_to_theme() outside of a theming context.

Otherwise, you can use:

variable_get('theme_default', 'bartik') 

to get the default theme, or:

global $user;
print $user->theme;

to get the current user's theme.

2

I also encountered this problem when I am calling path_to_theme function inside a region .tpl file.. Using the code concept of Mr. electblake

I created a function in my Drupal theme template.php like this:

function mythemepath() {
global $theme;
$mythemepathvar = drupal_get_path('theme', $theme ) ;
return $mythemepathvar;
}

And now I can simply call it from any of my template files.

<img src="<?php echo mythemepath(); ?>/images/imagethumbnail.png" />
1

The only time that it occurred with m was because there wasn't an theme_preprocces_node (or page if your problem is on an page file) on my theme. So it was pointing to some module that have it.

To solve, I've just declared the preprocces like:

function mytheme_preprocces_node(&vars)
{

}

and magically it worked.

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.