Skip to content

Instantly share code, notes, and snippets.

@svetlio
Last active February 13, 2024 14:21
Show Gist options
  • Save svetlio/91ae92ac830c17677f26eab7392702c5 to your computer and use it in GitHub Desktop.
Save svetlio/91ae92ac830c17677f26eab7392702c5 to your computer and use it in GitHub Desktop.
Import from module config/optional
<?php
// ...
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$module_path = $this->extensionListModule->getPath('cpct_config');
$directory = $module_path . '/config/optional';
$change_list_create = $this->getChangeListCreate();
$batch['title'] = t('Import config ...');
foreach ($change_list_create AS $config_file_name) {
$config_name = str_replace('.yml', '', $config_file_name);
$batch['operations'][] = [
'\Drupal\cpct_config\Batch\BatchImportConfig::process', [
[
'config_name' => $config_name,
'directory' => $directory,
],
],
];
}
$batch['finished'] ='\Drupal\cpct_config\Batch\BatchImportConfig::finished';
batch_set($batch);
// in the batch process use mike-potter's code to import each config (https://gist.github.com/crittermike/2d2c6734c506d509505fa79142125757?permalink_comment_id=3423368#gistcomment-3423368)
}
function getChangeListCreate() {
$module_path = $this->extensionListModule->getPath('cpct_config');
$directory = $module_path . '/config/optional';
$config_file_storage = new FileStorage($directory);
$storage_comparer = new StorageComparer(
$config_file_storage,
$this->configStorage
);
// this create a list with a proper order for importing config
$storage_comparer->createChangelist();
return $storage_comparer->getChangelist('create');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment