drupal_get_filename

  1. drupal
    1. 5
    2. 6
    3. 7
    4. 8
Versions
5 – 8 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, 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.

▾ 6 functions call drupal_get_filename()

BootstrapGetFilenameTestCase::testDrupalGetFilename in modules/simpletest/tests/bootstrap.test
Test that drupal_get_filename() works correctly when the file is not found in the database.
drupal_get_path in includes/common.inc
Returns the path to a system item (module, theme, etc.).
drupal_load in includes/bootstrap.inc
Includes a file with the provided type and name. This prevents including a theme, engine, module, etc., more than once.
module_list in includes/module.inc
Returns a list of currently active modules.
simpletest_test_get_all in modules/simpletest/simpletest.module
Get a list of all of the tests provided by the system.
system_list in includes/module.inc
Build a list of bootstrap modules and enabled modules and themes.

Code

includes/bootstrap.inc, line 826

<?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 {
      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 (!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];
  }
}
?>