DeleteMultiple.php in Bibliography & Citation 2.0.x
File
modules/bibcite_entity/src/Form/DeleteMultiple.php
View source
<?php
namespace Drupal\bibcite_entity\Form;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
class DeleteMultiple extends ConfirmFormBase {
protected $entityInfo = [];
protected $tempStore;
protected $entityTypeManager;
protected $currentUser;
protected $entityTypeId;
public function __construct(PrivateTempStoreFactory $temp_store_factory, EntityTypeManagerInterface $manager, AccountInterface $current_user) {
$this->tempStore = $temp_store_factory
->get('bibcite_entity_multiple_delete');
$this->entityTypeManager = $manager;
$this->currentUser = $current_user;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('tempstore.private'), $container
->get('entity_type.manager'), $container
->get('current_user'));
}
public function getQuestion() {
return $this
->formatPlural(count($this->entityInfo), 'Are you sure you want to delete this item?', 'Are you sure you want to delete these items?');
}
public function getCancelUrl() {
return new Url("entity.{$this->entityTypeId}.collection");
}
public function getFormId() {
return 'bibcite_entity_delete_multiple';
}
public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL) {
$this->entityTypeId = $entity_type_id;
$this->entityInfo = $this->tempStore
->get($this->currentUser
->id());
if (empty($this->entityInfo)) {
return new RedirectResponse($this
->getCancelUrl()
->setAbsolute()
->toString());
}
$form['entities'] = [
'#theme' => 'item_list',
'#items' => $this->entityInfo,
];
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($form_state
->getValue('confirm') && !empty($this->entityInfo)) {
$storage = $this->entityTypeManager
->getStorage($this->entityTypeId);
$entities = $storage
->loadMultiple(array_keys($this->entityInfo));
$storage
->delete($entities);
$this
->logger('bibcite')
->notice('Deleted @count references.', [
'@count' => count($entities),
]);
$this
->messenger()
->addStatus($this
->formatPlural(count($entities), 'Deleted 1 reference.', 'Deleted @count references.'));
$this->tempStore
->delete($this->currentUser
->id());
}
$form_state
->setRedirect('entity.bibcite_reference.collection');
}
}