ConfigDeleteForm.php in Config Delete 8
File
src/Form/ConfigDeleteForm.php
View source
<?php
namespace Drupal\config_delete\Form;
use Drupal\config\Form\ConfigSingleExportForm;
use Drupal\Core\Form\FormStateInterface;
class ConfigDeleteForm extends ConfigSingleExportForm {
public function getFormId() {
return 'config_delete_form';
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$config_name = $form_state
->getValue('config_name');
if (empty($config_name)) {
$form_state
->setErrorByName('config_name', $this
->t('Please select a valid configuration name.'));
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config_type = $form_state
->getValue('config_type');
$config_name = $form_state
->getValue('config_name');
if ($form_state
->getValue('config_type') !== 'system.simple') {
$definition = $this->entityTypeManager
->getDefinition($config_type);
$name = $definition
->getConfigPrefix() . '.' . $config_name;
}
else {
$name = $config_name;
}
$message = $this
->t('Configuration "@config_name" successfully deleted.', [
'@config_name' => $name,
]);
if ($form_state
->getValue('delete_dependencies')) {
$dependencies = \Drupal::configFactory()
->get($name)
->get('dependencies');
if (isset($dependencies['config'])) {
foreach ($dependencies['config'] as $config_name) {
$this
->deleteConfig($config_name);
}
$message = $this
->t('Configuration "@config_name" and all its dependencies successfully deleted.', [
'@config_name' => $name,
]);
}
}
$this
->deleteConfig($name);
\Drupal::messenger()
->addStatus($message);
}
protected function deleteConfig($name) {
\Drupal::configFactory()
->getEditable($name)
->delete();
}
}