You are here

public function AutosaveEntityFormHandler::autosaveFormAjax in Autosave Form 8

Ajax callback for autosaving content entity forms.

File

src/Form/AutosaveEntityFormHandler.php, line 179

Class

AutosaveEntityFormHandler
Base class for autosave form handlers.

Namespace

Drupal\autosave_form\Form

Code

public function autosaveFormAjax($form, FormStateInterface $form_state) {
  $response = $this
    ->traitAutosaveFormAjax($form, $form_state);
  $timestamp = $form_state
    ->getTemporaryValue('autosave_form_last_autosave_timestamp');
  if ($timestamp == 'entity_saved_meanwhile') {
    $input = $form_state
      ->getUserInput();

    // Autosave has run already or this is the first run.
    if (is_numeric($input['autosave_form_last_autosave_timestamp']) || empty($input['autosave_form_last_autosave_timestamp'])) {

      // Alert the user about turning off autosave and deleting all the
      // autosaved states.
      $message = $this->configFactory
        ->get('autosave_form.messages')
        ->get('entity_saved_in_background_alert_message');
      $options = [
        'width' => 'auto',
        'closeOnEscape' => FALSE,
      ];
      $response
        ->addCommand(new OpenAutosaveDisabledDialog($this
        ->t('Autosave has been disabled'), $message, $options));

      // Reset the last autosaved timestamp on the page.
      $response
        ->addCommand(new InvokeCommand('input[name="autosave_form_last_autosave_timestamp"]', 'attr', [
        'value',
        (string) $this
          ->t('Autosave is turned off.'),
      ]));

      // Stop the autosave submissions.
      $attachments = [];
      $attachments['drupalSettings']['autosaveForm']['autosaveFormRunning'] = FALSE;
      $response
        ->addAttachments($attachments);
    }
  }
  return $response;
}