6

Is there a Drupal function that returns the absolute theme path? Or is there a canonical / base practice way of doing this correctly returning the theme path?

What I've found so far is this:

<?php
// @file template.php
$template_path = DRUPAL_ROOT . drupal_get_path('theme', 'foo');

I'm looking to require_once the template path in a template.php file like so:

<?php
$template_path = DRUPAL_ROOT . drupal_get_path('theme', 'coo');
// ...
// Check file exists etc
require_once $template_path;

3 Answers 3

18

The base_path() function would return the base bath of the Drupal installation

Returns the base URL path of the Drupal installation. At the very least, this will always default to /.

and path_to_theme() will return the path to the current theme.

Return the path to the current themed element.

It can point to the active theme or the module handling a themed implementation. For example, when invoked within the scope of a theming call it will depend on where the theming function is handled. If implemented from a module, it will point to the module. If implemented from the active theme, it will point to the active theme. When called outside the scope of a theming call, it will always point to the active theme.

1
  • Beware using the path_to_theme(), in case of subtheme your path will be broken, because it will point to your subtheme instead of the main theme! And subtheme doesn't need to have the same files and structure as the main theme. So if you need to create your theme's a subtheme, don't use path_to_theme(). Commented Feb 14, 2020 at 16:50
2

Mohammed Shameem's answer is correct, but if you're really looking for the correct way (the Drupal way) to render a custom template, do not use include() or require_once(). Instead, use the Drupal theme system. Changing the output of something that already has a template is easy, see Overriding themable output to learn how to override a template. If there is no template yet, you would have to learn about hook_theme, registering your own templates, and render arrays.

0

You can try this module: System stream wrapper

It provides wrappers(examples from module's site) :

  • module://file/icons/text-plain.png

  • theme://bartik/screenshot.png

  • theme://default/screenshot.png

  • profile://minimal/minimal.info

  • profile://current/standard.info (profile://current expands to the active profile for the site)

  • library://ckeditor/images/spacer.gif (works only if the Libraries module is enabled)

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.