From 1151c73c1924e539f25e5abbcbc229f84e1a241a Mon Sep 17 00:00:00 2001
From: Dries <dries@buytaert.net>
Date: Tue, 26 Nov 2013 17:06:57 -0500
Subject: [PATCH] Issue #787896 by nod_, cha0s, jp.stacey, sun,
 David_Rothstein, Bojhan, lisarex: Add a link so that administrators can
 return to their most recently visited non-admin page.

---
 core/includes/common.inc                      |  9 ++++-
 .../block/Controller/BlockController.php      | 10 +++++
 core/modules/toolbar/css/toolbar.icons.css    | 27 ++++++-------
 core/modules/toolbar/css/toolbar.module.css   |  3 ++
 core/modules/toolbar/js/escapeAdmin.js        | 38 +++++++++++++++++++
 core/modules/toolbar/toolbar.module           | 28 ++++++++++++--
 6 files changed, 96 insertions(+), 19 deletions(-)
 create mode 100644 core/modules/toolbar/js/escapeAdmin.js

diff --git a/core/includes/common.inc b/core/includes/common.inc
index 36e069db6655..914d1365277d 100644
--- a/core/includes/common.inc
+++ b/core/includes/common.inc
@@ -2215,11 +2215,18 @@ function drupal_add_js($data = NULL, $options = NULL) {
           $scriptPath = $GLOBALS['script_path'];
           $pathPrefix = '';
           url('', array('script' => &$scriptPath, 'prefix' => &$pathPrefix));
+          $current_path = current_path();
+          $current_path_is_admin = FALSE;
+          // The function path_is_admin() is not available on update.php pages.
+          if (!(defined('MAINTENANCE_MODE') && MAINTENANCE_MODE === 'update')) {
+            $current_path_is_admin = path_is_admin($current_path);
+          }
           $javascript['settings']['data'][] = array(
             'basePath' => base_path(),
             'scriptPath' => $scriptPath,
             'pathPrefix' => $pathPrefix,
-            'currentPath' => current_path(),
+            'currentPath' => $current_path,
+            'currentPathIsAdmin' => $current_path_is_admin,
           );
         }
         // All JavaScript settings are placed in the header of the page with
diff --git a/core/modules/block/lib/Drupal/block/Controller/BlockController.php b/core/modules/block/lib/Drupal/block/Controller/BlockController.php
index c735ba0f726c..f2ebb41e2b72 100644
--- a/core/modules/block/lib/Drupal/block/Controller/BlockController.php
+++ b/core/modules/block/lib/Drupal/block/Controller/BlockController.php
@@ -30,6 +30,16 @@ public function demo($theme) {
     return array(
       '#title' => String::checkPlain($themes[$theme]->info['name']),
       '#attached' => array(
+        'js' => array(
+          array(
+            // The block demonstration page is not marked as an administrative
+            // page by path_is_admin() function in order to use the frontend
+            // theme. Since JavaScript relies on a proper separation of admin
+            // pages, it needs to know this is an actual administrative page.
+            'data' => array('currentPathIsAdmin' => TRUE),
+            'type' => 'setting',
+          )
+        ),
         'library' => array(
           array('block', 'drupal.block.admin'),
         ),
diff --git a/core/modules/toolbar/css/toolbar.icons.css b/core/modules/toolbar/css/toolbar.icons.css
index b10de97aa305..64d8bf478eab 100644
--- a/core/modules/toolbar/css/toolbar.icons.css
+++ b/core/modules/toolbar/css/toolbar.icons.css
@@ -65,20 +65,6 @@
 /**
  * Top level icons.
  */
-.toolbar-bar .toolbar-icon-home:before {
-  background-image: url("../../../misc/icons/bebebe/house.svg");
-}
-.no-svg .toolbar-bar .toolbar-icon-home:before {
-  background-image: url("../../../misc/icons/bebebe/house.png");
-}
-.toolbar-bar .toolbar-icon-home:active:before,
-.toolbar-bar .toolbar-icon-home.active:before {
-  background-image: url("../../../misc/icons/ffffff/house.svg");
-}
-.no-svg .toolbar-bar .toolbar-icon-home:active:before,
-.no-svg .toolbar-bar .toolbar-icon-home.active:before {
-  background-image: url("../../../misc/icons/ffffff/house.png");
-}
 .toolbar-bar .toolbar-icon-menu:before {
   background-image: url("../../../misc/icons/bebebe/hamburger.svg");
 }
@@ -319,7 +305,18 @@
   background-image: url("../../../misc/icons/787878/twistie-up.png");
   background-size: auto auto;
 }
-
+.toolbar .toolbar-icon-escape-admin:before {
+  background-image: url("../../../misc/icons/bebebe/chevron-disc-left.svg");
+}
+.no-svg .toolbar .toolbar-icon-escape-admin:before {
+  background-image: url("../../../misc/icons/bebebe/chevron-disc-left.png");
+}
+[dir="rtl"] .toolbar .toolbar-icon-escape-admin:before {
+  background-image: url("../../../misc/icons/bebebe/chevron-disc-right.svg");
+}
+[dir="rtl"] .no-svg .toolbar .toolbar-icon-escape-admin:before {
+  background-image: url("../../../misc/icons/bebebe/chevron-disc-right.png");
+}
 /**
  * Orientation toggle.
  */
diff --git a/core/modules/toolbar/css/toolbar.module.css b/core/modules/toolbar/css/toolbar.module.css
index 279d57bb233b..df76b90e2ccb 100644
--- a/core/modules/toolbar/css/toolbar.module.css
+++ b/core/modules/toolbar/css/toolbar.module.css
@@ -38,6 +38,9 @@
 .toolbar .menu li {
   display: block;
 }
+.toolbar .toolbar-bar .toolbar-tab.hidden {
+  display: none;
+}
 .toolbar a {
   display: block;
   line-height: 1;
diff --git a/core/modules/toolbar/js/escapeAdmin.js b/core/modules/toolbar/js/escapeAdmin.js
new file mode 100644
index 000000000000..0338280ef911
--- /dev/null
+++ b/core/modules/toolbar/js/escapeAdmin.js
@@ -0,0 +1,38 @@
+/**
+ * @file
+ *
+ * Replaces the home link in toolbar with a back to site link.
+ */
+(function ($, Drupal, drupalSettings) {
+
+"use strict";
+
+var escapeAdminPath = sessionStorage.getItem('escapeAdminPath');
+
+// Saves the last non-administrative page in the browser to be able to link back
+// to it when browsing administrative pages. If there is a destination parameter
+// there is not need to save the current path because the page is loaded within
+// an existing "workflow".
+if (!drupalSettings.currentPathIsAdmin && !/destination=/.test(window.location.search)) {
+  sessionStorage.setItem('escapeAdminPath', drupalSettings.currentPath);
+}
+
+/**
+ * Replaces the "Home" link with "Back to site" link.
+ *
+ * Back to site link points to the last non-administrative page the user visited
+ * within the same browser tab.
+ */
+Drupal.behaviors.escapeAdmin = {
+  attach: function () {
+    var $toolbarEscape = $('[data-toolbar-escape-admin]').once('escapeAdmin');
+    if ($toolbarEscape.length) {
+      if (drupalSettings.currentPathIsAdmin && escapeAdminPath) {
+        $toolbarEscape.attr('href', Drupal.url(escapeAdminPath));
+        $toolbarEscape.closest('.toolbar-tab').removeClass('hidden');
+      }
+    }
+  }
+};
+
+})(jQuery, Drupal, drupalSettings);
diff --git a/core/modules/toolbar/toolbar.module b/core/modules/toolbar/toolbar.module
index 45610c1b0306..32027858a033 100644
--- a/core/modules/toolbar/toolbar.module
+++ b/core/modules/toolbar/toolbar.module
@@ -410,15 +410,24 @@ function toolbar_toolbar() {
     '#type' => 'toolbar_item',
     'tab' => array(
       '#type' => 'link',
-      '#title' => t('Home'),
+      '#title' => t('Back to site'),
       '#href' => '<front>',
       '#options' => array(
         'attributes' => array(
-          'title' => t('Home page'),
-          'class' => array('toolbar-icon', 'toolbar-icon-home'),
+          'title' => t('Return to site content'),
+          'class' => array('toolbar-icon', 'toolbar-icon-escape-admin'),
+          'data-toolbar-escape-admin' => TRUE,
         ),
       ),
     ),
+    '#wrapper_attributes' => array(
+      'class' => array('hidden'),
+    ),
+    '#attached' => array(
+      'library' => array(
+        array('toolbar', 'toolbar.escapeAdmin'),
+      ),
+    ),
     '#weight' => -20,
   );
 
@@ -609,6 +618,19 @@ function toolbar_library_info() {
       array('system', 'jquery.once'),
     ),
   );
+  $libraries['toolbar.escapeAdmin'] = array(
+    'title' => 'Provides a button to escape the administration area.',
+    'version' => \Drupal::VERSION,
+    'js' => array(
+      drupal_get_path('module', 'toolbar') . '/js/escapeAdmin.js',
+    ),
+    'dependencies' => array(
+      array('system', 'jquery'),
+      array('system', 'drupal'),
+      array('system', 'drupalSettings'),
+      array('system', 'jquery.once'),
+    ),
+  );
 
   return $libraries;
 }
-- 
GitLab