Last active
January 14, 2022 13:36
-
-
Save lolandese/eb0e2392264aca7d736dd712e432410e to your computer and use it in GitHub Desktop.
Convert a Drupal YAML configuration file 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/fjjDu
This file contains 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 a YAML file to PHP code to write its values to the database. | |
*/ | |
use Drupal\Component\Serialization\Yaml; | |
// Replace 'filter.format.full_html' with any other existing configuration | |
// object. The configuration YAML file must exist. Export the configuration | |
// first if that is not the case. | |
$input = 'filter.format.full_html'; | |
// Parse the YAML file and convert it to a PHP array. | |
$yamlDecoded = Yaml::decode(file_get_contents(dirname(\Drupal::root()) . '/config/' . CONFIG_SYNC_DIRECTORY . '/' . $input . '.yml')); | |
// Remove the 'UUID' and '_core'. Not needed in the generated code. | |
unset($yamlDecoded['uuid']); | |
unset($yamlDecoded['_core']); | |
// Create a variable name based on the configuration object name. | |
$dynamic_var = '$' . str_replace('.', '_', $input); | |
// Create a parsable string representation of the YAML file. | |
$output = print_r(var_export($yamlDecoded, TRUE), TRUE); | |
// Sanitize the output according to the Drupal coding standard. | |
$target = [ | |
'/array \(/', | |
'/\)/', | |
'/true/', | |
'/false/', | |
'/=>[ \t]+' . PHP_EOL . '[ \t]+\[' . PHP_EOL . '[ \t]+\],/', | |
'/=>[ \t]+' . PHP_EOL . '/', | |
]; | |
$replacement = [ | |
'[', | |
']', | |
'TRUE', | |
'FALSE', | |
'=> [],', | |
'=>' . PHP_EOL, | |
]; | |
$output = preg_replace($target, $replacement, $output); | |
// Generate the PHP code snippet. | |
print('<h2>PHP code to copy and paste</h2>'); | |
print('<pre>' . highlight_string("<?php | |
/** | |
* @file | |
* Set a complete configuration object in the database. | |
*/ | |
// Still to export to the configuration YAML file (drush cex). | |
// Code generated with https://git.io/fjjDu (leave for future reference). | |
$dynamic_var = $output; | |
\Drupal::configFactory()->getEditable('$input')->setData($dynamic_var) | |
->save(); | |
", TRUE) . '</pre>'); |
An example of usage
Input (to insert on line 13 in the snippet)
filter.format.full_html
Output (executing the snippet)
Does comply with both Drupal Code Sniffer and Best Practice standards.
PHP code to copy and paste
<?php
/**
* @file
* Set a complete configuration object in the database.
*/
// Still to export to the configuration YAML file (drush cex).
// Code generated with https://git.io/fjjDu (leave for future reference).
$filter_format_full_html = [
'langcode' => 'es',
'status' => TRUE,
'dependencies' =>
[
'module' =>
[
0 => 'editor',
],
],
'name' => 'Full HTML',
'format' => 'full_html',
'weight' => 2,
'filters' =>
[
'filter_align' =>
[
'id' => 'filter_align',
'provider' => 'filter',
'status' => TRUE,
'weight' => 8,
'settings' => [],
],
'filter_caption' =>
[
'id' => 'filter_caption',
'provider' => 'filter',
'status' => TRUE,
'weight' => 9,
'settings' => [],
],
'filter_htmlcorrector' =>
[
'id' => 'filter_htmlcorrector',
'provider' => 'filter',
'status' => TRUE,
'weight' => 10,
'settings' => [],
],
'editor_file_reference' =>
[
'id' => 'editor_file_reference',
'provider' => 'editor',
'status' => TRUE,
'weight' => 11,
'settings' => [],
],
],
];
\Drupal::configFactory()->getEditable('filter.format.full_html')->setData($filter_format_full_html)
->save();
To convert a Drupal YAML snippet to a working PHP code snippet, go to https://git.io/Jem8t.
This can also be just a part of a YAML, either from a file or from the database suggested as export at admin/config/development/configuration/single/export.
@todo
Add error handling, messaging and logging.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Set a Drupal 8 configuration object programmatically
The above code generates the needed PHP code from a known configuration YAML file to set the values in the database accordingly. Doing it programmatically instead of an import. Code to generate code.
This might be needed:
if
statementforeach
loopInstructions
In short, you need a local Drupal 8 site to generate the configuration that you need, export that into a YAML file and use that as the input for the code snippet above.
Use a (local) sandbox site to generate your code from a configuration YAML file
Set the configuration
drush cex
).Execute the PHP code snippet
Test and apply
drush cedit filter.format.full_html
(change with your own machine name for the config object).Suggestions for the generated code
Obviously you can wrap the generated code in a conditional, only setting a certain configuration in a specific context, or use dynamic variables.
To illustrate, as an example, the content type machine name 'page' could be replaced with a variable
$content_type
in the generated example code from the comment below (output). Let's say, if we are inside a class now using dependency injection, the first line to set our configuration object would look like:Also other occurrences of 'page' should also be replaced with
$content_type
like:Wrapping the whole code snippet then inside a
foreach
loop to set a certain configuration for each content type:Documentation
Simple Configuration API - Writing configuration - Drupal 8 guide on Drupal.org