UserRevisionRevertForm.php in User Revision 8
File
src/Form/UserRevisionRevertForm.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 UserRevisionRevertForm extends ConfirmFormBase {
protected $revision;
protected $userStorage;
public function __construct(EntityStorageInterface $user_storage) {
$this->userStorage = $user_storage;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.manager')
->getStorage('user'));
}
public function getFormId() {
return 'user_revision_revert_confirm';
}
public function getQuestion() {
return t('Are you sure you want to revert to 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('Revert');
}
public function getDescription() {
return '';
}
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->revision
->setNewRevision();
$this->revision
->isDefaultRevision(TRUE);
$original_revision_timestamp = $this->revision->revision_timestamp->value;
$this->revision->revision_log = t('Copy of the revision from %date.', array(
'%date' => \Drupal::service('date.formatter')
->format($original_revision_timestamp),
));
$this->revision
->save();
$this
->logger('user_revision')
->notice('user: reverted %name revision %revision.', array(
'%name' => $this->revision
->label(),
'%revision' => $this->revision
->getRevisionId(),
));
$this
->messenger()
->addStatus(t('User %name has been reverted back to the revision from %revision-date.', array(
'%name' => $this->revision
->label(),
'%revision-date' => \Drupal::service('date.formatter')
->format($original_revision_timestamp),
)));
$form_state
->setRedirect('entity.user.version_history', array(
'user' => $this->revision
->id(),
));
}
}