StyleswitcherStyleDeleteForm.php in Style Switcher 8.2
File
src/Form/StyleswitcherStyleDeleteForm.php
View source
<?php
namespace Drupal\styleswitcher\Form;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
class StyleswitcherStyleDeleteForm extends ConfirmFormBase {
protected $style;
public function getFormId() {
return 'styleswitcher_style_delete_form';
}
public function getQuestion() {
return $this
->t('Are you sure you want to delete the style %title?', [
'%title' => $this->style['label'],
]);
}
public function getCancelUrl() {
return Url::fromRoute('styleswitcher.admin');
}
public function getDescription() {
return $this
->t('The style %title will be permanently deleted.', [
'%title' => $this->style['label'],
]) . '<br />' . $this
->t('After this operation users who have chosen this style will see the default one instead.');
}
public function buildForm(array $form, FormStateInterface $form_state, array $style = NULL) {
$this->style = $style;
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$name = $this->style['name'];
$styles = styleswitcher_custom_styles();
if (isset($styles[$name]['path'])) {
unset($styles[$name]);
$this
->configFactory()
->getEditable('styleswitcher.custom_styles')
->set('styles', $styles)
->save();
$this
->messenger()
->addStatus($this
->t('The style %title has been deleted.', [
'%title' => $this->style['label'],
]));
}
else {
$this
->messenger()
->addWarning($this
->t('The blank style cannot be deleted.'));
}
$form_state
->setRedirect('styleswitcher.admin');
}
}