FieldDeleteForm.php in Display Suite 8.3
File
src/Form/FieldDeleteForm.php
View source
<?php
namespace Drupal\ds\Form;
use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Form\ConfigFormBaseTrait;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
class FieldDeleteForm extends ConfirmFormBase implements ContainerInjectionInterface {
use ConfigFormBaseTrait;
protected $cacheInvalidator;
protected $field;
public function __construct(CacheTagsInvalidatorInterface $cache_invalidator) {
$this->cacheInvalidator = $cache_invalidator;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('cache_tags.invalidator'));
}
public function getQuestion() {
return $this
->t('Are you sure you want to delete @field ?', [
'@field' => $this->field['label'],
]);
}
public function getCancelUrl() {
return new Url('ds.fields_list');
}
public function getFormId() {
return 'field_delete_form';
}
public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL, $field_key = '') {
$config = $this
->config('ds.field.' . $field_key);
$this->field = $config
->get();
if (empty($this->field)) {
$this
->messenger()
->addMessage($this
->t('Field not found.'));
return new RedirectResponse('/admin/structure/ds/fields');
}
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$field = $this->field;
$this
->config('ds.field.' . $field['id'])
->delete();
$this->cacheInvalidator
->invalidateTags([
'ds_fields_info',
]);
\Drupal::service('plugin.manager.ds')
->clearCachedDefinitions();
$url = new Url('ds.fields_list');
$form_state
->setRedirectUrl($url);
$this
->messenger()
->addMessage($this
->t('The field @field has been deleted.', [
'@field' => $field['label'],
]));
}
protected function getEditableConfigNames() {
if ($this->field) {
return [
'ds.field.' . $this->field['id'],
];
}
return [];
}
}