Same name and namespace in other branches
  1. 4.6.x includes/bootstrap.inc \drupal_get_filename()
  2. 4.7.x includes/bootstrap.inc \drupal_get_filename()
  3. 5.x includes/bootstrap.inc \drupal_get_filename()
  4. 6.x includes/bootstrap.inc \drupal_get_filename()
  5. 7.x includes/bootstrap.inc \drupal_get_filename()
  6. 8.9.x core/includes/bootstrap.inc \drupal_get_filename()

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:

core/modules/foo/foo.info.yml modules/foo/foo.info.yml sites/example.com/modules/foo/foo.info.yml

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; one of 'core', 'profile', 'module', 'theme', or 'theme_engine'.

$name: The name of the item for which the filename is requested. Ignored for $type 'core'.

$filename: The filename of the item if it is to be set explicitly rather than by consulting the database.

Return value

string The filename of the requested item or NULL if the item is not found.

Deprecated

in drupal:9.3.0 and is removed from drupal:10.0.0. Use \Drupal\Core\Extension\ExtensionPathResolver::getPathname() instead.

See also

https://www.drupal.org/node/2940438

2 calls to drupal_get_filename()
drupal_get_path in core/includes/bootstrap.inc
Returns the path to a system item (module, theme, etc.).
GetFilenameTest::testDrupalGetFilename in core/tests/Drupal/KernelTests/Core/Bootstrap/GetFilenameTest.php
Tests drupal_get_filename() deprecation.

File

core/includes/bootstrap.inc, line 190
Functions that need to be loaded on every Drupal request.

Code

function drupal_get_filename($type, $name, $filename = NULL) {
  @trigger_error('drupal_get_filename() is deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. Use \\Drupal\\Core\\Extension\\ExtensionPathResolver::getPathname() instead. See https://www.drupal.org/node/2940438', E_USER_DEPRECATED);

  // Type 'core' only exists to simplify application-level logic; it always maps
  // to the /core directory, whereas $name is ignored. It is only requested via
  // \Drupal\Core\Extension\ExtensionList::getPath(). The file
  // /core/core.info.yml does not exist, but is required since
  // ExtensionList::getPath() returns the dirname() of the returned pathname.
  if ($type === 'core') {
    return 'core/core.info.yml';
  }
  try {

    /** @var \Drupal\Core\Extension\ExtensionList $extension_list */
    $extension_list = \Drupal::service("extension.list.{$type}");
    if (isset($filename)) {

      // Manually add the info file path of an extension.
      $extension_list
        ->setPathname($name, $filename);
    }
    return $extension_list
      ->getPathname($name);
  } catch (ServiceNotFoundException $e) {

    // Catch the exception. This will result in triggering an error.
    // If the service is unknown, create a user-level error message.
    trigger_error(sprintf('Unknown type specified: "%s". Must be one of: "core", "profile", "module", "theme", or "theme_engine".', $type), E_USER_WARNING);
  } catch (\InvalidArgumentException $e) {

    // Catch the exception. This will result in triggering an error.
    // If the filename is still unknown, create a user-level error message.
    trigger_error(sprintf('The following %s is missing from the file system: %s', $type, $name), E_USER_WARNING);
  }
}