INPUT
OUTPUT

Convert a YAML snippet or a YAML file to a PHP formatted array with a simple form at:
admin/config/development/yaml2php

This is a developer utility to generate a code snippet to copy and paste into your own code. Look at the example screenshots on the right (input and output).

Features

  • Convert a YAML file or just part of it (pasting a snippet).
  • All fields are optional. It will process the data given, also just the YAML file name.
  • Generated code will pass Drupal CodeSniffer.
  • Follows recommended best practice.
  • Code contains the PHP formatted array separately. Copy and paste what you need.
  • Syntax highlighting of the code.
  • A provided YAML snippet can come from anywhere, even non-Drupal projects.
  • PHPUnit test coverage (49 assertions).

Use cases

  • The generated code is the Drupal 8 replacement of what was variable_set in Drupal 7.
  • Set a certain configuration if certain conditions are met, wrapping it in an `if` statement.
  • Set a certain configuration dynamically, for example, for all available content types, wrapping it in a `foreach` loop. Example using a dynamic variable and dependency injection.
  • As part of an automated process to set up an environment with certain configuration through PHP after a Drupal bootstrap. Although in those cases using Configuration selector together with install profiles seems a better fit.

Usage

Go to the conversion form at admin/config/development/yaml2php (restricted to users with the permission "Administer site configuration"). Provide a value for the first field only (YAML file name) or for all three fields (YAML snippet). In case just a YAML file name is provided, it must obviously exist. A snippet can also be taken from another project and will be converted anyway.

The snippet can be only a part of a YAML, either from a file or a copy-paste from the database through the interface at 'admin/config/development/configuration/single/export'. There is no need to remove preceding whitespace. Excessive indenting will be removed automatically. Just copy and paste the needed lines as they are. Leaving the snippet empty will result in the PHP code for the complete configuration object taken from the YAML file instead of the database.

As the last step copy and paste the needed part of the PHP snippet to your own code and test locally. The generated working PHP code snippet will write the values to the database when executed. Be aware that it should be followed by a configuration export and commit.

Example

See documentation.

Supporting organizations: 

Project information

  • caution Seeking new maintainer
    The current maintainers are looking for new people to take ownership.
  • Project categories: Developer tools
  • Created by lolandese on , updated
  • shieldStable releases for this project are covered by the security advisory policy.
    There are currently no supported stable releases.

Releases