Add recommendations for YAML files coding standards to minimize usage of inline JSON constructions

Created on 21 June 2023, about 1 year ago
Updated 22 June 2023, about 1 year ago

Problem/Motivation

We already have a coding standard for YAML files here https://www.drupal.org/docs/develop/coding-standards/configuration-file-... β†’

But it describes too few rules.

I see that even in Drupal Core we have a mix of YAML and inline JSON constructions like this: https://git.drupalcode.org/project/drupal/-/blob/10.0.0/core/modules/com...

services:
  comment.breadcrumb:
    class: Drupal\comment\CommentBreadcrumbBuilder
    arguments: ['@entity_type.manager']
    tags:
      - { name: breadcrumb_builder, priority: 100 }

  comment.manager:
    class: Drupal\comment\CommentManager
    arguments: ['@entity_type.manager', '@config.factory', '@string_translation', '@module_handler', '@current_user', '@entity_field.manager', '@entity_display.repository']

  comment.statistics:
    class: Drupal\comment\CommentStatistics
    arguments: ['@database', '@current_user', '@entity_type.manager', '@state', '@database.replica']
    tags:
      - { name: backend_overridable }

So, using brackets instead of plaintext in { name: backend_overridable } looks not good, and lines with a list of arguments longer than 120 characters (we have 173 in the example) - too.

So we can use just the YAML language without JSON injections, and with it, the file will even look much clear! Here is a cleaned up example:

services:
  comment.breadcrumb:
    class: Drupal\comment\CommentBreadcrumbBuilder
    arguments:
      - '@entity_type.manager'
    tags:
      - name: breadcrumb_builder
        priority: 100

  comment.manager:
    class: Drupal\comment\CommentManager
    arguments:
      - '@entity_type.manager'
      - '@config.factory'
      - '@string_translation'
      - '@module_handler'
      - '@current_user'
      - '@entity_field.manager'
      - '@entity_display.repository'

  comment.statistics:
    class: Drupal\comment\CommentStatistics
    arguments:
      - '@database'
      - '@current_user'
      - '@entity_type.manager'
      - '@state'
      - '@database.replica'
    tags:
      - name: backend_overridable

Also, we have a mix of class FQCN formats with a leading backslash and without it, even within a single file, eg this https://git.drupalcode.org/project/drupal/-/blob/10.0.0/core/core.servic...

  router:
    class: Drupal\Core\Routing\AccessAwareRouter
    arguments: ['@router.no_access_checks', '@access_manager', '@current_user']
  router.no_access_checks:
    class: \Drupal\Core\Routing\Router
    arguments: ['@router.route_provider', '@path.current', '@url_generator']

Proposed resolution

Describe recommendations to avoid the usage of JSON constructions in the YAML files and prefer a YAML formatted lists instead.

Also, describe other recommendations like putting an empty line between each service, missing backslash in class' FQCN, etc.

Remaining tasks

User interface changes

API changes

Data model changes

✨ Feature request
Status

Active

Component

Coding Standards

Created by

πŸ‡¦πŸ‡²Armenia Murz Yerevan, Armenia

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.69.0 2024