UserRevisionDeleteForm.php in User Revision 8
File
src/Form/UserRevisionDeleteForm.php
View source
<?php
namespace Drupal\user_revision\Form;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class UserRevisionDeleteForm extends ConfirmFormBase {
protected $revision;
protected $userStorage;
public function __construct(EntityStorageInterface $user_storage) {
$this->userStorage = $user_storage;
}
public static function create(ContainerInterface $container) {
$entity_manager = $container
->get('entity.manager');
return new static($entity_manager
->getStorage('user'));
}
public function getFormId() {
return 'user_revision_delete_confirm';
}
public function getQuestion() {
return t('Are you sure you want to delete the revision from %revision-date?', array(
'%revision-date' => \Drupal::service('date.formatter')
->format($this->revision->revision_timestamp->value),
));
}
public function getCancelUrl() {
return new Url('entity.user.version_history', array(
'user' => $this->revision
->id(),
));
}
public function getConfirmText() {
return t('Delete');
}
public function buildForm(array $form, FormStateInterface $form_state, $user = NULL, $user_revision = NULL) {
$this->revision = $this->userStorage
->loadRevision($user_revision);
if ($this->revision
->id() != $user) {
throw new NotFoundHttpException();
}
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->userStorage
->deleteRevision($this->revision
->getRevisionId());
$this
->logger('user_revision')
->notice('user: deleted %name revision %revision.', array(
'%name' => $this->revision
->label(),
'%revision' => $this->revision
->getRevisionId(),
));
$this
->messenger()
->addStatus(t('Revision from %revision-date of user %name has been deleted.', array(
'%revision-date' => \Drupal::service('date.formatter')
->format($this->revision->revision_timestamp->value),
'%name' => $this->revision
->label(),
)));
$form_state
->setRedirect('entity.user.canonical', array(
'user' => $this->revision
->id(),
));
if (user_revision_count($this->revision) > 1) {
$form_state
->setRedirect('entity.user.version_history', array(
'user' => $this->revision
->id(),
));
}
}
}