View source
<?php
namespace Drupal\editor_note\Form;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\OpenModalDialogCommand;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Url;
use Drupal\editor_note\EditorNoteHelperService;
use Drupal\editor_note\Entity\EditorNote;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ModalForm extends FormBase {
protected $messenger;
protected $editorNoteHelper;
public function __construct(MessengerInterface $messenger, EditorNoteHelperService $editorNoteHelper) {
$this->messenger = $messenger;
$this->editorNoteHelper = $editorNoteHelper;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('messenger'), $container
->get('editor_note.helper'));
}
public function getFormId() {
return 'modal_form';
}
protected static function getDataDialogOptions() {
return [
'width' => '50%',
];
}
protected function getEditorNoteEntity(FormStateInterface $form_state) {
$editor_note = NULL;
$build_info = $form_state
->getBuildInfo();
if (!empty($build_info['args'][0])) {
if ($build_info['args'][0] instanceof EditorNote) {
$editor_note = $build_info['args'][0];
}
}
return $editor_note;
}
public function buildForm(array $form, FormStateInterface $form_state, EditorNote $editor_note = NULL, $nojs = NULL) {
$form['#attached']['library'][] = 'core/drupal.ajax';
if ($nojs == 'nojs') {
$form['use_ajax_container'] = [
'#type' => 'details',
'#open' => TRUE,
];
$form['use_ajax_container']['description'] = [
'#type' => 'item',
'#markup' => $this
->t('In order to show a modal dialog by clicking on a link, that link has to have class <code>use-ajax</code> and <code>data-dialog-type="modal"</code>. This link has those attributes.'),
];
$form['use_ajax_container']['use_ajax'] = [
'#type' => 'link',
'#title' => $this
->t('Edit in modal.'),
'#url' => Url::fromRoute('editor_note.modal_form', [
'editor_note' => $editor_note
->id(),
'nojs' => 'ajax',
]),
'#attributes' => [
'class' => [
'use-ajax',
],
'data-dialog-type' => 'modal',
'data-dialog-options' => json_encode(static::getDataDialogOptions()),
'id' => 'ajax-example-modal-link',
],
];
$form['use_ajax_container']['delete'] = [
'#type' => 'link',
'#title' => $this
->t('Delete in modal.'),
'#url' => Url::fromRoute('editor_note.confirm_delete_editor_note_form', [
'nojs' => 'ajax',
]),
'#attributes' => [
'class' => [
'use-ajax',
],
'data-dialog-type' => 'modal',
'data-dialog-options' => json_encode(static::getDataDialogOptions()),
'id' => 'ajax-example-modal-link',
],
];
}
if ($nojs == 'ajax') {
$form['status_messages'] = [
'#type' => 'status_messages',
'#weight' => -999,
];
}
$note_value = $editor_note->note->value;
$user_input = $form_state
->getUserInput();
if (!empty($user_input['editor_note'])) {
$note_input = $user_input['editor_note'];
if (isset($note_input['value'])) {
$note_value = $note_input['value'];
}
}
$form['editor_note'] = [
'#type' => 'textarea',
'#default_value' => $note_value,
'#required' => TRUE,
'#title' => $this
->t('Update note'),
'#weight' => '0',
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Update'),
'#ajax' => [
'callback' => '::ajaxSubmitForm',
'event' => 'click',
],
];
if ($nojs == 'nojs') {
unset($form['actions']['submit']['#ajax']);
}
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$input = $form_state
->getValue('editor_note');
if (empty($input)) {
$form_state
->setError($form['editor_note'], $this
->t('Note text is empty.'));
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$title = $form_state
->getValue('title');
if ($editor_note = $this
->getEditorNoteEntity($form_state)) {
$note = $form_state
->getValue('editor_note');
$new_note = [
'value' => Xss::filterAdmin($note),
'format' => 'plain_text',
];
$editor_note
->set('note', $new_note);
$editor_note
->save();
}
$this->messenger
->addMessage($this
->t('Submit handler: You specified a title of @title.', [
'@title' => $title,
]));
}
public function ajaxSubmitForm(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
if ($form_state
->getErrors()) {
$form['status_messages'] = [
'#type' => 'status_messages',
'#weight' => -10,
];
$response
->addCommand(new OpenModalDialogCommand($this
->t('Errors'), $form, static::getDataDialogOptions()));
}
else {
$this->messenger
->deleteAll();
$editor_note = $this
->getEditorNoteEntity($form_state);
$content = [
'#type' => 'item',
'#markup' => $this
->t('Note has been updated and saved.'),
];
$response
->addCommand(new OpenModalDialogCommand($this
->t('Update selected item'), $content, static::getDataDialogOptions()));
$replace_command = $this->editorNoteHelper
->getWidgetAjaxReplaceCommand($editor_note);
$response
->addCommand($replace_command);
}
return $response;
}
}