View source
<?php
namespace Drupal\config_distro_ignore\Form;
use Drupal\config_distro_ignore\Plugin\ConfigFilter\DistroIgnoreFilter;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class AddIgnoredConfigForm extends ConfigFormBase implements ContainerInjectionInterface {
protected $distroStorage;
public function __construct(ConfigFactoryInterface $config_factory, StorageInterface $distro_storage) {
parent::__construct($config_factory);
$this->distroStorage = $distro_storage;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('config_distro.storage.distro'));
}
protected function getEditableConfigNames() {
return [
'config_distro_ignore.settings',
];
}
public function getFormId() {
return 'config_distro_ignore_add_config';
}
public function buildForm(array $form, FormStateInterface $form_state, $config_name = NULL, $collection = NULL) {
$form = parent::buildForm($form, $form_state);
$form['info'] = [
'#markup' => $this
->t('Retain the configuration: @config', [
'@config' => $config_name,
]),
];
$form['name'] = [
'#type' => 'hidden',
'#value' => $config_name,
];
$form['collection'] = [
'#type' => 'hidden',
'#value' => $collection,
];
$form['type'] = [
'#type' => 'radios',
'#title' => $this
->t('How to retain.'),
'#default_value' => 'permanent',
'#options' => [
'permanent' => $this
->t('Permanently'),
'hash' => $this
->t('This specific version'),
],
];
$names = $this->distroStorage
->getAllCollectionNames();
$options = [
'all' => $this
->t('All collections'),
'default' => $this
->t('Default collection'),
] + array_combine($names, $names);
$form['apply_collection'] = [
'#type' => 'select',
'#title' => $this
->t('Collection'),
'#default_value' => 'all',
'#options' => $options,
];
if ($collection) {
$form['apply_collection']['#default_value'] = $collection;
}
if (empty($names)) {
$form['apply_collection']['#access'] = FALSE;
}
return $form;
}
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') {
$hash = DistroIgnoreFilter::hashConfig($this->distroStorage
->read($name));
$this
->addNameToConfig('default_collection', $name . DistroIgnoreFilter::HASH_SEPARATOR . $hash);
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 {
switch ($collection) {
case 'all':
$key = 'all_collections';
break;
case 'default':
$key = 'default_collection';
break;
default:
$key = 'custom_collections.' . $collection;
if ($collection != StorageInterface::DEFAULT_COLLECTION) {
$this->distroStorage = $this->distroStorage
->createCollection($collection);
}
break;
}
if ($form_state
->getValue('type') == 'hash') {
$hash = DistroIgnoreFilter::hashConfig($this->distroStorage
->read($name));
$this
->addNameToConfig($key, $name . DistroIgnoreFilter::HASH_SEPARATOR . $hash);
}
else {
$this
->addNameToConfig($key, $name);
}
}
$form_state
->setRedirect('config_distro.import');
\Drupal::service('plugin.manager.config_filter')
->clearCachedDefinitions();
}
protected function addNameToConfig($key, $name) {
$settings = $this
->config('config_distro_ignore.settings');
$data = $settings
->get($key);
$data[] = $name;
$data = array_filter($data);
$data = array_unique($data);
sort($data);
$settings
->set($key, $data);
$settings
->save();
}
}