diff --git a/core/lib/Drupal/Core/Form/FormBuilder.php b/core/lib/Drupal/Core/Form/FormBuilder.php index d16d851..524e71d 100644 --- a/core/lib/Drupal/Core/Form/FormBuilder.php +++ b/core/lib/Drupal/Core/Form/FormBuilder.php @@ -398,6 +398,10 @@ public function retrieveForm($form_id, FormStateInterface &$form_state) { $form['#attributes']['class'][] = Html::getClass($build_info['base_form_id']); } + // Make sure misc/form.js is included in all forms. + // This is important to prevent multiple form submits (@see Drupal.behaviors.formSingleSubmit). + $form['#attached']['library'][] = 'core/drupal.form'; + // We need to pass $form_state by reference in order for forms to modify it, // since call_user_func_array() requires that referenced variables are // passed explicitly. diff --git a/core/misc/form.js b/core/misc/form.js index b1d1fff..4b1b53a 100644 --- a/core/misc/form.js +++ b/core/misc/form.js @@ -170,32 +170,4 @@ } }; - /** - * Prepopulate form fields with information from the visitor browser. - */ - Drupal.behaviors.fillUserInfoFromBrowser = { - attach: function (context, settings) { - var userInfo = ['name', 'mail', 'homepage']; - var $forms = $('[data-user-info-from-browser]').once('user-info-from-browser'); - if ($forms.length) { - userInfo.map(function (info) { - var $element = $forms.find('[name=' + info + ']'); - var browserData = localStorage.getItem('Drupal.visitor.' + info); - var emptyOrDefault = ($element.val() === '' || ($element.attr('data-drupal-default-value') === $element.val())); - if ($element.length && emptyOrDefault && browserData) { - $element.val(browserData); - } - }); - } - $forms.on('submit', function () { - userInfo.map(function (info) { - var $element = $forms.find('[name=' + info + ']'); - if ($element.length) { - localStorage.setItem('Drupal.visitor.' + info, $element.val()); - } - }); - }); - } - }; - })(jQuery, Drupal, Drupal.debounce); diff --git a/core/modules/comment/src/CommentForm.php b/core/modules/comment/src/CommentForm.php index a9d742b..658f5ef 100644 --- a/core/modules/comment/src/CommentForm.php +++ b/core/modules/comment/src/CommentForm.php @@ -87,7 +87,7 @@ public function form(array $form, FormStateInterface $form_state) { $is_admin = $comment->id() && $this->currentUser->hasPermission('administer comments'); if (!$this->currentUser->isAuthenticated() && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT) { - $form['#attached']['library'][] = 'core/drupal.form'; + $form['#attached']['library'][] = 'user/drupal.user.fill'; $form['#attributes']['data-user-info-from-browser'] = TRUE; } diff --git a/core/modules/contact/src/MessageForm.php b/core/modules/contact/src/MessageForm.php index 908ed17..5ab7616 100644 --- a/core/modules/contact/src/MessageForm.php +++ b/core/modules/contact/src/MessageForm.php @@ -109,7 +109,7 @@ public function form(array $form, FormStateInterface $form_state) { '#required' => TRUE, ); if ($user->isAnonymous()) { - $form['#attached']['library'][] = 'core/drupal.form'; + $form['#attached']['library'][] = 'user/drupal.user.fill'; $form['#attributes']['data-user-info-from-browser'] = TRUE; } // Do not allow authenticated users to alter the name or email values to diff --git a/core/modules/user/src/RegisterForm.php b/core/modules/user/src/RegisterForm.php index bbf131e..aa786d7 100644 --- a/core/modules/user/src/RegisterForm.php +++ b/core/modules/user/src/RegisterForm.php @@ -40,7 +40,7 @@ public function form(array $form, FormStateInterface $form_state) { '#value' => $admin, ); - $form['#attached']['library'][] = 'core/drupal.form'; + $form['#attached']['library'][] = 'user/drupal.user.fill'; // For non-admin users, populate the form fields using data from the // browser. diff --git a/core/modules/user/user.fill.js b/core/modules/user/user.fill.js new file mode 100644 index 0000000..47bcf9b --- /dev/null +++ b/core/modules/user/user.fill.js @@ -0,0 +1,33 @@ +(function ($) { + + "use strict"; + + /** + * Prepopulate form fields with information from the visitor browser. + */ + Drupal.behaviors.fillUserInfoFromBrowser = { + attach: function (context, settings) { + var userInfo = ['name', 'mail', 'homepage']; + var $forms = $('[data-user-info-from-browser]').once('user-info-from-browser'); + if ($forms.length) { + userInfo.map(function (info) { + var $element = $forms.find('[name=' + info + ']'); + var browserData = localStorage.getItem('Drupal.visitor.' + info); + var emptyOrDefault = ($element.val() === '' || ($element.attr('data-drupal-default-value') === $element.val())); + if ($element.length && emptyOrDefault && browserData) { + $element.val(browserData); + } + }); + } + $forms.on('submit', function () { + userInfo.map(function (info) { + var $element = $forms.find('[name=' + info + ']'); + if ($element.length) { + localStorage.setItem('Drupal.visitor.' + info, $element.val()); + } + }); + }); + } + }; + +})(jQuery); diff --git a/core/modules/user/user.libraries.yml b/core/modules/user/user.libraries.yml index 037023a..d8e5cae 100644 --- a/core/modules/user/user.libraries.yml +++ b/core/modules/user/user.libraries.yml @@ -32,3 +32,10 @@ drupal.user.icons: css: theme: css/user.icons.css: {} + +drupal.user.fill: + version: VERSION + js: + user.fill.js: {} + dependencies: + - core/drupal.form