You are here

DataPolicyRevisionDeleteForm.php in Data Policy 8

File

src/Form/DataPolicyRevisionDeleteForm.php
View source
<?php

namespace Drupal\data_policy\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\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a form for deleting a Data policy revision.
 *
 * @ingroup data_policy
 */
class DataPolicyRevisionDeleteForm extends ConfirmFormBase {

  /**
   * The Data policy revision.
   *
   * @var \Drupal\data_policy\Entity\DataPolicyInterface
   */
  protected $revision;

  /**
   * The Data policy storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $dataPolicyStorage;

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * The date formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected $dateFormatter;

  /**
   * Constructs a new DataPolicyRevisionDeleteForm.
   *
   * @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
   *   The entity storage.
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection.
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   *   The date formatter service.
   */
  public function __construct(EntityStorageInterface $entity_storage, Connection $connection, DateFormatterInterface $date_formatter) {
    $this->dataPolicyStorage = $entity_storage;
    $this->connection = $connection;
    $this->dateFormatter = $date_formatter;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_type.manager')
      ->getStorage('data_policy'), $container
      ->get('database'), $container
      ->get('date.formatter'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'data_policy_data_policy_revision_delete_confirm';
  }

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    return $this
      ->t('Are you sure you want to delete the data policy revision from %revision-date?', [
      '%revision-date' => $this->dateFormatter
        ->format($this->revision
        ->getRevisionCreationTime()),
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl() {
    return new Url('entity.data_policy.version_history', [
      'entity_id' => $this->revision
        ->id(),
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function getConfirmText() {
    return $this
      ->t('Delete');
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, $data_policy_revision = NULL) {
    $this->revision = $this->dataPolicyStorage
      ->loadRevision($data_policy_revision);
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->dataPolicyStorage
      ->deleteRevision($this->revision
      ->getRevisionId());
    $this
      ->logger('content')
      ->notice('Data policy: deleted revision %revision.', [
      '%revision' => $this->revision
        ->getRevisionId(),
    ]);
    $this
      ->messenger()
      ->addStatus($this
      ->t('Revision from %revision-date has been deleted.', [
      '%revision-date' => $this->dateFormatter
        ->format($this->revision
        ->getRevisionCreationTime()),
    ]));
    $form_state
      ->setRedirectUrl($this
      ->getCancelUrl());
  }

}

Classes

Namesort descending Description
DataPolicyRevisionDeleteForm Provides a form for deleting a Data policy revision.