You are here

public function UpdateConfigurationForm::createConfigFiles in Config Direct Save 8

Same name and namespace in other branches
  1. 8.2 src/Form/UpdateConfigurationForm.php \Drupal\config_direct_save\Form\UpdateConfigurationForm::createConfigFiles()

Override the old configurations.

1 call to UpdateConfigurationForm::createConfigFiles()
UpdateConfigurationForm::submitForm in src/Form/UpdateConfigurationForm.php
Form submission handler.

File

src/Form/UpdateConfigurationForm.php, line 118

Class

UpdateConfigurationForm
Provide the settings form for updating configurations.

Namespace

Drupal\config_direct_save\Form

Code

public function createConfigFiles(array &$form, FormStateInterface $form_state) {

  // Get the name of config source( sync, text, etc...).
  $config_directory_selected = $form_state
    ->getValue('config_directory');
  $config_files_names = $this->configManager
    ->getConfigFactory()
    ->listAll();

  // If the user check to make a backup, a directory will be created.
  if ($form_state
    ->getValue('backup')) {
    $current_date = date('d-m-Y-H-i-s');
    $folder_backup = $config_directory_selected . "-" . $current_date;
    $this
      ->recurseCopy($config_directory_selected, $folder_backup);
  }

  // Delete old configurations files( except .htaccess).
  $this
    ->unlinkRecursive($config_directory_selected, 'yml');
  foreach ($config_files_names as $name) {

    // Data associated to file.
    $data = Yaml::encode($this->configManager
      ->getConfigFactory()
      ->get($name)
      ->getRawData());

    // Create new files.
    file_put_contents($config_directory_selected . "/{$name}.yml", $data);
  }

  // Get all override data from the remaining collections.
  foreach ($this->targetStorage
    ->getAllCollectionNames() as $collection) {
    $target = str_replace('.', '/', $collection);
    $this
      ->unlinkRecursive($config_directory_selected . "/" . $target . "/", 'yml');
    if (!is_dir($config_directory_selected . "/" . $target . "/")) {
      mkdir($config_directory_selected . "/" . $target . "/", 0775, TRUE);
    }
    $collection_storage = $this->targetStorage
      ->createCollection($collection);
    foreach ($collection_storage
      ->listAll() as $name) {
      $target = str_replace('.', '/', $collection);
      file_put_contents($config_directory_selected . "/" . $target . "/{$name}.yml", Yaml::encode($collection_storage
        ->read($name)));
    }
  }
  $this
    ->messenger()
    ->addMessage($this
    ->t("The configuration has been uploaded."));
}