Same filename and directory in other branches
- 8.9.x core/modules/system/tests/modules/ajax_test/src/Form/AjaxTestDialogForm.php
- 9 core/modules/system/tests/modules/ajax_test/src/Form/AjaxTestDialogForm.php
Namespace
Drupal\ajax_test\Form
File
core/modules/system/tests/modules/ajax_test/src/Form/AjaxTestDialogForm.php
View source
<?php
namespace Drupal\ajax_test\Form;
use Drupal\ajax_test\Controller\AjaxTestController;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\OpenDialogCommand;
use Drupal\Core\Ajax\OpenModalDialogCommand;
use Drupal\Core\Ajax\OpenModalDialogWithUrl;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
class AjaxTestDialogForm extends FormBase {
public function getFormId() {
return 'ajax_test_dialog_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['button1'] = [
'#type' => 'submit',
'#name' => 'button1',
'#value' => 'Button 1 (modal)',
'#ajax' => [
'callback' => '::modal',
],
];
$form['button2'] = [
'#type' => 'submit',
'#name' => 'button2',
'#value' => 'Button 2 (non-modal)',
'#ajax' => [
'callback' => '::nonModal',
],
];
$form['button3'] = [
'#type' => 'submit',
'#name' => 'button3',
'#value' => 'Button 3 (modal from url)',
'#ajax' => [
'callback' => '::modalFromUrl',
],
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$form_state
->setRedirect('ajax_test.dialog_contents');
}
public function modal(&$form, FormStateInterface $form_state) {
return $this
->dialog(TRUE);
}
public function modalFromUrl(&$form, FormStateInterface $form_state) {
return $this
->dialog(TRUE, TRUE);
}
public function nonModal(&$form, FormStateInterface $form_state) {
return $this
->dialog(FALSE);
}
protected function dialog(bool $is_modal = FALSE, bool $is_url = FALSE) {
$content = AjaxTestController::dialogContents();
$response = new AjaxResponse();
$title = $this
->t('AJAX Dialog & contents');
$content['#attached']['library'][] = 'core/drupal.dialog.ajax';
if ($is_modal) {
if ($is_url) {
$response
->addCommand(new OpenModalDialogWithUrl(Url::fromRoute('ajax_test.dialog_form')
->toString(), []));
}
else {
$response
->addCommand(new OpenModalDialogCommand($title, $content));
}
}
else {
$selector = '#ajax-test-dialog-wrapper-1';
$response
->addCommand(new OpenDialogCommand($selector, $title, $content));
}
return $response;
}
}
Classes