[Drupal] YAML in drupal 8

| | 2 min read

YAML is a data serialization language which is easily readable to both humans and computers. It is for this very reason that YAML is being used in Drupal 8. YAML stands for YAML Ain't Markup Language.

This is what a Drupal 8 module.info.yml file would look like :


name: Zyx Module
description: Zyx Module description.
;This is how you comment. Description field is mandatory.
package: Custom
type: module
version: 1.0
core: 8.x
dependencies:
  - node

This is how a theme is defined in theme.info.yml file :


name: Zyx
type: theme
description: 'A zyx theme.'
package: Custom
core: 8.x
stylesheets:
  all:
    - css/layout.css
  print:
    - css/print.css
stylesheets-remove:
  - normalize.css
regions:
  header: Header
  content: Content
  sidebar_first: 'Sidebar first'
  footer: Footer

This is how permissions are set in Drupal 8 YAML :


# In views.permissions.yml file.
access all views:
  title: 'Bypass views access control'
  description: 'Bypass access control when accessing views.'
  restrict access: TRUE

This is how routing is set in Drupal 8 YAML :


demo.page:
  path: /demo
  defaults:
    _title: 'Demo'
    _content: '\Drupal\demo\Controller\DefaultController::demo'
  requirements:
    _permission: 'access demo content'

YAML files are parsed by Yaml.php in Drupal 8. It is a symfony component. Another reason to use YAML is because, .info files are Drupal specific and YAML is a created to provide data serialization. In Drupal 8, not just info files but permissions, routing and settings configuration are also defined in YAML format now. Earlies this was accomplished in the .module file. This makes the code more lean. To describe it in two words, compartmentalization and delegating.