diff --git a/core/misc/form.js b/core/misc/form.js index 84212109b6614466c620c35144a1fe5f726f5267..4f741276d0efbb5af3235ace11e604ca79b0deb0 100644 --- a/core/misc/form.js +++ b/core/misc/form.js @@ -170,21 +170,29 @@ }; /** - * Prepopulate form fields with information from the visitor cookie. + * Prepopulate form fields with information from the visitor browser. */ - Drupal.behaviors.fillUserInfoFromCookie = { + Drupal.behaviors.fillUserInfoFromBrowser = { attach: function (context, settings) { var userInfo = ['name', 'mail', 'homepage']; - $('form.user-info-from-cookie').once('user-info-from-cookie', function () { - var $formContext = $(this); - var i, il, $element, cookie; - for (i = 0, il = userInfo.length; i < il; i += 1) { - $element = $formContext.find('[name=' + userInfo[i] + ']'); - cookie = $.cookie('Drupal.visitor.' + userInfo[i]); - if ($element.length && cookie) { - $element.val(cookie); + 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()); + } + }); }); } }; diff --git a/core/modules/comment/src/CommentForm.php b/core/modules/comment/src/CommentForm.php index 7b2eee849e5929291a0ca4016d6686de3d3082d7..2819a56f7dd80489640cd9bfe8453661f75630be 100644 --- a/core/modules/comment/src/CommentForm.php +++ b/core/modules/comment/src/CommentForm.php @@ -88,8 +88,8 @@ 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/jquery.cookie'; - $form['#attributes']['class'][] = 'user-info-from-cookie'; + $form['#attached']['library'][] = 'core/drupal.form'; + $form['#attributes']['data-user-info-from-browser'] = TRUE; } // If not replying to a comment, use our dedicated page callback for new @@ -156,6 +156,9 @@ public function form(array $form, FormStateInterface $form_state) { $form['author']['name']['#theme'] = 'username'; $form['author']['name']['#account'] = $this->currentUser; } + elseif($this->currentUser->isAnonymous()) { + $form['author']['name']['#attributes']['data-drupal-default-value'] = $this->config('user.settings')->get('anonymous'); + } $language_configuration = \Drupal::moduleHandler()->invoke('language', 'get_default_configuration', array('comment', $comment->getTypeId())); $form['langcode'] = array( @@ -367,11 +370,6 @@ public function save(array $form, FormStateInterface $form_state) { $logger = $this->logger('content'); if ($this->currentUser->hasPermission('post comments') && ($this->currentUser->hasPermission('administer comments') || $entity->{$field_name}->status == CommentItemInterface::OPEN)) { - // Save the anonymous user information to a cookie for reuse. - if ($this->currentUser->isAnonymous()) { - user_cookie_save(array_intersect_key($form_state['values'], array_flip(array('name', 'mail', 'homepage')))); - } - $comment->save(); $form_state['values']['cid'] = $comment->id(); diff --git a/core/modules/contact/src/MessageForm.php b/core/modules/contact/src/MessageForm.php index 2e7bd71eb570a7bcac3143dbb027851f581a4f0c..83066a33cb511b9e865682c6e669cadd3c845e38 100644 --- a/core/modules/contact/src/MessageForm.php +++ b/core/modules/contact/src/MessageForm.php @@ -108,8 +108,8 @@ public function form(array $form, FormStateInterface $form_state) { '#required' => TRUE, ); if ($user->isAnonymous()) { - $form['#attached']['library'][] = 'core/jquery.cookie'; - $form['#attributes']['class'][] = 'user-info-from-cookie'; + $form['#attached']['library'][] = 'core/drupal.form'; + $form['#attributes']['data-user-info-from-browser'] = TRUE; } // Do not allow authenticated users to alter the name or email values to // prevent the impersonation of other users. @@ -191,9 +191,7 @@ public function save(array $form, FormStateInterface $form_state) { // over the submitted form values. $sender->name = $message->getSenderName(); $sender->mail = $message->getSenderMail(); - // Save the anonymous user information to a cookie for reuse. - // @todo remove when https://www.drupal.org/node/749748 is in. - user_cookie_save(array('name' => $message->getSenderName(), 'mail' => $message->getSenderMail())); + // For the email message, clarify that the sender name is not verified; it // could potentially clash with a username on this site. $sender->name = $this->t('!name (not verified)', array('!name' => $message->getSenderName())); diff --git a/core/modules/user/src/RegisterForm.php b/core/modules/user/src/RegisterForm.php index 5b9fcd6ce5dea7dc8907e96435e8f5bd1897b0d5..f5c31765850c5fcce64bf4d4e619866370a69b61 100644 --- a/core/modules/user/src/RegisterForm.php +++ b/core/modules/user/src/RegisterForm.php @@ -46,8 +46,8 @@ public function form(array $form, FormStateInterface $form_state) { return new RedirectResponse(url('user/' . \Drupal::currentUser()->id(), array('absolute' => TRUE))); } - $form['#attached']['library'][] = 'core/jquery.cookie'; - $form['#attributes']['class'][] = 'user-info-from-cookie'; + $form['#attached']['library'][] = 'core/drupal.form'; + $form['#attributes']['data-user-info-from-browser'] = TRUE; // Because the user status has security implications, users are blocked by // default when created programmatically and need to be actively activated