Introduction to YAML | Drupal 8

Hey Drupalers!

Before delving into the intricacies of the topic to be focused today, it would be notably important to mention that the second week of GSoC'17 has commenced and it's time to move on to a more pressing week of coding, albeit the Drupal way! Previously I had blogged about my experience and objectives completed during the community bonding period, which can be found here.

Let's get started on today's topic then: Why YML?

Now, if you have worked with Drupal before, you are probably familiar with the dot info file. 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. Now, the dot info structure is actually Drupal-specific. It's not used anywhere else. So in Drupal 8, we switched over to using a YAML file instead. That's the .yml extension here.

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. Following are some of the essential features of YAML files:

  • They are used for the file and for assigning the related routes.
  • 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 a single line from a info.yml file: name: Welcome


The code for a info.yml file for a typical module would look like this:

name: Welcome

type: module

description: Display a message when a user logs in

core: 8.x

package: Custom

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.

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 for a  Hello World module route helloworld.routing.yml:


   path: '/helloworld'


      _controller: 'Drupal\helloworld\Controller\FirstController::content'

      _title: 'Hello world'


      _permission: 'access content'

In the above sample code some of the lines are indented. This is because YAML files support hierarchy via indentation.

Another example:


    title: 'Sample module settings'

    description: 'A basic module to return hello world'

    parent: system.admin_config_development

    route_name: sample_module.content

    weight: 100

In the above YAML, all lines below sample_module.admin are indented, which means they are nested under sample_module.admin. In YAML, this is called a compound data type, which sounds complex, but it a lot like an array in PHP. It means that everything listed under sample_module.admin are children of it.

The PHP equivalent of the above code would be the following:

array (

   'sample_module.admin' => array(

      'name' => 'Sample module settings',

      'description' => 'A basic module to return hello world',

      'parent' => 'system.admin_config_development',

      'route_name' => 'sample_module.content',

      'weight' => '100',



So, this was a brief insight into the exciting possibilties of YAML files. Hopefully it provides some helpful context for the same.