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;
class DeleteMultiple extends ConfirmFormBase {
protected $messages = [];
protected $tempStoreFactory;
public function __construct(PrivateTempStoreFactory $temp_store_factory, EntityTypeManagerInterface $entity_type_manager) {
$this->tempStoreFactory = $temp_store_factory;
$this->storage = $entity_type_manager
->getStorage('message');
}
public static function create(ContainerInterface $container) {
return new static($container
->get('tempstore.private'), $container
->get('entity_type.manager'));
}
public function getFormId() {
return 'message_multiple_delete_confirm';
}
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?');
}
public function getCancelRoute() {
}
public function getConfirmText() {
return t('Delete');
}
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;
}
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');
}
public function getCancelUrl() {
return new Url('message.messages');
}
}