class ConfigPartialExportForm in Config Partial Export 8
Construct the storage changes in a configuration synchronization form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\config_partial_export\Form\ConfigPartialExportForm
Expanded class hierarchy of ConfigPartialExportForm
1 string reference to 'ConfigPartialExportForm'
File
- src/
Form/ ConfigPartialExportForm.php, line 19
Namespace
Drupal\config_partial_export\FormView source
class ConfigPartialExportForm extends FormBase {
/**
* The active configuration object.
*
* @var \Drupal\Core\Config\StorageInterface
*/
protected $activeStorage;
/**
* The snapshot configuration object.
*
* @var \Drupal\Core\Config\StorageInterface
*/
protected $snapshotStorage;
/**
* The configuration manager.
*
* @var \Drupal\Core\Config\ConfigManagerInterface
*/
protected $configManager;
/**
* The file system service.
*
* @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;
/**
* The state service.
*
* @var \Drupal\Core\State\StateInterface
*/
protected $state;
/**
* Constructs the object.
*
* @param \Drupal\Core\Config\StorageInterface $active_storage
* The target storage.
* @param \Drupal\Core\Config\StorageInterface $snapshot_storage
* The snapshot storage.
* @param \Drupal\Core\Config\ConfigManagerInterface $config_manager
* Configuration manager.
* @param \Drupal\Core\File\FileSystemInterface $file_system
* The file system service.
* @param \Drupal\Core\State\StateInterface $state
* The state object of the current site instance.
*/
public function __construct(StorageInterface $active_storage, StorageInterface $snapshot_storage, ConfigManagerInterface $config_manager, FileSystemInterface $file_system, StateInterface $state) {
$this->activeStorage = $active_storage;
$this->snapshotStorage = $snapshot_storage;
$this->configManager = $config_manager;
$this->fileSystem = $file_system;
$this->state = $state;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('config.storage'), $container
->get('config.storage.snapshot'), $container
->get('config.manager'), $container
->get('file_system'), $container
->get('state'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'config_partial_export_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$snapshot_comparer = new StorageComparer($this->activeStorage, $this->snapshotStorage, $this->configManager);
$change_list = [];
if ($snapshot_comparer
->createChangelist()
->hasChanges()) {
$this
->messenger()
->addWarning($this
->t('Your current configuration has changed.'));
foreach ($snapshot_comparer
->getAllCollectionNames() as $collection) {
foreach ($snapshot_comparer
->getChangelist(NULL, $collection) as $config_names) {
if (empty($config_names)) {
continue;
}
foreach ($config_names as $config_name) {
$change_list[$config_name]['name'] = $config_name;
}
}
}
}
if (empty($change_list)) {
$user_input = $form_state
->getUserInput();
if (isset($user_input['change_list'])) {
$change_list = $user_input['change_list'];
}
}
ksort($change_list);
$form['change_list'] = [
'#type' => 'tableselect',
'#header' => [
'name' => $this
->t('Name'),
],
'#options' => $change_list,
];
$form['description'] = [
'#markup' => '<p><b>' . $this
->t('Use the export button to download the selected files listed above.') . '</b></p>',
];
$form['addSystemSiteInfo'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Add system.site info'),
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Export'),
];
$last_selection = $this->state
->get('config_partial_export_form');
$current_user_id = $this
->currentUser()
->id();
if (!empty($last_selection[$current_user_id])) {
$current_user_last_selection = $last_selection[$current_user_id];
$form['change_list']['#default_value'] = $current_user_last_selection['status_checkboxes_all'];
$form['addSystemSiteInfo']['#default_value'] = $current_user_last_selection['status_checkbox_system'];
}
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$user_input = $form_state
->getUserInput();
$count = 0;
if (!empty($user_input)) {
foreach ($user_input['change_list'] as $change_item) {
if ($change_item) {
$count++;
}
}
}
if ((empty($user_input['change_list']) || !$count) && empty($user_input['addSystemSiteInfo'])) {
$form_state
->setErrorByName('', $this
->t('No items selected.'));
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$user_input = $form_state
->getUserInput();
$change_list = $user_input['change_list'] ?: [];
$add_system_site_info = $user_input['addSystemSiteInfo'];
$change_list_booleans = [];
foreach ($change_list as $key => $value) {
if ($value) {
$change_list_booleans[$key] = TRUE;
}
}
$last_selection = $this->state
->get('config_partial_export_form');
$last_selection[$this
->currentUser()
->id()] = [
'status_checkboxes_all' => $change_list_booleans,
'status_checkbox_system' => (bool) $add_system_site_info,
];
$this->state
->set('config_partial_export_form', $last_selection);
$this
->createArchive(array_filter($change_list), $add_system_site_info);
$form_state
->setRedirect('config_partial.export_partial_download');
}
/**
* Creates a tarball based on $change_list.
*
* Creates a tarball based on $change_list in the temporary directory
* set on admin/config/media/file-system page.
*
* @param array $change_list
* Array of modified config files.
* @param bool $add_system_site_info
* If TRUE the system.site.yml file will be added to change list.
*/
public function createArchive(array $change_list, $add_system_site_info = FALSE) {
$this->fileSystem
->delete($this->fileSystem
->getTempDirectory() . '/config_partial.tar.gz');
$archiver = new ArchiveTar($this->fileSystem
->getTempDirectory() . '/config_partial.tar.gz', 'gz');
// Get raw configuration data without overrides.
if ($add_system_site_info && !in_array('system.site', $change_list)) {
$change_list[] = 'system.site';
}
foreach ($change_list as $name) {
$yaml = Yaml::encode($this->configManager
->getConfigFactory()
->get($name)
->getRawData());
$archiver
->addString("{$name}.yml", $yaml);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigPartialExportForm:: |
protected | property | The active configuration object. | |
ConfigPartialExportForm:: |
protected | property | The configuration manager. | |
ConfigPartialExportForm:: |
protected | property | The file system service. | |
ConfigPartialExportForm:: |
protected | property | The snapshot configuration object. | |
ConfigPartialExportForm:: |
protected | property | The state service. | |
ConfigPartialExportForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
ConfigPartialExportForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
ConfigPartialExportForm:: |
public | function | Creates a tarball based on $change_list. | |
ConfigPartialExportForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
ConfigPartialExportForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
ConfigPartialExportForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
ConfigPartialExportForm:: |
public | function | Constructs the object. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |