DirectoryDeleteForm.php in Media Directories 2.x
File
modules/media_directories_ui/src/Form/DirectoryDeleteForm.php
View source
<?php
namespace Drupal\media_directories_ui\Form;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\CloseModalDialogCommand;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\media_directories_ui\Ajax\RefreshDirectoryTree;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DirectoryDeleteForm extends ConfirmFormBase {
protected $directory;
protected $formContext;
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entityTypeManager) {
$this->entityTypeManager = $entityTypeManager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'));
}
public function getQuestion() {
return $this
->t('Do you want to delete @name?', [
'@name' => $this->directory
->getName(),
]);
}
public function getCancelUrl() {
}
public function getFormId() {
return 'directory_delete_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$build_info = $form_state
->getBuildInfo();
if (isset($build_info['args'][0])) {
$this->formContext = $build_info['args'][0];
$this->directory = $this->formContext['directory'];
$form['directory_id'] = [
'#type' => 'hidden',
'#value' => $this->directory
->id(),
];
}
if ($this->directory
->access('delete')) {
$form['question']['#markup'] = '<h4>' . $this
->getQuestion() . '</h4>';
$form = parent::buildForm($form, $form_state);
$form['#attached']['library'][] = 'core/drupal.dialog.ajax';
$form['actions']['submit']['#ajax'] = [
'callback' => [
$this,
'submitModalAjax',
],
'event' => 'click',
];
$form['actions']['cancel'] = [
'#type' => 'button',
'#value' => $this
->t('Cancel'),
'#button_type' => 'secondary',
'#ajax' => [
'callback' => [
$this,
'closeModalAjax',
],
'event' => 'click',
],
];
}
else {
$form['permission_info']['#markup'] = $this
->t('No permission found to delete this directory.');
}
return $form;
}
public function submitModalAjax(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
if ($this->directory
->access('delete')) {
$query = $this->entityTypeManager
->getStorage('media')
->getQuery();
$query
->condition('directory', $this->directory
->id());
$media_ids = $query
->execute();
if (!empty($media_ids)) {
$media_items = $this->entityTypeManager
->getStorage('media')
->loadMultiple($media_ids);
foreach ($media_items as $media_item) {
$media_item
->get('directory')
->setValue(NULL);
$media_item
->save();
}
}
$parent_id = (int) $this->directory
->get('parent')->target_id;
if ($parent_id === 0) {
$parent_id = -1;
}
$this->directory
->delete();
$response
->addCommand(new CloseModalDialogCommand());
$response
->addCommand(new RefreshDirectoryTree($parent_id));
}
return $response;
}
public function closeModalAjax() {
$response = new AjaxResponse();
$response
->addCommand(new CloseModalDialogCommand());
return $response;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
}