bibcite-8.x-1.x-dev/modules/bibcite_entity/src/Form/DeleteMultiple.php
modules/bibcite_entity/src/Form/DeleteMultiple.php
<?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;
/**
* Delete multiple entities form.
*/
class DeleteMultiple extends ConfirmFormBase {
/**
* The array of entities to delete.
*
* @var array
*/
protected $entityInfo = [];
/**
* The tempstore object.
*
* @var \Drupal\Core\TempStore\PrivateTempStore
*/
protected $tempStore;
/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The current user object.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* The entity type identifier.
*
* @var string
*/
protected $entityTypeId;
/**
* Constructs a DeleteMultiple form object.
*
* @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory
* The tempstore factory.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $manager
* The entity manager.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user object.
*/
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;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('tempstore.private'),
$container->get('entity_type.manager'),
$container->get('current_user')
);
}
/**
* {@inheritdoc}
*/
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?');
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return new Url("entity.{$this->entityTypeId}.collection");
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'bibcite_entity_delete_multiple';
}
/**
* {@inheritdoc}
*/
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);
}
/**
* {@inheritdoc}
*/
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');
}
}
