DataPolicyRevisionDeleteForm.php in Data Policy 8
File
src/Form/DataPolicyRevisionDeleteForm.php
View source
<?php
namespace Drupal\data_policy\Form;
use Drupal\Core\Database\Connection;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DataPolicyRevisionDeleteForm extends ConfirmFormBase {
protected $revision;
protected $dataPolicyStorage;
protected $connection;
protected $dateFormatter;
public function __construct(EntityStorageInterface $entity_storage, Connection $connection, DateFormatterInterface $date_formatter) {
$this->dataPolicyStorage = $entity_storage;
$this->connection = $connection;
$this->dateFormatter = $date_formatter;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager')
->getStorage('data_policy'), $container
->get('database'), $container
->get('date.formatter'));
}
public function getFormId() {
return 'data_policy_data_policy_revision_delete_confirm';
}
public function getQuestion() {
return $this
->t('Are you sure you want to delete the data policy revision from %revision-date?', [
'%revision-date' => $this->dateFormatter
->format($this->revision
->getRevisionCreationTime()),
]);
}
public function getCancelUrl() {
return new Url('entity.data_policy.version_history', [
'entity_id' => $this->revision
->id(),
]);
}
public function getConfirmText() {
return $this
->t('Delete');
}
public function buildForm(array $form, FormStateInterface $form_state, $data_policy_revision = NULL) {
$this->revision = $this->dataPolicyStorage
->loadRevision($data_policy_revision);
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->dataPolicyStorage
->deleteRevision($this->revision
->getRevisionId());
$this
->logger('content')
->notice('Data policy: deleted revision %revision.', [
'%revision' => $this->revision
->getRevisionId(),
]);
$this
->messenger()
->addStatus($this
->t('Revision from %revision-date has been deleted.', [
'%revision-date' => $this->dateFormatter
->format($this->revision
->getRevisionCreationTime()),
]));
$form_state
->setRedirectUrl($this
->getCancelUrl());
}
}