Skip to content

Instantly share code, notes, and snippets.

@larowlan
Created April 15, 2015 00:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larowlan/0e55ccee31be29d77639 to your computer and use it in GitHub Desktop.
Save larowlan/0e55ccee31be29d77639 to your computer and use it in GitHub Desktop.
<?php
/**
* @file
* Contains Drupal\modal_test\Controller\ModalForm.
*/
namespace Drupal\modal_test\Controller;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\CloseModalDialogCommand;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
class ModalForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'modal_test_context_configure';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $context = NULL) {
$form['context'] = [
'#type' => 'value',
'#value' => $context
];
$form['label'] = [
'#type' => 'textfield',
'#title' => $this->t('Label'),
];
$form['description'] = [
'#type' => 'textarea',
'#title' => $this->t('Description'),
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Save'),
'#ajax' => [
'callback' => [$this, 'ajaxSubmit'],
'event' => 'click',
],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Do stuff if JavaScript is disabled.
}
/**
* {@inheritdoc}
*/
public function ajaxSubmit(array &$form, FormStateInterface $form_state) {
// Do stuff to save via JavaScript.
$response = new AjaxResponse();
$response->addCommand(new CloseModalDialogCommand());
return $response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment