YAML is a data serialisation format that is both powerful and easy for us humans to read and understand. In Drupal 8 it’s used where Drupal needs a list but doesn’t need to execute PHP. One of the reasons why YAML was chosen for Drupal 8 is because it is already used in Symfony.
In last week’s introduction to namespacing tutorial, you saw YAML files used for the module info file and the route.
They are also used in Drupal 8 to replace info hooks in modules, declaring services, permissions and in the configuration management system.
A YAML file comprises of key and value pairs, separated with a colon. This is how a single line from a YAML file looks:
The key and value pair for this example is:
YAML files replaces .info files
In Drupal 7, .info files were used to provide information to Drupal about modules and themes. This information includes its human readable name, machine name, description, version number, any dependencies and so on. Without a .info file, Drupal wouldn’t know that a module or theme existed.
Drupal 8 uses YAML (.yml) files instead of .info files.
Here is an example of the code in a YAML file. This is the code for the info.yml file for a module.
name: Welcome type: module description: Display a message when a user logs in core: 8.x package: Custom
If you have done any Drupal 7 development, the information will look familiar. One of the key differences between Drupal 7 and 8 is that the key value pair are separated with an equals sign (“=“) in the INI format used in Drupal 7, and colon (“:”) in the YAML format used in Drupal 8. There needs to be a space after the colon.
YAML files define routes
In Drupal 8, YAML files do more than just provide information about modules and themes. As mentioned above, they are also used to define menu items, tabs, routes, services and so on.
Here is the example from last week’s email of the hello module route hello.routing.yml:
hello.content: path: '/hello' defaults: _controller: 'Drupal\hello\Controller\FirstController::content' _title: 'Hello world' requirements: _permission: 'access content'
In the example above, some of the lines are indented. This is because YAML files support hierarchy via indentation.
Here is another example:
first_module.admin: title: 'First module settings' description: 'A basic module to return hello world' parent: system.admin_config_development route_name: first_module.content weight: 100
In the above YAML, all lines below first_module.admin are indented, which means they are nested under first_module.admin. In YAML, this is called a compound data type, which sounds scary, but it a lot like an array in PHP. It means that everything listed under first_module.admin are children of it.
This would be the equivalent of the following PHP array:
array ( 'first_module.admin' => array( 'name' => 'First module settings', 'description' => 'A basic module to return hello world', 'parent' => 'system.admin_config_development', 'route_name' => 'first_module.content', 'weight' => '100', ), );
Hopefully it provides you with some context around YAML files. You’ll end up using YAML files a lot as you embark on Drupal 8 development.