function drupal_get_filename

7.x bootstrap.inc drupal_get_filename($type, $name, $filename = NULL)
6.x bootstrap.inc drupal_get_filename($type, $name, $filename = NULL)

Returns and optionally sets the filename for a system resource.

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 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 (theme, theme_engine, module, profile).

$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 or NULL if the item is not found.

7 calls to drupal_get_filename()
BootstrapGetFilenameTestCase::testDrupalGetFilename in drupal-7.x/modules/simpletest/tests/bootstrap.test
Test that drupal_get_filename() works correctly when the file is not found in the database.
DrupalUnitTestCase::setUp in drupal-7.x/modules/simpletest/drupal_web_test_case.php
Sets up unit test environment.
drupal_get_path in drupal-7.x/includes/common.inc
Returns the path to a system item (module, theme, etc.).
drupal_load in drupal-7.x/includes/bootstrap.inc
Includes a file with the provided type and name.
module_list in drupal-7.x/includes/module.inc
Returns a list of currently active modules.

... See full list

1 string reference to 'drupal_get_filename'
BootstrapGetFilenameTestCase::testDrupalGetFilename in drupal-7.x/modules/simpletest/tests/bootstrap.test
Test that drupal_get_filename() works correctly when the file is not found in the database.

File

drupal-7.x/includes/bootstrap.inc, line 820
Functions that need to be loaded on every Drupal request.

Code

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(), $dirs = array();

  // Profiles are a special case: they have a fixed location and naming.
  if ($type == 'profile') {
    $profile_filename = "profiles/$name/$name.profile";
    $files[$type][$name] = file_exists($profile_filename) ? $profile_filename : FALSE;
  }
  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 {
      if (function_exists('db_query')) {
        $file = db_query("SELECT filename FROM {system} WHERE name = :name AND type = :type", array(':name' => $name, ':type' => $type))->fetchField();
        if (file_exists(DRUPAL_ROOT . '/' . $file)) {
          $files[$type][$name] = $file;
        }
      }
    }
    catch (Exception $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';
        $extension = 'engine';
      }
      elseif ($type == 'theme') {
        $extension = 'info';
      }
      else {
        $extension = $type;
      }

      if (!isset($dirs[$dir][$extension])) {
        $dirs[$dir][$extension] = TRUE;
        if (!function_exists('drupal_system_listing')) {
          require_once DRUPAL_ROOT . '/includes/common.inc';
        }
        // Scan the appropriate directories for all files with the requested
        // extension, not just the file we are currently looking for. This
        // prevents unnecessary scans from being repeated when this function is
        // called more than once in the same page request.
        $matches = drupal_system_listing("/^" . DRUPAL_PHP_FUNCTION_PATTERN . "\.$extension$/", $dir, 'name', 0);
        foreach ($matches as $matched_name => $file) {
          $files[$type][$matched_name] = $file->uri;
        }
      }
    }
  }

  if (isset($files[$type][$name])) {
    return $files[$type][$name];
  }
}