View source
<?php
namespace Drupal\filebrowser\Form;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\node\Entity\Node;
use Drupal\Core\Ajax\AfterCommand;
use Drupal\Core\Ajax\AjaxResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\Ajax\AlertCommand;
use Drupal\Core\Ajax\RemoveCommand;
use Drupal\Core\Ajax\RedirectCommand;
class DeleteForm extends ConfirmFormBase {
protected $queryFid;
protected $node;
protected $common;
protected $validator;
protected $filebrowser;
protected $itemsToDelete;
protected $fileSystem;
public function __construct() {
$this->validator = \Drupal::service('filebrowser.validator');
$this->common = \Drupal::service('filebrowser.common');
$this->fileSystem = \Drupal::service('file_system');
$this->itemsToDelete = null;
}
public function getFormId() {
return 'filebrowser_delete_form';
}
public function buildForm(array $form, FormStateInterface $form_state, $nid = null, $query_fid = 0, $fids_str = null, $ajax = null) {
$this->node = Node::load($nid);
$this->queryFid = $query_fid;
$this->filebrowser = $this->node->filebrowser;
$fids = explode(',', $fids_str);
$folder_selected = false;
$files = $this->common
->nodeContentLoadMultiple($fids);
foreach ($files as $fid => $file) {
$file['type'] = unserialize($file['file_data'])->type;
$file['full_path'] = $this->validator
->encodingToFs($this->filebrowser->encoding, $this->validator
->getNodeRoot($this->filebrowser->folderPath . $file['path']));
$file['display_name'] = $this->validator
->safeBaseName($file['full_path']);
$this->itemsToDelete[$fid] = $file;
}
$list = '<ul>';
foreach ($this->itemsToDelete as $item) {
$list .= '<li>';
if ($item['type'] == 'dir') {
$folder_selected = true;
$list .= '<b>' . $item['display_name'] . '</b>';
}
else {
$list .= $item['display_name'];
}
$list .= '</li>';
}
$list .= '</ul>';
if ($ajax) {
$form['#attributes'] = [
'class' => [
'form-in-slide-down',
],
];
$form['close_button'] = $this->common
->closeButtonMarkup();
}
$form['items'] = [
'#type' => 'item',
'#title' => $this
->t('Items being deleted'),
'#markup' => $list,
];
if ($folder_selected) {
$form['confirmation'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Confirm deletion of selected <b>folders</b> and all of their content.'),
'#default_value' => false,
];
}
else {
$form['confirmation'] = [
'#type' => 'value',
'#value' => TRUE,
];
}
$form = parent::buildForm($form, $form_state);
$form['actions']['cancel']['#attributes']['class'][] = 'button btn btn-default';
if ($ajax) {
$form['actions']['submit']['#attributes']['class'][] = 'use-ajax-submit';
$this->ajax = true;
}
return $form;
}
public function getQuestion() {
return $this
->t('Are you sure you want to delete the following items?');
}
public function getCancelUrl() {
return $this->node
->toUrl();
}
public function getDescription() {
return $this
->t('this action can not be undone.');
}
public function getConfirmText() {
return $this
->t('Delete');
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($this->error) {
$response = new AjaxResponse();
$response
->addCommand(new RemoveCommand('#filebrowser-form-action-error'));
$response
->addCommand(new RemoveCommand('.form-in-slide-down'));
$response
->addCommand(new AfterCommand('#form-action-actions-wrapper', $form));
$response
->addCommand(new AlertCommand($this
->t('You must confirm deletion of selected folders.')));
$form_state
->setResponse($response);
}
else {
foreach ($this->itemsToDelete as $item) {
$data = unserialize($item['file_data']);
$success = $this->fileSystem
->deleteRecursive($data->uri);
if ($success) {
Cache::invalidateTags([
'filebrowser:node:' . $this->node
->id(),
]);
}
else {
\Drupal::messenger()
->addWarning($this
->t('Unable to delete @file', [
'@file' => $data->uri,
]));
}
}
$route = $this->common
->redirectRoute($this->queryFid, $this->node
->id());
if ($this->ajax) {
$response_url = Url::fromRoute($route['name'], $route['node'], $route['query']);
$response = new AjaxResponse();
$response
->addCommand(new RedirectCommand($response_url
->toString()));
$form_state
->setResponse($response);
}
else {
$form_state
->setRedirect($route['name'], $route['node'], $route['query']);
}
}
}
public function validateForm(array &$form, FormStateInterface $form_state) {
if (empty($form_state
->getValue('confirmation'))) {
$this->error = true;
}
parent::validateForm($form, $form_state);
}
}