You are here

function drupal_get_filename in Drupal 8

Same name and namespace in other branches
  1. 4 includes/bootstrap.inc \drupal_get_filename()
  2. 5 includes/bootstrap.inc \drupal_get_filename()
  3. 6 includes/bootstrap.inc \drupal_get_filename()
  4. 7 includes/bootstrap.inc \drupal_get_filename()
  5. 9 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.

12 calls to drupal_get_filename()
DrupalKernelTest::testCompileDIC in core/tests/Drupal/KernelTests/Core/DrupalKernel/DrupalKernelTest.php
Tests DIC compilation.
drupal_get_path in core/includes/bootstrap.inc
Returns the path to a system item (module, theme, etc.).
ExtensionInstallStorage::getAllFolders in core/lib/Drupal/Core/Config/ExtensionInstallStorage.php
Returns a map of all config object names and their folders.
GetFilenameTest::testDrupalGetFilename in core/tests/Drupal/KernelTests/Core/Bootstrap/GetFilenameTest.php
Tests that drupal_get_filename() works when the file is not in database.
InstallerDependenciesResolutionTest::testDependenciesResolution in core/modules/system/tests/src/Kernel/Installer/InstallerDependenciesResolutionTest.php
Verifies that the exception message in the profile step is correct.

... See full list

File

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

Code

function drupal_get_filename($type, $name, $filename = NULL) {

  // 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_get_path(). /core/core.info.yml does not exist, but is required
  // since drupal_get_path() 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);
  }
}