devel_wizard-2.x-dev/templates/spell/entity_type/content/revision_delete_form.php.twig
templates/spell/entity_type/content/revision_delete_form.php.twig
{#
@todo
Dependency injection for
- ::$stringTranslation
- ::$messenger
#}
{%
include '@devel_wizard/php/devel_wizard.php.file.header.php.twig'
with {
'namespace': content.namespace,
}
%}
use {{ content.interface_fqn }};
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class RevisionDeleteForm extends ConfirmFormBase {
protected string $entityTypeId = '{{ content.id }}';
protected {{ content.interface }} $revision;
/**
* {@inheritdoc}
*
* @return static
*/
public static function create(ContainerInterface $container) {
// @phpstan-ignore-next-line
return new static(
$container->get('entity_type.manager'),
$container->get('date.formatter'),
$container->get('database'),
);
}
public function __construct(
protected EntityTypeManagerInterface $entityTypeManager,
protected DateFormatterInterface $dateFormatter,
protected Connection $connection,
) {
}
/**
* {@inheritdoc}
*/
public function getFormId(): string {
return "{$this->entityTypeId}_revision_delete_confirm";
}
/**
* {@inheritdoc}
*/
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()),
],
);
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
$entityTypeId = $this->revision->getEntityTypeId();
return new Url(
"entity.$entityTypeId.revision_history",
[
$entityTypeId => $this->revision->id(),
],
);
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Delete');
}
/**
* {@inheritdoc}
*
* @phpstan-param array<string, mixed> $form
*
* @phpstan-return array<string, mixed>
*/
public function buildForm(
array $form,
FormStateInterface $form_state,
?{{ content.interface }} ${{ content.id }}_revision = NULL,
): array {
/* @noinspection PhpUnhandledExceptionInspection */
/** @var \{{ content.namespace }}\StorageInterface $storage */
$storage = $this->entityTypeManager->getStorage($this->entityTypeId);
$this->revision = $storage->loadRevision(${{ content.id }}_revision);
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*
* @phpstan-param array<string, mixed> $form
*/
public function submitForm(array &$form, FormStateInterface $form_state): void {
/* @noinspection PhpUnhandledExceptionInspection */
/** @var \{{ content.namespace }}\StorageInterface $storage */
$storage = $this->entityTypeManager->getStorage($this->entityTypeId);
$revisionIdToDelete = $this->revision->getRevisionId();
if (!$revisionIdToDelete) {
// @todo Status message.
return;
}
$storage->deleteRevision((int) $revisionIdToDelete);
$args = $this->commonMessageArgs();
$this
->emitLogEntry($args)
->emitStatusMessage($args)
->setRedirect($form, $form_state);
}
/**
* @return array<string, string|\Stringable>
*/
protected function commonMessageArgs(): array {
$entityType = $this->revision->getEntityType();
$args = [
'%entityType.label' => $entityType->getLabel(),
'@entity.id' => (string) $this->revision->id(),
'%entity.label' => $this->revision->label(),
'@entity.revision.id' => (string) $this->revision->getRevisionId(),
'@entity.revision.date' => $this->dateFormatter->format($this->revision->getRevisionCreationTime()),
];
$bundle = $this->getBundle();
if ($bundle) {
$args += [
'@entity.bundle.id' => (string) $bundle->id(),
'%entity.bundle.label' => $bundle->label(),
];
}
return $args;
}
/**
* @param array<string, string|\Stringable> $args
*/
protected function emitStatusMessage(array $args): static {
$bundle = $this->getBundle();
$message = $bundle
? $this->t('Revision from @entity.revision.date of %entity.bundle.label %entity.label has been deleted.', $args)
: $this->t('Revision from @entity.revision.date of %entity.label has been deleted.', $args);
$this
->messenger()
->addStatus($message);
return $this;
}
/**
* @param array<string, string|\Stringable> $args
*/
protected function emitLogEntry(array $args): static {
$bundle = $this->getBundle();
if ($bundle) {
$this
->logger($this->entityTypeId)
->notice(
'%entityType.label - %entity.bundle.label - delete - %entity.label - revision @entity.revision.id',
$args,
);
return $this;
}
$this
->logger($this->entityTypeId)
->notice(
'%entityType.label - delete - %entity.label - revision @entity.revision.id',
$args,
);
return $this;
}
/**
* @phpstan-param array<string, mixed> $form
*/
protected function setRedirect(array &$form, FormStateInterface $formState): static {
$routeName = $this->getNumberOfRevisions() === 1 ?
"entity.{$this->entityTypeId}.canonical"
: "entity.{$this->entityTypeId}.revision_history";
$formState->setRedirect(
$routeName,
[
$this->entityTypeId => $this->revision->id(),
],
);
return $this;
}
protected function getBundle(): ?ConfigEntityInterface {
$entityType = $this->revision->getEntityType();
$bundleEntityTypeId = $entityType->getBundleEntityType();
if (!$bundleEntityTypeId) {
return NULL;
}
/* @noinspection PhpUnhandledExceptionInspection */
/** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $storage */
$storage = $this->entityTypeManager->getStorage($bundleEntityTypeId);
// @phpstan-ignore-next-line
return $storage->load($this->revision->bundle());
}
protected function getNumberOfRevisions(): int {
$idFieldName = (string) $this->revision->getEntityType()->getKey('id');
/* @noinspection PhpUnhandledExceptionInspection */
$result = $this
->entityTypeManager
->getStorage($this->entityTypeId)
->getQuery()
->accessCheck(FALSE)
->allRevisions()
->condition($idFieldName, $this->revision->id())
->execute();
return count($result);
}
}
