DeleteMultiple.php in Message UI 8
File
src/Form/DeleteMultiple.php
View source
<?php
namespace Drupal\message_ui\Form;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Drupal\Core\Url;
use Drupal\message\Entity\Message;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DeleteMultiple extends ConfirmFormBase {
protected $messages = [];
protected $tempStoreFactory;
protected $manager;
public function __construct(PrivateTempStoreFactory $temp_store_factory, EntityTypeManagerInterface $manager) {
$this->tempStoreFactory = $temp_store_factory;
$this->storage = $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([
$this,
'filterCallback',
], $this->messages),
];
$form = parent::buildForm($form, $form_state);
$form['actions']['cancel']['#href'] = $this
->getCancelRoute();
return $form;
}
private function filterCallback(Message $message) {
$params = [
'@id' => $message
->id(),
'@template' => $message
->getTemplate()
->label(),
];
return t('Delete message ID @id fo template @template', $params);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$query = \Drupal::entityQuery('message');
$result = $query
->condition('type', $form_state['values']['types'], 'IN')
->execute();
if (empty($result['message'])) {
$this
->messenger()
->addError(t('No messages were found according to the parameters you entered'));
return;
}
$chunks = array_chunk(array_keys($result['message']), 100);
$operations = [];
foreach ($chunks as $chunk) {
$operations[] = [
'message_delete_multiple',
[
$chunk,
],
];
}
$batch = [
'operations' => $operations,
'title' => t('deleting messages.'),
'init_message' => t('Starting to delete messages.'),
'progress_message' => t('Processed @current out of @total.'),
'error_message' => t('The batch operation has failed.'),
];
batch_set($batch);
batch_process($_GET['q']);
}
public function getCancelUrl() {
return new Url('message.messages');
}
}