View source
<?php
namespace Drupal\webform\Form;
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Ajax\WebformHtmlCommand;
trait WebformEntityAjaxFormTrait {
use WebformAjaxFormTrait;
protected function isAjax() {
return TRUE;
}
protected function isDialogDisabled() {
return \Drupal::config('webform.settings')
->get('ui.dialog_disabled');
}
protected function replaceForm(array $form, FormStateInterface $form_state) {
$form = [
'status_messages' => [
'#type' => 'status_messages',
'#weight' => -1000,
],
] + $form;
unset($form['#prefix'], $form['#suffix']);
$response = $this
->createAjaxResponse($form, $form_state);
$response
->addCommand(new WebformHtmlCommand('#' . $this
->getWrapperId(), $form));
return $response;
}
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);
}
}
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;
}
public function cancelAjaxForm(array &$form, FormStateInterface $form_state) {
$entity = $this
->getEntity();
$entity_type = $entity
->getEntityTypeId();
$entity_id = $entity
->id();
$entity_storage = $this->entityTypeManager
->getStorage($entity_type);
$entity_storage
->resetCache([
$entity_id,
]);
$entity = $entity_storage
->load($entity_id);
$form_object = $this->entityTypeManager
->getFormObject($entity_type, $this->operation);
$form_object
->setEntity($entity);
$form_state = new FormState();
$form_state
->setFormState([]);
$form_state
->setUserInput([]);
$form_builder = \Drupal::service('form_builder');
$form = $form_builder
->buildForm($form_object, $form_state);
return $this
->replaceForm($form, $form_state);
}
protected function getDefaultAjaxSettings() {
return [
'disable-refocus' => TRUE,
'progress' => [
'type' => 'fullscreen',
],
];
}
}