Namespace
Drupal\form_api_example\Form
File
-
modules/form_api_example/src/Form/ModalForm.php
View source
<?php
namespace Drupal\form_api_example\Form;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\OpenModalDialogCommand;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ModalForm extends FormBase {
public static function create(ContainerInterface $container) {
$form = new static();
$form->setRequestStack($container->get('request_stack'));
$form->setStringTranslation($container->get('string_translation'));
$form->setMessenger($container->get('messenger'));
return $form;
}
public function getFormId() {
return 'form_api_example_modal_form';
}
protected static function getDataDialogOptions() {
return [
'width' => '50%',
];
}
public function buildForm(array $form, FormStateInterface $form_state, $nojs = NULL) {
$form['#attached']['library'][] = 'core/drupal.ajax';
$form['description'] = [
'#type' => 'item',
'#markup' => $this->t('This example demonstrates a form that can work as a normal multi-request form, or as a modal dialog using 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('See this form as a modal.'),
'#url' => Url::fromRoute('form_api_example.modal_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,
];
}
$form['title'] = [
'#type' => 'textfield',
'#title' => $this->t('Title'),
'#required' => TRUE,
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Submit'),
'#ajax' => [
'callback' => '::ajaxSubmitForm',
'event' => 'click',
],
];
if ($nojs == 'nojs') {
unset($form['actions']['submit']['#ajax']);
}
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$title = $form_state->getValue('title');
$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();
$title = new FormattableMarkup(':title', [
':title' => $form_state->getValue('title'),
]);
$content = [
'#type' => 'item',
'#markup' => $this->t("Your specified title of '%title' appears in this modal dialog.", [
'%title' => $title,
]),
];
$response->addCommand(new OpenModalDialogCommand($title, $content, static::getDataDialogOptions()));
}
return $response;
}
}
Classes
| Title |
Deprecated |
Summary |
| ModalForm |
|
Implements the ModalForm form controller. |