MediaRevisionDeleteForm.php in Media Revisions UI 8
File
src/Form/MediaRevisionDeleteForm.php
View source
<?php
namespace Drupal\media_revisions_ui\Form;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\RevisionableStorageInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MediaRevisionDeleteForm extends ConfirmFormBase {
protected $revision;
protected $mediaStorage;
protected $dateFormatter;
public function __construct(RevisionableStorageInterface $media_storage, DateFormatterInterface $date_formatter) {
$this->mediaStorage = $media_storage;
$this->dateFormatter = $date_formatter;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager')
->getStorage('media'), $container
->get('date.formatter'));
}
public function getFormId() {
return 'media_revision_delete_confirm';
}
public function getQuestion() {
return $this
->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.media.version_history', [
'media' => $this->revision
->id(),
]);
}
public function getConfirmText() {
return $this
->t('Delete');
}
public function buildForm(array $form, FormStateInterface $form_state, $media_revision = NULL) {
$this->revision = $this->mediaStorage
->loadRevision($media_revision);
$form = parent::buildForm($form, $form_state);
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->mediaStorage
->deleteRevision($this->revision
->getRevisionId());
$this
->logger('content')
->notice('@type: deleted %title revision %revision.', [
'@type' => $this->revision
->bundle(),
'%title' => $this->revision
->label(),
'%revision' => $this->revision
->getRevisionId(),
]);
$this
->messenger()
->addStatus($this
->t('Revision from %revision-date of %title has been deleted.', [
'%revision-date' => $this->dateFormatter
->format($this->revision
->getRevisionCreationTime()),
'%title' => $this->revision
->label(),
]));
$form_state
->setRedirect('entity.media.canonical', [
'media' => $this->revision
->id(),
]);
$revisionCount = $this->mediaStorage
->getQuery()
->allRevisions()
->condition('mid', $this->revision
->id())
->count()
->execute();
if ($revisionCount > 1) {
$form_state
->setRedirect('entity.media.version_history', [
'media' => $this->revision
->id(),
]);
}
}
}