FileDeleteMultipleForm.php in File Entity (fieldable files) 8.2
File
src/Form/FileDeleteMultipleForm.php
View source
<?php
namespace Drupal\file_entity\Form;
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\FileInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FileDeleteMultipleForm extends ConfirmFormBase {
protected $files = array();
protected $tempStore;
protected $storage;
public function __construct(PrivateTempStoreFactory $temp_store_factory, EntityTypeManagerInterface $entity_type_manager) {
$this->tempStore = $temp_store_factory
->get('file_multiple_delete_confirm');
$this->storage = $entity_type_manager
->getStorage('file');
}
public static function create(ContainerInterface $container) {
return new static($container
->get('tempstore.private'), $container
->get('entity_type.manager'));
}
public function getFormId() {
return 'file_multiple_delete_confirm';
}
public function getQuestion() {
return \Drupal::translation()
->formatPlural(count($this->files), 'Are you sure you want to delete this file?', 'Are you sure you want to delete these files?');
}
public function getCancelUrl() {
return new Url('entity.file.collection');
}
public function getConfirmText() {
return t('Delete');
}
public function buildForm(array $form, FormStateInterface $form_state) {
$this->files = $this->tempStore
->get('delete');
if (empty($this->files)) {
$form_state
->setRedirect('entity.file.collection');
}
$form['files'] = array(
'#theme' => 'item_list',
'#items' => array_map(function (FileInterface $file) {
return Html::escape($file
->label());
}, $this->files),
);
$form = parent::buildForm($form, $form_state);
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($form_state
->getValue('confirm') && !empty($this->files)) {
$this->storage
->delete($this->files);
$this->tempStore
->delete('delete');
}
$form_state
->setRedirect('entity.file.collection');
}
}