From 4361e6795b8126aa5ac320c68268580fa644c1ba Mon Sep 17 00:00:00 2001
From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org>
Date: Mon, 28 Oct 2013 16:58:04 +0000
Subject: [PATCH] Issue #2120117 by Wim Leers, nod_: Fixed
 drupalSettings.user.uid added to page even if there is no other JS;
 drupal.node-new-comments-link lib added for anon users.

---
 core/modules/comment/comment.module |  2 +-
 core/modules/user/user.module       | 21 +++++++++++++++------
 2 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module
index 166f6b7c5786..8b28e76d3319 100644
--- a/core/modules/comment/comment.module
+++ b/core/modules/comment/comment.module
@@ -595,7 +595,7 @@ function comment_entity_view(EntityInterface $entity, EntityDisplay $display, $v
       '#links' => $links,
       '#attributes' => array('class' => array('links', 'inline')),
     );
-    if ($view_mode == 'teaser' && \Drupal::moduleHandler()->moduleExists('history')) {
+    if ($view_mode == 'teaser' && \Drupal::moduleHandler()->moduleExists('history') && \Drupal::currentUser()->isAuthenticated()) {
       $entity->content['links']['#attached']['library'][] = array('comment', 'drupal.node-new-comments-link');
     }
   }
diff --git a/core/modules/user/user.module b/core/modules/user/user.module
index 1535eb772efe..8b528d3cb398 100644
--- a/core/modules/user/user.module
+++ b/core/modules/user/user.module
@@ -116,16 +116,25 @@ function user_theme() {
 function user_page_build(&$page) {
   $path = drupal_get_path('module', 'user');
   $page['#attached']['css'][$path . '/css/user.module.css'] = array('every_page' => TRUE);
+}
+
+/**
+ * Implements hook_js_alter().
+ */
+function user_js_alter(&$javascript) {
+  // If >=1 JavaScript asset has declared a dependency on drupalSettings, the
+  // 'settings' key will exist. Thus when that key does not exist, return early.
+  if (!isset($javascript['settings'])) {
+    return;
+  }
 
   // Provide the user ID in drupalSettings to allow JavaScript code to customize
   // the experience for the end user, rather than the server side, which would
   // break the render cache.
-  global $user;
-  $page['#attached']['js'][] = array(
-    'type' => 'setting',
-    'data' => array('user' => array(
-      'uid' => $user->id(),
-    )),
+  $javascript['settings']['data'][] = array(
+    'user' => array(
+      'uid' => \Drupal::currentUser()->id(),
+    ),
   );
 }
 
-- 
GitLab