trait YamlFormDialogTrait in YAML Form 8
Trait class form dialogs.
Hierarchy
- trait \Drupal\yamlform\YamlFormDialogTrait
3 files declare their use of YamlFormDialogTrait
- YamlFormHandlerFormBase.php in src/
Form/ YamlFormHandlerFormBase.php - YamlFormTemplatesSubmissionPreviewForm.php in modules/
yamlform_templates/ src/ YamlFormTemplatesSubmissionPreviewForm.php - YamlFormUiElementFormBase.php in modules/
yamlform_ui/ src/ Form/ YamlFormUiElementFormBase.php
File
- src/
YamlFormDialogTrait.php, line 15
Namespace
Drupal\yamlformView source
trait YamlFormDialogTrait {
/**
* Is the current request for an AJAX modal dialog.
*
* @return bool
* TRUE is the current request if for an AJAX modal dialog.
*/
protected function isModalDialog() {
$wrapper_format = $this
->getRequest()
->get(MainContentViewSubscriber::WRAPPER_FORMAT);
return in_array($wrapper_format, [
'drupal_ajax',
'drupal_modal',
]) ? TRUE : FALSE;
}
/**
* Add modal dialog support to a form.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return array
* The form with modal dialog support.
*/
protected function buildDialog(array &$form, FormStateInterface $form_state) {
if ($this
->isModalDialog()) {
$form['actions']['submit']['#ajax'] = [
'callback' => '::submitForm',
'event' => 'click',
];
$form['#attached']['library'][] = 'core/drupal.dialog.ajax';
$form['#prefix'] = '<div id="yamlform-dialog">';
$form['#suffix'] = '</div>';
}
return $form;
}
/**
* Display validation error messages in modal dialog.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return bool|\Drupal\Core\Ajax\AjaxResponse
* An AJAX response that display validation error messages.
*/
protected function validateDialog(array &$form, FormStateInterface $form_state) {
if ($this
->isModalDialog() && $form_state
->hasAnyErrors()) {
unset($form['#prefix'], $form['#suffix']);
$form['status_messages'] = [
'#type' => 'status_messages',
'#weight' => -1000,
];
$response = new AjaxResponse();
$response
->addCommand(new HtmlCommand('#yamlform-dialog', $form));
return $response;
}
return FALSE;
}
/**
* Handler dialog redirect after form is submitted.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param \Drupal\Core\Url $url
* Redirect URL.
*
* @return \Drupal\Core\Ajax\AjaxResponse|null
* An AJAX redirect response or null if redirection is being handled by the
* $form_state.
*/
protected function redirectForm(array &$form, FormStateInterface $form_state, Url $url) {
if ($this
->isModalDialog()) {
$response = new AjaxResponse();
$response
->addCommand(new RedirectCommand($url
->toString()));
return $response;
}
else {
$form_state
->setRedirectUrl($url);
return NULL;
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
YamlFormDialogTrait:: |
protected | function | Add modal dialog support to a form. | |
YamlFormDialogTrait:: |
protected | function | Is the current request for an AJAX modal dialog. | |
YamlFormDialogTrait:: |
protected | function | Handler dialog redirect after form is submitted. | |
YamlFormDialogTrait:: |
protected | function | Display validation error messages in modal dialog. |