core/includes/form.inc | 18 ++++++---------- core/misc/form.autosave.js | 22 ++++++++++++++------ .../node/lib/Drupal/node/NodeFormController.php | 6 +----- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/core/includes/form.inc b/core/includes/form.inc index 07aa821..675aefd 100644 --- a/core/includes/form.inc +++ b/core/includes/form.inc @@ -873,9 +873,6 @@ function drupal_process_form($form_id, &$form, &$form_state) { // In case of errors, do not break HTML IDs of other forms. drupal_static_reset('drupal_html_id'); } - elseif (!empty($form['#autosave'])) { - $form['#autosave']['#presave'] = TRUE; - } if ($form_state['submitted'] && !form_get_errors() && !$form_state['rebuild']) { // Execute form submit handlers. @@ -954,18 +951,15 @@ function drupal_process_form($form_id, &$form, &$form_state) { } } - if (!empty($form['#autosave']['#enabled'])) { - $form_autosave_settings = array( - 'formAutosave' => array( - $form['#id'] => array( - 'form' => $form['#id'], - 'enabled' => TRUE, - 'presave' => !empty($form['#autosave']['#presave']), - 'changed' => !empty($form['#autosave']['#changed']) ? $form['#autosave']['#changed'] : 0, + if (!empty($form['#autosave'])) { + $form['#attached']['js'][] = array( + 'data' => array( + 'formAutoSave' => array( + $form['#id'] => !empty($form['#autosave']['#changed']) ? $form['#autosave']['#changed'] : 0 ), ), + 'type' => 'setting', ); - drupal_add_js($form_autosave_settings, 'setting'); } // After processing the form, the form builder or a #process callback may diff --git a/core/misc/form.autosave.js b/core/misc/form.autosave.js index 8182c52..ffb4db5 100644 --- a/core/misc/form.autosave.js +++ b/core/misc/form.autosave.js @@ -1,15 +1,25 @@ -(function ($) { +(function ($, Drupal) { "use strict"; /** - * Automatically save the contents of a form - * using localStorage when it is modified. + * Automatically saves the contents of a form using localStorage when modified. */ Drupal.behaviors.formAutoSave = { - attach: function(context, settings) { - $('form', context).garlic(); + attach: function (context, settings) { + for (var formID in settings.formAutoSave) { + if (settings.formAutoSave.hasOwnProperty(formID)) { + $(context).find('#' + formID).garlic({ + expires: 86400 * 30, + conflictManager: { enabled: false }, + // Not yet supported by Garlic, but we need something like this to + // ensure Garlic doesn't overwrite values when the form contains + // different values by default. + lastModifiedIdentifier: settings.formAutoSave[formID] + }); + } + } } }; -})(jQuery); +})(jQuery, Drupal); diff --git a/core/modules/node/lib/Drupal/node/NodeFormController.php b/core/modules/node/lib/Drupal/node/NodeFormController.php index 5b410c8..c12bd5c 100644 --- a/core/modules/node/lib/Drupal/node/NodeFormController.php +++ b/core/modules/node/lib/Drupal/node/NodeFormController.php @@ -58,9 +58,8 @@ protected function prepareEntity(EntityInterface $node) { public function form(array $form, array &$form_state, EntityInterface $node) { $user_config = config('user.settings'); - // Let the node form use localstorage javascript mechanism. + // Let the node form use localStorage to prevent data loss. $form['#autosave'] = array( - '#enabled' => TRUE, '#changed' => isset($node->changed) ? $node->changed : NULL, ); @@ -68,9 +67,6 @@ public function form(array $form, array &$form_state, EntityInterface $node) { if (isset($form_state['node_preview'])) { $form['#prefix'] = $form_state['node_preview']; $node->in_preview = TRUE; - - // In preview state, backup the unsaved state of the node on load. - $form['#autosave']['#presave'] = TRUE; } else { unset($node->in_preview);