You are here

public function AddIgnoredConfigForm::submitForm in Config Distro 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides ConfigFormBase::submitForm

File

modules/config_distro_ignore/src/Form/AddIgnoredConfigForm.php, line 122

Class

AddIgnoredConfigForm
Class AddIgnoredConfigForm.

Namespace

Drupal\config_distro_ignore\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  parent::submitForm($form, $form_state);
  $name = $form_state
    ->getValue('name');
  if (!strlen($name)) {
    throw new \RuntimeException('The config name can not be empty');
  }
  $collection = $form_state
    ->getValue('apply_collection');
  if (!strlen($collection)) {
    throw new \RuntimeException('The collection can not be empty');
  }
  if ($collection == 'all' && $form_state
    ->getValue('type') == 'hash') {

    // When ignoring just the hash but for all collections, we save the hash
    // In the collection keys instead of the all key.
    $hash = DistroIgnoreFilter::hashConfig($this->distroStorage
      ->read($name));
    $this
      ->addNameToConfig('default_collection', $name . DistroIgnoreFilter::HASH_SEPARATOR . $hash);

    // Create hashes for all collections.
    foreach ($this->distroStorage
      ->getAllCollectionNames() as $collectionName) {
      $storage = $this->distroStorage
        ->createCollection($collectionName);
      $hash = DistroIgnoreFilter::hashConfig($storage
        ->read($name));
      $this
        ->addNameToConfig('custom_collections.' . $collectionName, $name . DistroIgnoreFilter::HASH_SEPARATOR . $hash);
    }
  }
  else {

    // Select the config key to use.
    switch ($collection) {
      case 'all':
        $key = 'all_collections';
        break;
      case 'default':
        $key = 'default_collection';
        break;
      default:
        $key = 'custom_collections.' . $collection;
        if ($collection != StorageInterface::DEFAULT_COLLECTION) {

          // Set the distro storage to use the collection so that the hash is
          // later calculated correctly.
          $this->distroStorage = $this->distroStorage
            ->createCollection($collection);
        }
        break;
    }

    // Save the name in the list.
    if ($form_state
      ->getValue('type') == 'hash') {

      // Create the hash of the config as read from the distro storage.
      $hash = DistroIgnoreFilter::hashConfig($this->distroStorage
        ->read($name));
      $this
        ->addNameToConfig($key, $name . DistroIgnoreFilter::HASH_SEPARATOR . $hash);
    }
    else {
      $this
        ->addNameToConfig($key, $name);
    }
  }

  // Redirect back to the page we came from.
  $form_state
    ->setRedirect('config_distro.import');

  // Clear the config_filter plugin cache.
  \Drupal::service('plugin.manager.config_filter')
    ->clearCachedDefinitions();
}