Skip to content

Instantly share code, notes, and snippets.

@rpayanm
Created July 12, 2019 20:25
Show Gist options
  • Save rpayanm/58bcd421751a92b80a402fa64a15a084 to your computer and use it in GitHub Desktop.
Save rpayanm/58bcd421751a92b80a402fa64a15a084 to your computer and use it in GitHub Desktop.
<?php
namespace Drupal\modal_form_example\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\OpenModalDialogCommand;
use Drupal\Core\Ajax\ReplaceCommand;
/**
* ModalForm class.
*/
class ModalForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'modal_form_example_modal_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $options = NULL) {
$form['#prefix'] = '<div id="modal_example_form">';
$form['#suffix'] = '</div>';
// The status messages that will contain any form errors.
$form['status_messages'] = [
'#type' => 'status_messages',
'#weight' => -10,
];
// A required checkbox field.
$form['our_checkbox'] = [
'#type' => 'checkbox',
'#title' => $this->t('I Agree: modal forms are awesome!'),
'#required' => TRUE,
];
$form['actions'] = array('#type' => 'actions');
$form['actions']['send'] = [
'#type' => 'submit',
'#value' => $this->t('Submit modal form'),
'#attributes' => [
'class' => [
'use-ajax',
],
],
'#ajax' => [
'callback' => [$this, 'submitModalFormAjax'],
'event' => 'click',
],
];
$form['#attached']['library'][] = 'core/drupal.dialog.ajax';
return $form;
}
/**
* AJAX callback handler that displays any errors or a success message.
*/
public function submitModalFormAjax(array $form, FormStateInterface $form_state) {
$response = new AjaxResponse();
// If there are any form errors, re-display the form.
if ($form_state->hasAnyErrors()) {
$response->addCommand(new ReplaceCommand('#modal_example_form', $form));
}
else {
$response->addCommand(new OpenModalDialogCommand("Success!", 'The modal form has been submitted.', ['width' => 800]));
}
return $response;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {}
/**
* Gets the configuration names that will be editable.
*
* @return array
* An array of configuration object names that are editable if called in
* conjunction with the trait's config() method.
*/
protected function getEditableConfigNames() {
return ['config.modal_form_example_modal_form'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment