drupal_get_filename
- Versions
- 4.6 – 7
drupal_get_filename($type, $name, $filename = NULL)
Returns and optionally sets the filename for a system item (module, theme, etc.). The filename, whether provided, cached, or retrieved from the database, is only returned if the file exists.
This function plays a key role in allowing Drupal's resources (modules and themes) to be located in different places depending on a site's configuration. For example, a module 'foo' may legally be be located in any of these three places:
modules/foo/foo.module sites/all/modules/foo/foo.module sites/example.com/modules/foo/foo.module
Calling drupal_get_filename('module', 'foo') will give you one of the above, depending on where the module is located.
Parameters
$type The type of the item (i.e. theme, theme_engine, module).
$name The name of the item for which the filename is requested.
$filename The filename of the item if it is to be set explicitly rather than by consulting the database.
Return value
The filename of the requested item.
Code
includes/bootstrap.inc, line 632
<?php
function drupal_get_filename($type, $name, $filename = NULL) {
// The location of files will not change during the request, so do not use
// drupal_static().
static $files = array();
if (!isset($files[$type])) {
$files[$type] = array();
}
if (!empty($filename) && file_exists($filename)) {
$files[$type][$name] = $filename;
}
elseif (isset($files[$type][$name])) {
// nothing
}
// Verify that we have an active database connection, before querying
// the database. This is required because this function is called both
// before we have a database connection (i.e. during installation) and
// when a database connection fails.
else {
try {
$file = db_query("SELECT filename FROM {system} WHERE name = :name AND type = :type", array(':name' => $name, ':type' => $type))->fetchField();
if (file_exists($file)) {
$files[$type][$name] = $file;
}
}
catch (PDOException $e) {
// The database table may not exist because Drupal is not yet installed,
// or the database might be down. We have a fallback for this case so we
// hide the error completely.
}
// Fallback to searching the filesystem if the database could not find the
// file or the file returned by the database is not found.
if (!isset($files[$type][$name])) {
// We have a consistent directory naming: modules, themes...
$dir = $type . 's';
if ($type == 'theme_engine') {
$dir = 'themes/engines';
$mask = "/$name\.engine$/";
}
elseif ($type == 'theme') {
$mask = "/$name\.info$/";
}
else {
$mask = "/$name\.$type$/";
}
if (!function_exists('drupal_system_listing')) {
require_once DRUPAL_ROOT . '/includes/common.inc';
}
$matches = drupal_system_listing($mask, $dir, 'name', 0);
if (!empty($matches[$name]->uri)) {
$files[$type][$name] = $matches[$name]->uri;
}
}
}
if (isset($files[$type][$name])) {
return $files[$type][$name];
}
}
?>Login or register to post comments 