Community Documentation

drupal_get_path

5 common.inc drupal_get_path($type, $name)
6 common.inc drupal_get_path($type, $name)
7 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.

▾ 41 functions call drupal_get_path()

aggregator_init in modules/aggregator/aggregator.module
Implementation of hook_init().
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.
color_scheme_form in modules/color/color.module
Form callback. Returns the configuration form.
color_scheme_form_submit in modules/color/color.module
Submit handler for color change form.
comment_form in modules/comment/comment.module
Generate the basic commenting form, for appending to a node or display on a separate page.
comment_render in modules/comment/comment.module
Renders comment(s).
dblog_init in modules/dblog/dblog.module
drupal_eval in includes/common.inc
Evaluate a string of PHP code.
forum_init in modules/forum/forum.module
Implementation of hook_init().
help_main in modules/help/help.admin.inc
Menu callback; prints a page listing a glossary of Drupal terminology.
help_page in modules/help/help.admin.inc
Menu callback; prints a page listing general help for a module.
hook_init in developer/hooks/core.php
Perform setup tasks. See also, hook_boot.
install_tasks in ./install.php
Tasks performed after the database is initialized.
module_load_include in includes/module.inc
Load a module include file.
node_init in modules/node/node.module
Implementation of hook_init().
node_mass_update in modules/node/node.admin.inc
Make mass update of nodes, changing all nodes in the $nodes array to update them with the field values in $updates.
openid_form_alter in modules/openid/openid.module
Implementation of hook_form_alter : adds OpenID login to the login forms.
openid_user_identities in modules/openid/openid.pages.inc
Menu callback; Manage OpenID identities for the specified user.
poll_init in modules/poll/poll.module
Implementation of hook_init().
search_form in modules/search/search.module
Render a search form.
search_menu in modules/search/search.module
Implementation of hook_menu().
system_clean_url_settings in modules/system/system.admin.inc
Form builder; Configure Clean URL settings.
system_date_time_settings in modules/system/system.admin.inc
Form builder; Configure the site date and time settings.
system_init in modules/system/system.module
Implementation of hook_init().
theme_color_scheme_form in modules/color/color.module
Theme color form.
theme_profile_admin_overview in modules/profile/profile.admin.inc
Theme the profile field overview into a drag and drop enabled table.
theme_taxonomy_overview_terms in modules/taxonomy/taxonomy.admin.inc
Theme the terms overview as a sortable list of terms.
theme_taxonomy_term_page in modules/taxonomy/taxonomy.pages.inc
Render a taxonomy term page HTML output.
theme_update_report in modules/update/update.report.inc
Theme project status report.
tracker_page in modules/tracker/tracker.pages.inc
Menu callback. Prints a listing of active nodes on the site.
user_admin_settings in modules/user/user.admin.inc
Form builder; Configure user settings for this site.
user_init in modules/user/user.module
Implementation of hook_init().
user_menu in modules/user/user.module
Implementation of hook_menu().
_drupal_maintenance_theme in includes/theme.maintenance.inc
Sets up the theming system for site installs, updates and when the site is in off-line mode. It also applies when the database is unavailable.
_locale_translate_language_list in includes/locale.inc
List languages in search result table
_menu_router_build in includes/menu.inc
Helper function to build the router table based on the data from hook_menu.
_menu_site_is_offline in includes/menu.inc
Checks whether the site is off-line for maintenance.
_theme_build_registry in includes/theme.inc
Rebuild the hook theme_registry cache.
_user_password_dynamic_validation in modules/user/user.module
Add javascript and string translations for dynamic password validation (strength and confirmation checking).

File

includes/common.inc, line 1749
Common functions that many Drupal modules will need to reference.

Code

<?php
function drupal_get_path($type, $name) {
  return dirname(drupal_get_filename($type, $name));
}
?>

Comments

use dirname(__FILE__) for better performance

If you need to refer to other files within the same module.

<?php
dirname
(__FILE__);
?>

doesn't require a database hit so has better performance

It's a good idea to:

Program to an interface not an implementation. That's the whole point of this function. :)

Clarifying DB hits...

Just to clarify a few points about seanburlington comment on database hits:

  • drupal_get_filename statically caches its results so you only have a DB hit on the first drupal_get_path call 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.
  • You can often use relative paths within a module. Use dirname(__FILE__) when you need the whole path.

Return Value

The 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

you 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

Had some problems with this. Here's a simple way to use this:

<?php
   
function 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.jpg

Hope this helps someone.

Even better...

It'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:

<?php
function 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.jpg

Note that the function call has been replaced by a variable, thus making it even better, right?

Cheers

Remember that the $name

Remember that the $name variable 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 the name variable within that file is My Theme Name, use mytheme for the $name variable.

Cross link: path_to_theme()

Cross linking to function for current theme...

path_to_theme()

Available $types?

Shouldn't available types be listed or cross-referenced here?

Try this it will work.....

like 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

if its in theme
drupal_add_js(drupal_get_path('theme', 'themename') . '/themename.js');

Login or register to post comments