View source
<?php
namespace Drupal\editor_note\Form;
use Drupal\editor_note\EditorNoteHelperService;
use Drupal\editor_note\Entity\EditorNote;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\CloseModalDialogCommand;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ConfirmDeleteEditorNoteForm extends FormBase {
protected $editorNote;
protected $editorNoteHelper;
public function __construct(EditorNoteHelperService $editorNoteHelper) {
$this->editorNoteHelper = $editorNoteHelper;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('editor_note.helper'));
}
public function getFormId() {
return 'confirm_delete_editor_note_form';
}
public function buildForm(array $form, FormStateInterface $form_state, EditorNote $editor_note = NULL, $nojs = NULL) {
$this->editorNote = $editor_note;
$form['use_ajax_container']['description'] = [
'#type' => 'item',
'#markup' => $this
->t('Are you sure you want to remove the note? This action cannot be undone.'),
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Delete'),
'#attributes' => [
'class' => [
'use-ajax',
],
],
'#ajax' => [
'callback' => [
$this,
'submitModalFormAjax',
],
'event' => 'click',
],
];
$form['cancel'] = [
'#type' => 'button',
'#value' => $this
->t('Cancel'),
'#attributes' => [
'class' => [
'use-ajax',
],
],
'#ajax' => [
'callback' => [
$this,
'closeModalForm',
],
'event' => 'click',
],
];
return $form;
}
public function submitModalFormAjax(array $form, FormStateInterface $form_state) {
$response = new AjaxResponse();
if ($this->editorNote) {
$replace_command = $this->editorNoteHelper
->getWidgetAjaxReplaceCommand($this->editorNote);
$response
->addCommand($replace_command);
}
$command = new CloseModalDialogCommand();
$response
->addCommand($command);
return $response;
}
public function closeModalForm(array $form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$command = new CloseModalDialogCommand();
$response
->addCommand($command);
return $response;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($this->editorNote) {
$this->editorNote
->delete();
}
}
}