Drupal core includes a Composer plugin, drupal/core-composer-scaffold. This takes care of setting up Drupal core files which need to be installed outside of the core/ folder. This process of setting files is called the scaffolding process.
This Composer plugin now writes a DrupalLocations.php file to the Composer project root as part of the scaffolding process. This file is automatically added to the .gitignore by the scaffolding process, and does not need to be committed to version control.
The DrupalLocations.php file contains a class \Drupal\Locations\DrupalLocation. Its purpose is to allow Drupal to determine its location from the class constant \Drupal\Locations\DrupalLocation::APP_ROOT.
For Drupal to access this class, it must be declared to the Composer autoloader in the project root's composer.json. You do not need to do this if Drupal is installed in a standard configuration, as if this file is not found, Drupal guesses its location as it has done in previous versions. The standard configurations are:
- Installed with the drupal/recommended-project Composer template,
- Installed with the drupal/legacy-project Composer template,
- Installed directly from a Drupal core git clone.
To declare this file to the Composer autoloader on an existing project, add the following to the project root's composer.json:
"autoload": {
"psr-4": {
"Drupal\\Locations\\": ""
}
},
Project code in a project which has the drupal/core-composer-scaffold plugin installed can use the class constant \Drupal\Locations\DrupalLocations::APP_ROOT. This can be used as soon as the Composer autoloader has been included.
Third party code which may be installed in a project which does not have the drupal/core-composer-scaffold plugin present should use the method \Drupal\Core\DrupalKernel::getAppRoot().