MediaDeleteForm.php in Media Directories 2.x
File
modules/media_directories_ui/src/Form/MediaDeleteForm.php
View source
<?php
namespace Drupal\media_directories_ui\Form;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\CloseModalDialogCommand;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\media_directories_ui\Ajax\LoadDirectoryContent;
class MediaDeleteForm extends ConfirmFormBase {
protected $entities;
protected $formContext;
public function getQuestion() {
if (count($this->entities) > 1) {
return $this
->t('Do you want to delete @count items?', [
'@count' => count($this->entities),
]);
}
$first_entity = reset($this->entities);
return $this
->t('Do you want to delete @name?', [
'@name' => $first_entity
->getName(),
]);
}
public function getCancelUrl() {
}
public function getFormId() {
return 'media_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->entities = $this->formContext['media_items'];
$form['media_items'] = [
'#tree' => TRUE,
];
foreach ($this->entities as $entity) {
$form['media_items'][] = [
'#type' => 'hidden',
'#value' => $entity
->id(),
];
}
}
$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',
],
];
return $form;
}
public function submitModalAjax(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
foreach ($this->entities as $entity) {
$entity
->delete();
}
$response
->addCommand(new CloseModalDialogCommand());
$response
->addCommand(new LoadDirectoryContent());
return $response;
}
public function closeModalAjax() {
$response = new AjaxResponse();
$response
->addCommand(new CloseModalDialogCommand());
return $response;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
}