Last active
September 19, 2019 08:14
-
-
Save lolandese/f65898a56dcb64b7e92e990a87195dce to your computer and use it in GitHub Desktop.
Convert a Drupal YAML snippet to an array as a working PHP code snippet to write its values to the database. This is code to generate code. Short URL: https://git.io/Jem8t
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * @file | |
| * Convert YAML snippet to PHP code snippet to write its values to database. | |
| */ | |
| use Drupal\Component\Serialization\Yaml; | |
| // Replace the input below with any part of a valid YAML snippet. | |
| // Example taken from the config object 'filter.format.full_html', under the key | |
| // 'filters'. | |
| // REPLACE INPUT WITH YOUR OWN CONFIG DATA BELOW! In total 3 variables. | |
| $input_config_object = 'filter.format.full_html'; | |
| // Parent keys from highest to one above the snippet. In this example only one. | |
| $input_parent_keys = [ | |
| 'filters', | |
| ]; | |
| // The snippet can be only a part of a YAML, either from a file or from the | |
| // database at 'admin/config/development/configuration/single/export'. | |
| // Escape double quotes with a backslash (\). | |
| $input_snippet = " | |
| filter_caption: | |
| id: filter_caption | |
| provider: filter | |
| status: true | |
| weight: 9 | |
| settings: { } | |
| filter_htmlcorrector: | |
| id: filter_htmlcorrector | |
| provider: filter | |
| status: true | |
| weight: 10 | |
| settings: { } | |
| "; | |
| // Parse the YAML snippet and convert it to a PHP array. | |
| $yamlDecoded = Yaml::decode(unindent($input_snippet)); | |
| // Initialize variable. | |
| $output = NULL; | |
| // Rules to sanitize the output according to the Drupal coding standard later. | |
| $target = [ | |
| '/array \(/', | |
| '/\)/', | |
| '/true/', | |
| '/false/', | |
| '/=>[ \t]+' . PHP_EOL . '[ \t]+\[' . PHP_EOL . '[ \t]+\],/', | |
| '/=>[ \t]+' . PHP_EOL . '/', | |
| ]; | |
| $replacement = [ | |
| '[', | |
| ']', | |
| 'TRUE', | |
| 'FALSE', | |
| '=> [],', | |
| '=>' . PHP_EOL, | |
| ]; | |
| // Create the name of the configuration object to set. | |
| $parent_keys = implode('.', $input_parent_keys); | |
| // Generate the PHP code snippet. | |
| print('<h2>PHP code to copy and paste</h2>'); | |
| foreach ($yamlDecoded as $key => $value) { | |
| $config_object = print_r(var_export($value, TRUE), TRUE); | |
| $output .= "$$key = " . preg_replace($target, $replacement, $config_object) . "; | |
| \Drupal::configFactory()->getEditable('$input_config_object')->set('$parent_keys.$key', $$key) | |
| ->save();" . PHP_EOL . PHP_EOL; | |
| } | |
| print('<pre>' . highlight_string("<?php | |
| /** | |
| * @file | |
| * Set part of a configuration object in the database. | |
| */ | |
| // Still to export to the configuration YAML file (drush cex). | |
| // Code generated with https://git.io/Jem8t (leave for future reference). | |
| " . $output, TRUE) . '</pre>'); | |
| /** | |
| * Remove excessive indenting. | |
| * | |
| * Removes the first space of each line in a loop until any line does not start | |
| * with a space anymore. Needed to get a valid YAML snippet. | |
| * | |
| * @param string $snippet | |
| * The snippet to unindent. | |
| * | |
| * @return string | |
| * A string containing the unindented snippet. | |
| */ | |
| function unindent($snippet) { | |
| while (!preg_match('/' . PHP_EOL . '\S/', $snippet)) { | |
| $snippet = str_replace([PHP_EOL . ' '], [PHP_EOL], $snippet); | |
| } | |
| return $snippet; | |
| } |
Author
Author
To convert a complete Drupal YAML configuration file to a working PHP code snippet, go to https://git.io/fjjDu.
Also the instructions on how to execute the PHP snippet can be found there.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example
Input
Output (executing the snippet)
Does comply with both Drupal Code Sniffer and Best Practice standards.
PHP code to copy and paste