Commit 5afbef95 authored by alexpott's avatar alexpott

Issue #749748 by Bès, penyaskito, sun, hefox, Dave Reid, mavimo | c960657:...

Issue #749748 by Bès, penyaskito, sun, hefox, Dave Reid, mavimo | c960657: Fixed Contact, register and comment forms do not prefill with user info from browser.
parent 627a719a
...@@ -170,21 +170,29 @@ ...@@ -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) { attach: function (context, settings) {
var userInfo = ['name', 'mail', 'homepage']; var userInfo = ['name', 'mail', 'homepage'];
$('form.user-info-from-cookie').once('user-info-from-cookie', function () { var $forms = $('[data-user-info-from-browser]').once('user-info-from-browser');
var $formContext = $(this); if ($forms.length) {
var i, il, $element, cookie; userInfo.map(function (info) {
for (i = 0, il = userInfo.length; i < il; i += 1) { var $element = $forms.find('[name=' + info + ']');
$element = $formContext.find('[name=' + userInfo[i] + ']'); var browserData = localStorage.getItem('Drupal.visitor.' + info);
cookie = $.cookie('Drupal.visitor.' + userInfo[i]); var emptyOrDefault = ($element.val() === '' || ($element.attr('data-drupal-default-value') === $element.val()));
if ($element.length && cookie) { if ($element.length && emptyOrDefault && browserData) {
$element.val(cookie); $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());
}
});
}); });
} }
}; };
......
...@@ -88,8 +88,8 @@ public function form(array $form, FormStateInterface $form_state) { ...@@ -88,8 +88,8 @@ public function form(array $form, FormStateInterface $form_state) {
$is_admin = $comment->id() && $this->currentUser->hasPermission('administer comments'); $is_admin = $comment->id() && $this->currentUser->hasPermission('administer comments');
if (!$this->currentUser->isAuthenticated() && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT) { if (!$this->currentUser->isAuthenticated() && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT) {
$form['#attached']['library'][] = 'core/jquery.cookie'; $form['#attached']['library'][] = 'core/drupal.form';
$form['#attributes']['class'][] = 'user-info-from-cookie'; $form['#attributes']['data-user-info-from-browser'] = TRUE;
} }
// If not replying to a comment, use our dedicated page callback for new // 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) { ...@@ -156,6 +156,9 @@ public function form(array $form, FormStateInterface $form_state) {
$form['author']['name']['#theme'] = 'username'; $form['author']['name']['#theme'] = 'username';
$form['author']['name']['#account'] = $this->currentUser; $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())); $language_configuration = \Drupal::moduleHandler()->invoke('language', 'get_default_configuration', array('comment', $comment->getTypeId()));
$form['langcode'] = array( $form['langcode'] = array(
...@@ -367,11 +370,6 @@ public function save(array $form, FormStateInterface $form_state) { ...@@ -367,11 +370,6 @@ public function save(array $form, FormStateInterface $form_state) {
$logger = $this->logger('content'); $logger = $this->logger('content');
if ($this->currentUser->hasPermission('post comments') && ($this->currentUser->hasPermission('administer comments') || $entity->{$field_name}->status == CommentItemInterface::OPEN)) { 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(); $comment->save();
$form_state['values']['cid'] = $comment->id(); $form_state['values']['cid'] = $comment->id();
......
...@@ -108,8 +108,8 @@ public function form(array $form, FormStateInterface $form_state) { ...@@ -108,8 +108,8 @@ public function form(array $form, FormStateInterface $form_state) {
'#required' => TRUE, '#required' => TRUE,
); );
if ($user->isAnonymous()) { if ($user->isAnonymous()) {
$form['#attached']['library'][] = 'core/jquery.cookie'; $form['#attached']['library'][] = 'core/drupal.form';
$form['#attributes']['class'][] = 'user-info-from-cookie'; $form['#attributes']['data-user-info-from-browser'] = TRUE;
} }
// Do not allow authenticated users to alter the name or email values to // Do not allow authenticated users to alter the name or email values to
// prevent the impersonation of other users. // prevent the impersonation of other users.
...@@ -191,9 +191,7 @@ public function save(array $form, FormStateInterface $form_state) { ...@@ -191,9 +191,7 @@ public function save(array $form, FormStateInterface $form_state) {
// over the submitted form values. // over the submitted form values.
$sender->name = $message->getSenderName(); $sender->name = $message->getSenderName();
$sender->mail = $message->getSenderMail(); $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 // For the email message, clarify that the sender name is not verified; it
// could potentially clash with a username on this site. // could potentially clash with a username on this site.
$sender->name = $this->t('!name (not verified)', array('!name' => $message->getSenderName())); $sender->name = $this->t('!name (not verified)', array('!name' => $message->getSenderName()));
......
...@@ -46,8 +46,8 @@ public function form(array $form, FormStateInterface $form_state) { ...@@ -46,8 +46,8 @@ public function form(array $form, FormStateInterface $form_state) {
return new RedirectResponse(url('user/' . \Drupal::currentUser()->id(), array('absolute' => TRUE))); return new RedirectResponse(url('user/' . \Drupal::currentUser()->id(), array('absolute' => TRUE)));
} }
$form['#attached']['library'][] = 'core/jquery.cookie'; $form['#attached']['library'][] = 'core/drupal.form';
$form['#attributes']['class'][] = 'user-info-from-cookie'; $form['#attributes']['data-user-info-from-browser'] = TRUE;
// Because the user status has security implications, users are blocked by // Because the user status has security implications, users are blocked by
// default when created programmatically and need to be actively activated // default when created programmatically and need to be actively activated
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment