Skip to content

Commit

Permalink
feat(slb-146): workaround: purghe stored states on beforeupload
Browse files Browse the repository at this point in the history
  • Loading branch information
dspachos committed Apr 12, 2024
1 parent c8c8276 commit 53b3a50
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@
* Add a variable which determines if the window is being unloaded.
*/
Drupal.autosaveForm.beforeUnloadCalled = false;
$(window).on('beforeunload pagehide', function () {

$(window).on('pagehide', function () {
Drupal.autosaveForm.beforeUnloadCalled = true;
});

$(window).on('beforeunload', function () {
$('#purge-button').trigger('click');
Drupal.autosaveForm.beforeUnloadCalled = true;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,43 @@ public function formAlter(array &$form, FormStateInterface $form_state) {
'#silverback_autosave_session_id' => $silverback_autosave_session_id,
];

$form['purge'] = [
'#type' => 'submit',
'#id' => 'purge-button',
'#name' => 'autosave_form_purge',
'#value' => $this->t('Autosave purge'),
'#limit_validation_errors' => [],
'#attributes' => ['class' => ['autosave-form-purge', 'visually-hidden']],
'#submit' => [[$this, 'autosaveFormPurgeSubmit']],
'#ajax' => [
'callback' => [$this, 'autosaveFormPurgeAjax'],
'event' => 'click',
],
];

$form['silverback_autosave_last_autosave_timestamp'] = [
'#type' => 'hidden',
'#name' => 'silverback_autosave_last_autosave_timestamp',
'#value' => $form_state->get('silverback_autosave_last_autosave_timestamp') ?: '',
];
}

/**
* Form submission handler for rejecting autosaved states.
*/
public function autosaveFormPurgeSubmit($form, FormStateInterface $form_state) {
\Drupal::logger('debug')->debug(__METHOD__);
$this->purgeAllAutosavedStates($form_state, $this->currentUser->id());
}

/**
* Ajax callback for rejecting autosaved states.
*/
public function autosaveFormPurgeAjax($form, FormStateInterface $form_state) {
\Drupal::logger('debug')->debug(__METHOD__);
return new AjaxResponse();
}

/**
* Form submission handler for autosaving forms.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ public function storeEntityAndFormState($form_id, $form_session_id, $entity_type

if (!$entity->isNew()) {

// @todo FIX This causes new previews to restore the saved one (instead of current form state).
// Purge previous stored states for the entity,
// e.g. when user is navigating away from the page.
// Also, this approach ensures that there will be always
// a stored form state in the db, in case we need to restore.
// $this->purgeAutosavedEntityState($entity_type_id, $entity_id, NULL, $form_id, $langcode, $uid);
// Now, enter the new values.
$this->connection->insert(static::AUTOSAVE_ENTITY_FORM_TABLE)
->fields([
'form_id',
Expand Down

0 comments on commit 53b3a50

Please sign in to comment.