trait WebformEntityAjaxFormTrait in Webform 6.x
Same name and namespace in other branches
- 8.5 src/Form/WebformEntityAjaxFormTrait.php \Drupal\webform\Form\WebformEntityAjaxFormTrait
Trait for webform entity ajax support.
@todo Issue #2785047: In Outside In mode, messages should appear in the off-canvas tray, not the main page.
Hierarchy
- trait \Drupal\webform\Form\WebformEntityAjaxFormTrait uses WebformAjaxFormTrait
See also
https://www.drupal.org/node/2785047
4 files declare their use of WebformEntityAjaxFormTrait
- WebformDevelEntitySchemaForm.php in modules/
webform_devel/ src/ Form/ WebformDevelEntitySchemaForm.php - WebformEntityHandlersForm.php in src/
WebformEntityHandlersForm.php - WebformEntityVariantsForm.php in src/
WebformEntityVariantsForm.php - WebformUiEntityElementsForm.php in modules/
webform_ui/ src/ WebformUiEntityElementsForm.php
File
- src/
Form/ WebformEntityAjaxFormTrait.php, line 15
Namespace
Drupal\webform\FormView source
trait WebformEntityAjaxFormTrait {
use WebformAjaxFormTrait;
/**
* {@inheritdoc}
*/
protected function isAjax() {
return TRUE;
}
/**
* Determine if dialogs are disabled.
*
* @return bool
* TRUE if dialogs are disabled.
*/
protected function isDialogDisabled() {
return \Drupal::config('webform.settings')
->get('ui.dialog_disabled');
}
/**
* Replace form via an Ajax response.
*
* @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 \Drupal\Core\Ajax\AjaxResponse
* An Ajax response that replaces a form.
*/
protected function replaceForm(array $form, FormStateInterface $form_state) {
// Display messages first by prefixing it the form and setting its weight
// to -1000.
$form = [
'status_messages' => [
'#type' => 'status_messages',
'#weight' => -1000,
],
] + $form;
// Remove wrapper.
unset($form['#prefix'], $form['#suffix']);
$response = $this
->createAjaxResponse($form, $form_state);
$response
->addCommand(new WebformHtmlCommand('#' . $this
->getWrapperId(), $form));
return $response;
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
if ($this
->isDialogDisabled()) {
return $form;
}
else {
return $this
->buildAjaxForm($form, $form_state);
}
}
/**
* {@inheritdoc}
*/
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
if (!$this
->getEntity()
->isNew() && !$this
->isDialogDisabled()) {
$actions['reset'] = [
'#type' => 'submit',
'#value' => $this
->t('Reset'),
'#submit' => [
'::noSubmit',
],
'#validate' => [
'::noSubmit',
],
'#attributes' => [
'class' => [
'webform-ajax-refresh',
],
],
'#weight' => 100,
];
}
return $actions;
}
/**
* {@inheritdoc}
*/
public function cancelAjaxForm(array &$form, FormStateInterface $form_state) {
$entity = $this
->getEntity();
$entity_type = $entity
->getEntityTypeId();
$entity_id = $entity
->id();
// Must complete reload the entity to make sure all changes are reflected.
$entity_storage = $this->entityTypeManager
->getStorage($entity_type);
$entity_storage
->resetCache([
$entity_id,
]);
$entity = $entity_storage
->load($entity_id);
// Get form object.
$form_object = $this->entityTypeManager
->getFormObject($entity_type, $this->operation);
// Set form entity.
$form_object
->setEntity($entity);
// Set form state.
$form_state = new FormState();
$form_state
->setFormState([]);
$form_state
->setUserInput([]);
// Build form.
/** @var \Drupal\Core\Form\FormBuilderInterface $form_builder */
$form_builder = \Drupal::service('form_builder');
$form = $form_builder
->buildForm($form_object, $form_state);
// Return replace form as response.
return $this
->replaceForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
protected function getDefaultAjaxSettings() {
return [
'disable-refocus' => TRUE,
'progress' => [
'type' => 'fullscreen',
],
];
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
WebformAjaxFormTrait:: |
protected | function | Queue announcement with Ajax response. | |
WebformAjaxFormTrait:: |
protected | function | Add Ajax support to a form. | |
WebformAjaxFormTrait:: |
protected | function | Create an AjaxResponse or WebformAjaxResponse object. | |
WebformAjaxFormTrait:: |
protected | function | Get announcements. | |
WebformAjaxFormTrait:: |
protected | function | Get redirect URL from the form's state. | |
WebformAjaxFormTrait:: |
protected | function | Get the form's Ajax wrapper id. | 1 |
WebformAjaxFormTrait:: |
protected | function | Determine if Ajax callback is callable. | |
WebformAjaxFormTrait:: |
protected | function | Is the current request for an Ajax modal/dialog. | |
WebformAjaxFormTrait:: |
protected | function | Is the current request for an off canvas dialog. | |
WebformAjaxFormTrait:: |
protected | function | Handle missing Ajax callback. | |
WebformAjaxFormTrait:: |
public | function | Empty submit callback used to only have the submit button to use an #ajax submit callback. | 1 |
WebformAjaxFormTrait:: |
protected | function | Reset announcements. | |
WebformAjaxFormTrait:: |
protected | function | Set announcements. | |
WebformAjaxFormTrait:: |
public | function | Submit form #ajax callback. | 1 |
WebformAjaxFormTrait:: |
public | function | Validate form #ajax callback. | 1 |
WebformEntityAjaxFormTrait:: |
protected | function | ||
WebformEntityAjaxFormTrait:: |
public | function | 1 | |
WebformEntityAjaxFormTrait:: |
public | function |
Cancel form #ajax callback. Overrides WebformAjaxFormTrait:: |
|
WebformEntityAjaxFormTrait:: |
protected | function |
Get default ajax callback settings. Overrides WebformAjaxFormTrait:: |
|
WebformEntityAjaxFormTrait:: |
protected | function |
Returns if webform is using Ajax. Overrides WebformAjaxFormTrait:: |
|
WebformEntityAjaxFormTrait:: |
protected | function | Determine if dialogs are disabled. | |
WebformEntityAjaxFormTrait:: |
protected | function |
Replace form via an Ajax response. Overrides WebformAjaxFormTrait:: |