ConfigEntityRevisionsDeleteFormBase.php in Config Entity Revisions 8
File
src/ConfigEntityRevisionsDeleteFormBase.php
View source
<?php
namespace Drupal\config_entity_revisions;
use Drupal\Core\Database\Connection;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Datetime\DateFormatter;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\ContentEntityStorageInterface;
use Drupal\config_entity_revisions\ConfigEntityRevisionsControllerInterface;
abstract class ConfigEntityRevisionsDeleteFormBase extends ConfirmFormBase implements ContainerInjectionInterface {
protected $config_entity;
protected $connection;
protected $dateFormatter;
protected $revision;
protected $configEntityRevisionsStorage;
protected $controller;
public function __construct(EntityStorageInterface $storage, Connection $connection, $dateFormatter, ConfigEntityRevisionsControllerInterface $controller) {
$this->connection = $connection;
$this->dateFormatter = $dateFormatter;
$match = \Drupal::service('router')
->matchRequest(\Drupal::request());
$this->config_entity = $match['config_entity']
->revisioned_entity();
$this->configEntityRevisionsStorage = $storage;
$this->revision = $storage
->loadRevision($match['revision_id']);
$this->controller = $controller;
}
public function getFormId() {
return $this->config_entity
->module_name() . '_revision_delete_confirm';
}
public function getQuestion() {
return t('Are you sure you want to delete the revision from %revision-date?', [
'%revision-date' => $this->dateFormatter
->format($this->revision
->getRevisionCreationTime()),
]);
}
public function getCancelUrl() {
return new Url("entity." . $this->config_entity
->getEntityTypeId() . ".revisions", [
$this->config_entity
->getEntityTypeId() => $this->config_entity
->id(),
]);
}
public function getConfirmText() {
return t('Delete');
}
public function getDescription() {
if ($this->config_entity
->has_own_content()) {
$count = $this->config_entity
->contentEntityCount($this->revision->revision->value);
if ($count) {
return \Drupal::service('string_translation')
->formatPlural($count, '1 submission will be deleted.', '@count submissions will be deleted.');
}
}
return '';
}
public function setRevisionsFormRedirect(FormStateInterface &$form_state) {
$form_state
->setRedirect('entity.' . $this->config_entity
->config_entity_name() . '.revisions', [
$this->config_entity
->config_entity_name() => $this->config_entity
->id(),
]);
}
public function performDeletion() {
$this->controller
->deleteRevision($this->revision);
}
public function logUpdate() {
$this
->logger('content')
->notice('Deleted %label revision %revision.', [
'%label' => $this->config_entity
->label(),
'%revision' => $this->revision
->getRevisionId(),
]);
}
public function displayUpdate() {
\Drupal::messenger()
->addWarning(t('Revision from %revision-date of %form has been deleted.', [
'%revision-date' => $this->dateFormatter
->format($this->revision
->getRevisionCreationTime()),
'%form' => $this->config_entity
->label(),
]));
}
public function setBuildFormRedirect(FormStateInterface &$form_state) {
$form_state
->setRedirect('entity.' . $this->config_entity
->config_entity_name() . '.edit_form', [
$this->config_entity
->config_entity_name() => $this->config_entity
->id(),
]);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$revisions = $this->controller
->getRevisionIds($this->config_entity
->getContentEntityID());
if (count($revisions) == 1) {
\Drupal::messenger()
->addError(t('There is only one revision remaining. You may not delete it, sorry.'));
}
else {
if ($this->config_entity
->has_own_content()) {
$this->config_entity
->deleteRelatedContentEntities($this->revision->revision->value);
}
$this->controller
->deleteRevision($this->revision);
$this
->logUpdate();
$this
->displayUpdate();
}
if (count($revisions) > 2) {
$this
->setRevisionsFormRedirect($form_state);
}
else {
$this
->setBuildFormRedirect($form_state);
}
}
}