| 7 common.inc | drupal_get_path($type, $name) |
| 4.6 common.inc | drupal_get_path($type, $name) |
| 4.7 common.inc | drupal_get_path($type, $name) |
| 5 common.inc | drupal_get_path($type, $name) |
| 6 common.inc | drupal_get_path($type, $name) |
| 8 common.inc | drupal_get_path($type, $name) |
Returns the path to a system item (module, theme, etc.).
Parameters
$type: The type of the item (i.e. theme, theme_engine, module, profile).
$name: The name of the item for which the path is requested.
Return value
The path to the requested item.
42 calls to drupal_get_path()
- aggregator_init in modules/
aggregator/ aggregator.module - Implementation of hook_init().
- block-admin-display-form.tpl.php in modules/
block/ block-admin-display-form.tpl.php - block-admin-display-form.tpl.php Default theme implementation to configure blocks.
- block_admin_display_form in modules/
block/ block.admin.inc - Generate main blocks administration form.
- book_init in modules/
book/ book.module - Implementation of hook_init(). Add's the book module's CSS.
- color_get_info in modules/
color/ color.module - Retrieve the color.module info for a particular theme.
File
- includes/
common.inc, line 1764 - Common functions that many Drupal modules will need to reference.
Code
function drupal_get_path($type, $name) {
return dirname(drupal_get_filename($type, $name));
}
Comments
use dirname(__FILE__) for better performance
PermalinkIf you need to refer to other files within the same module.
<?phpdirname(__FILE__);
?>
doesn't require a database hit so has better performance
It's a good idea to:
PermalinkProgram to an interface not an implementation. That's the whole point of this function. :)
Clarifying DB hits...
PermalinkJust to clarify a few points about seanburlington comment on database hits:
drupal_get_filenamestatically caches its results so you only have a DB hit on the firstdrupal_get_pathcall for a given module or theme.global $theme_path;is a quick way to get at the path to the current theme without hitting the DB.dirname(__FILE__)when you need the whole path.Return Value
PermalinkThe path returned by this function will be relative to the DocumentRoot in which Drupal is running.
if trying to include a template from a subfolder in your module
Permalinkyou may not need to use the path argument of the hook_theme(), instead you can just prefix the value of the template string with the name of the subfolder that contains the template
'template' => 'templates/mymoduletemplate' ,etc...
In action
PermalinkHad some problems with this. Here's a simple way to use this:
<?phpfunction my_path() {
return drupal_get_path('theme', 'YourThemeName');
}
?>
<img src="<?php echo my_path(); ?>/img/test.jpg">
You can condense this down further and include it in a header that's required on every page and end up with a relative path variable like:
<?php echo p(); ?>/img/test.jpgHope this helps someone.
Even better...
PermalinkIt's usually best to avoid calling functions in .tpl files (excepting the obvious use-cases of course).
In this case, why don't you add a variable to all page templates via template_preprocess_page() to hold your relative path?
This would condense all those function calls into just one, or none if you write the p() function contents inside your _preprocess_page() function.
What i mean is writing this in template.php:
<?phpfunction mytheme_preprocess_page(&$vars){
$vars['p'] = drupal_get_path('theme', 'mytheme');
}
?>
This avoids all those function calls (since the function above is called only once!), and in your .tpl file you'd have something like this:
<?php echo $p; ?>/img/test.jpgNote that the function call has been replaced by a variable, thus making it even better, right?
Cheers
Remember that the $name
PermalinkRemember that the
$namevariable is the machine-readable name, which is the name of your .info file, not the human-readable name contained in the .info file. So, for a theme, if your info file is called mytheme.info, and thenamevariable within that file is My Theme Name, use mytheme for the$namevariable.Cross link: path_to_theme()
PermalinkCross linking to function for current theme...
path_to_theme()
Available $types?
PermalinkShouldn't available types be listed or cross-referenced here?
Try this it will work.....
Permalinklike this.
drupal_add_js(drupal_get_path('module', 'project_extras') . '/project_extras.js');
or
drupal_add_js(drupal_get_path('module', 'yourmodulename') . '/yourmodulename.js');
if its in
Permalinkif its in theme
drupal_add_js(drupal_get_path('theme', 'themename') . '/themename.js');
How to navigate to another directory?
PermalinkFor example, I'd like to get the path of sites/all/libraries/. What can I do?
base_path() .
Permalinkbase_path() . "/sites/all/libraries/";
or
global $base_path;
$base_path . "sites/all/libraries";
Assuming you have the
PermalinkAssuming you have the Libraries module installed, you can use libraries_get_path()
http://drupalcontrib.org/api/drupal/contributions%21libraries%21librarie...
Trailing Slash
PermalinkNote that drupal_get_path() does NOT add a trailing slash. So if you want to include some file in module's folder, you will need to add the slash yourself.