Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Last updated on
3 April 2017
Summary
Drupal 8 implements the PSR-4 standard for package-based PHP namespace autoloading by the PHP
Framework Interoperability Group. Upgrading Drupal 7 modules to Drupal 8 will require using PSR-4
standards. See more background info on Drupal 8 development here, for PSR-4 in context.
modules/vegetable/
o css/
o js/
o src/
Controller/
VegetableController.php → class
Drupal\vegetable\Controller\VegetableController
Form/
VegetableForm.php → class Drupal\vegetable\Form\VegetableForm
Plugin/
Block/
VegetableBlock.php → class
Drupal\vegetable\Plugin\Block\VegetableBlock
Entity/
Tomato.php → class Drupal\vegetable\Entity\Tomato
Cucumber.php → class Drupal\vegetable\Entity\Cucumber
tests/
TomatoTest.php → class Drupal\vegetable\Tests\Entity\TomatoTest
CucumberTest.php → class Drupal\vegetable\Tests\Entity\CucumberTest
VegetableManagerTest.php → class
Drupal\vegetable\Tests\VegetableManagerTest
templates/
fixtures/
weather-data.json
o tests/
src/
Functional/
Kernel/
Unit/
TomatoTest.php → class Drupal\Tests\vegetable\Unit\TomatoTest
Traits/
VegetableTestTrait.php → trait
Drupal\Tests\vegetable\Traits\VetegableTestTrait
o vegetable.info.yml
o vegetable.routing.yml
o vegetable.module
Explanation:
2. The module's namespace is mapped to the ./src/ folder in the module directory.
3. Anything after the module namespace directly maps to the directory and file structure in the ./src/
folder.
Namespace resolution
The namespace of all Drupal core components, as well as contributed modules, begins with Drupal\
The first parts of a namespaced class name indicate the base namespace that maps to a registered base
directory, in which PHP files will be looked up:
For modules, $modulename is the unique machine name of the module, which consists of lowercase
characters and underscores.
The remaining part of a namespaced class name indicates the relative path within the base directory: each
PHP namespace separator (\) is replaced with a directory separator (/) and the .php extension is appended:
Base namespace Relative class name → Base directory Relative file path
Drupal\Component core/lib/Drupal/Componen
Diff\DiffEngine → Diff/DiffEngine.php
\ t/
Drupal\node\ Entity\Node → core/modules/node/src/ Entity/Node.php
Drupal\Tests\views Form\Ajax\RearrangeFilte core/modules/views_ui/test Form/Ajax/RearrangeFilterTe
→
_ui\ rTest s/src/ st.php
Plugin\Block\DevelSwitc Plugin/Block/DevelSwitchUs
Drupal\devel\ → modules/contrib/devel/src/
hUser er.php