ConfigDeleteForm.php in Devel 4.x
File
src/Form/ConfigDeleteForm.php
View source
<?php
namespace Drupal\devel\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\ConfirmFormInterface;
use Drupal\Core\Form\ConfirmFormHelper;
use Drupal\Core\Url;
class ConfigDeleteForm extends FormBase implements ConfirmFormInterface {
public function getFormId() {
return 'devel_config_system_delete_form';
}
public function buildForm(array $form, FormStateInterface $form_state, $config_name = '') {
$config = $this
->config($config_name);
if ($config === FALSE || $config
->isNew()) {
$this
->messenger()
->addError(t('Config @name does not exist in the system.', array(
'@name' => $config_name,
)));
return;
}
$form['#title'] = $this
->getQuestion();
$form['#attributes']['class'][] = 'confirmation';
$form['description'] = array(
'#markup' => $this
->getDescription(),
);
$form[$this
->getFormName()] = array(
'#type' => 'hidden',
'#value' => 1,
);
if (!isset($form['#theme'])) {
$form['#theme'] = 'confirm_form';
}
$form['name'] = array(
'#type' => 'value',
'#value' => $config_name,
);
$form['actions'] = array(
'#type' => 'actions',
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => $this
->getConfirmText(),
'#submit' => array(
array(
$this,
'submitForm',
),
),
);
$form['actions']['cancel'] = ConfirmFormHelper::buildCancelLink($this, $this
->getRequest());
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config_name = $form_state
->getValue('name');
try {
$this
->configFactory()
->getEditable($config_name)
->delete();
$this
->messenger()
->addStatus($this
->t('Configuration variable %variable was successfully deleted.', array(
'%variable' => $config_name,
)));
$this
->logger('devel')
->info('Configuration variable %variable was successfully deleted.', array(
'%variable' => $config_name,
));
$form_state
->setRedirectUrl($this
->getCancelUrl());
} catch (\Exception $e) {
$this
->messenger()
->addError($e
->getMessage());
$this
->logger('devel')
->error('Error deleting configuration variable %variable : %error.', array(
'%variable' => $config_name,
'%error' => $e
->getMessage(),
));
}
}
public function getCancelUrl() {
return Url::fromRoute('devel.configs_list');
}
public function getQuestion() {
return $this
->t('Are you sure you want to delete this configuration?');
}
public function getDescription() {
return $this
->t('This action cannot be undone.');
}
public function getConfirmText() {
return $this
->t('Confirm');
}
public function getCancelText() {
return $this
->t('Cancel');
}
public function getFormName() {
return 'confirm';
}
}