You are here

public function WebformEntityAjaxFormTrait::cancelAjaxForm in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Form/WebformEntityAjaxFormTrait.php \Drupal\webform\Form\WebformEntityAjaxFormTrait::cancelAjaxForm()

Cancel form #ajax callback.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

\Drupal\Core\Ajax\AjaxResponse An Ajax response that display validation error messages or redirects to a URL

Overrides WebformAjaxFormTrait::cancelAjaxForm

File

src/Form/WebformEntityAjaxFormTrait.php, line 99

Class

WebformEntityAjaxFormTrait
Trait for webform entity ajax support.

Namespace

Drupal\webform\Form

Code

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);
}