marketo_suite-1.0.x-dev/src/Entity/Form/MarketoFormEntityRevisionDeleteForm.php
src/Entity/Form/MarketoFormEntityRevisionDeleteForm.php
<?php
namespace Drupal\e3_marketo\Entity\Form;
use Drupal\Core\Database\Connection;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\e3_marketo\Entity\MarketoFormEntityInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a form for deleting a Marketo form entity revision.
*
* @ingroup e3_marketo
*/
class MarketoFormEntityRevisionDeleteForm extends ConfirmFormBase {
/**
* The Marketo form entity revision.
*
* @var \Drupal\e3_marketo\Entity\MarketoFormEntityInterface|null
*/
protected ?MarketoFormEntityInterface $revision;
/**
* Constructs a new MarketoFormEntityRevisionDeleteForm.
*
* @param \Drupal\Core\Entity\EntityStorageInterface $marketoFormEntityStorage
* The entity storage.
* @param \Drupal\Core\Database\Connection $connection
* The database connection.
* @param \Drupal\Core\Datetime\DateFormatterInterface $dateFormatter
* The date formatter.
*/
public function __construct(
protected EntityStorageInterface $marketoFormEntityStorage,
protected Connection $connection,
protected DateFormatterInterface $dateFormatter
) {
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) : static {
return new static(
$container->get('entity_type.manager')->getStorage('marketo_form'),
$container->get('database'),
$container->get('date.formatter')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() : string {
return 'marketo_form_revision_delete_confirm';
}
/**
* {@inheritdoc}
*/
public function getQuestion() : TranslatableMarkup {
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() : Url {
return new Url('entity.marketo_form.version_history', ['marketo_form' => $this->revision->id()]);
}
/**
* {@inheritdoc}
*/
public function getConfirmText() : TranslatableMarkup {
return $this->t('Delete');
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $marketo_form_revision = NULL) : array {
/** @var \Drupal\e3_marketo\Entity\MarketoFormEntityInterface $revision */
$revision = $this->marketoFormEntityStorage->loadRevision($marketo_form_revision);
$this->revision = $revision;
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
/** @var \Drupal\e3_marketo\Entity\MarketoFormEntityInterface $revision */
$revision = $this->revision;
$this->marketoFormEntityStorage->deleteRevision($revision->getRevisionId());
$this->logger('content')->notice('Marketo form entity: deleted %title revision %revision.', [
'%title' => $revision->label(),
'%revision' => $revision->getRevisionId(),
]);
$this->messenger()->addStatus($this->t('Revision from %revision-date of Marketo form entity %title has been deleted.', [
'%revision-date' => $this->dateFormatter->format($revision->getRevisionCreationTime()),
'%title' => $revision->label(),
]));
$form_state->setRedirect(
'entity.marketo_form.edit_form',
['marketo_form' => $revision->id()]
);
$count_query = $this->connection->select('marketo_form_field_revision', 'mfr')
->fields('mfr', ['vid'])
->condition('mfr.id', $revision->id())
->distinct()
->countQuery()
->execute()
->fetchField();
if ($count_query > 1) {
$form_state->setRedirect(
'entity.marketo_form.version_history',
['marketo_form' => $revision->id()]
);
}
}
}
