ConfigDistroImportForm.php in Config Distro 8
File
src/Form/ConfigDistroImportForm.php
View source
<?php
namespace Drupal\config_distro\Form;
use Drupal\config\Form\ConfigSync;
use Drupal\config_distro\Event\ConfigDistroEvents;
use Drupal\Core\Config\NullStorage;
use Drupal\Core\Config\StorageComparer;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ConfigDistroImportForm extends ConfigSync {
public static function create(ContainerInterface $container) {
$class = parent::create($container);
$class->syncStorage = $container
->get('config_distro.storage.distro');
$class->snapshotStorage = new NullStorage();
return $class;
}
public function getFormId() {
return 'config_distro_import_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$storage_comparer = $form_state
->get('storage_comparer');
if (!isset($storage_comparer)) {
$storage_comparer = new StorageComparer($this->syncStorage, $this->activeStorage, $this->configManager);
}
foreach ($storage_comparer
->getAllCollectionNames() as $collection) {
if (isset($form[$collection])) {
foreach (array_keys($form[$collection]) as $config_change_type) {
if (isset($form[$collection][$config_change_type]['list'])) {
foreach ($form[$collection][$config_change_type]['list']['#rows'] as &$row) {
$config_name = $row['name'];
if ($config_change_type == 'rename') {
$names = $storage_comparer
->extractRenameNames($config_name);
$route_options = [
'source_name' => $names['old_name'],
'target_name' => $names['new_name'],
];
}
else {
$route_options = [
'source_name' => $config_name,
];
}
if ($collection != StorageInterface::DEFAULT_COLLECTION) {
$route_name = 'config_distro.diff_collection';
$route_options['collection'] = $collection;
}
else {
$route_name = 'config_distro.diff';
}
$row['operations']['data']['#links']['view_diff']['url'] = Url::fromRoute($route_name, $route_options);
}
}
}
}
}
return $form;
}
public static function finishBatch($success, $results, $operations) {
parent::finishBatch($success, $results, $operations);
if ($success) {
\Drupal::service('event_dispatcher')
->dispatch(ConfigDistroEvents::IMPORT);
}
}
}