You are here

DeleteMultiple.php in Message 8

File

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

namespace Drupal\message\Form;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\message\Entity\Message;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a message deletion confirmation form.
 */
class DeleteMultiple extends ConfirmFormBase {

  /**
   * The array of messages to delete.
   *
   * @var array
   */
  protected $messages = [];

  /**
   * The tempstore factory.
   *
   * @var \Drupal\Core\TempStore\PrivateTempStoreFactory
   */
  protected $tempStoreFactory;

  /**
   * Constructs a DeleteMultiple form object.
   *
   * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory
   *   The tempstore factory.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(PrivateTempStoreFactory $temp_store_factory, EntityTypeManagerInterface $entity_type_manager) {
    $this->tempStoreFactory = $temp_store_factory;
    $this->storage = $entity_type_manager
      ->getStorage('message');
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('tempstore.private'), $container
      ->get('entity_type.manager'));
  }

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

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    return \Drupal::translation()
      ->formatPlural(count($this->messages), 'Are you sure you want to delete this item?', 'Are you sure you want to delete these items?');
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelRoute() {
  }

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $this->messages = $this->tempStoreFactory
      ->get('message_multiple_delete_confirm')
      ->get(\Drupal::currentUser()
      ->id());
    if (empty($this->messages)) {
      return new RedirectResponse($this
        ->getCancelUrl()
        ->setAbsolute()
        ->toString());
    }
    $form['messages'] = [
      '#theme' => 'item_list',
      '#items' => array_map(function (Message $message) {
        $params = [
          '@id' => $message
            ->id(),
          '@template' => $message
            ->getTemplate()
            ->label(),
        ];
        return t('Delete message ID @id for template @template', $params);
      }, $this->messages),
    ];
    $form = parent::buildForm($form, $form_state);
    $form['actions']['cancel']['#href'] = $this
      ->getCancelRoute();
    $form['actions']['submit']['#submit'] = [
      '::submitForm',
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    if ($form_state
      ->getValue('confirm') && !empty($this->messages)) {
      $this->storage
        ->delete($this->messages);
      $this->tempStoreFactory
        ->get('message_multiple_delete_confirm')
        ->delete(\Drupal::currentUser()
        ->id());
      $count = count($this->messages);
      $this
        ->logger('message')
        ->notice('Deleted @count messages.', [
        '@count' => $count,
      ]);
      $this
        ->messenger()
        ->addMessage(\Drupal::translation()
        ->formatPlural($count, 'Deleted 1 message.', 'Deleted @count messages.'));
    }
    $form_state
      ->setRedirect('message.messages');
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl() {
    return new Url('message.messages');
  }

}

Classes

Namesort descending Description
DeleteMultiple Provides a message deletion confirmation form.