From 9526bb82689b58713715ee1aa552321fdd94dfd0 Mon Sep 17 00:00:00 2001
From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org>
Date: Mon, 10 Feb 2014 14:21:34 +0000
Subject: [PATCH] Issue #2081963 by mr.baileys, dawehner: Shortcut add/remove
 link on titles don't work on route only pages.

---
 .../Drupal/shortcut/Tests/ShortcutLinksTest.php    | 14 +++++++++++---
 core/modules/shortcut/shortcut.module              | 13 ++++++++++++-
 2 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/core/modules/shortcut/lib/Drupal/shortcut/Tests/ShortcutLinksTest.php b/core/modules/shortcut/lib/Drupal/shortcut/Tests/ShortcutLinksTest.php
index 10d56e265deb..9025e535ab06 100644
--- a/core/modules/shortcut/lib/Drupal/shortcut/Tests/ShortcutLinksTest.php
+++ b/core/modules/shortcut/lib/Drupal/shortcut/Tests/ShortcutLinksTest.php
@@ -165,16 +165,24 @@ public function testNoShortcutLink() {
       ->save();
 
     $this->drupalGet('page-that-does-not-exist');
-    $this->assertNoRaw('add-shortcut', 'Add to shortcuts link was not shown on a page not found.');
+    $result = $this->xpath('//div[contains(@class, "add-shortcut")]');
+    $this->assertTrue(empty($result), 'Add to shortcuts link was not shown on a page not found.');
 
     // The user does not have access to this path.
     $this->drupalGet('admin/modules');
-    $this->assertNoRaw('add-shortcut', 'Add to shortcuts link was not shown on a page the user does not have access to.');
+    $result = $this->xpath('//div[contains(@class, "add-shortcut")]');
+    $this->assertTrue(empty($result), 'Add to shortcuts link was not shown on a page the user does not have access to.');
 
     // Verify that the testing mechanism works by verifying the shortcut
     // link appears on admin/people.
     $this->drupalGet('admin/people');
-    $this->assertRaw('remove-shortcut', 'Remove from shortcuts link was shown on a page the user does have access to.');
+    $result = $this->xpath('//div[contains(@class, "remove-shortcut")]');
+    $this->assertTrue(!empty($result), 'Remove from shortcuts link was shown on a page the user does have access to.');
+
+    // Verify that the shortcut link appears on routing only pages.
+    $this->drupalGet('router_test/test2');
+    $result = $this->xpath('//div[contains(@class, "add-shortcut")]');
+    $this->assertTrue(!empty($result), 'Add to shortcuts link was shown on a page the user does have access to.');
   }
 
 }
diff --git a/core/modules/shortcut/shortcut.module b/core/modules/shortcut/shortcut.module
index 6251859e0fb1..b520e08627b0 100644
--- a/core/modules/shortcut/shortcut.module
+++ b/core/modules/shortcut/shortcut.module
@@ -8,6 +8,7 @@
 use Drupal\Core\Routing\UrlMatcher;
 use Drupal\Core\Url;
 use Drupal\shortcut\ShortcutSetInterface;
+use Symfony\Cmf\Component\Routing\RouteObjectInterface;
 use Symfony\Component\HttpFoundation\Request;
 
 /**
@@ -388,7 +389,17 @@ function shortcut_preprocess_page(&$variables) {
   // shortcuts and if the page's actual content is being shown (for example,
   // we do not want to display it on "access denied" or "page not found"
   // pages).
-  if (shortcut_set_edit_access() && ($item = menu_get_item()) && $item['access']) {
+
+  // Load the router item corresponding to the current page.
+  $request = \Drupal::request();
+  $item = array();
+  if ($route = $request->attributes->get(RouteObjectInterface::ROUTE_NAME)) {
+    $item['href'] = $request->attributes->get('_system_path');
+    // @todo What should be done on a 404/403 page?
+    $item['access'] = TRUE;
+  }
+
+  if (shortcut_set_edit_access() && !empty($item['access'])) {
     $link = current_path();
     if (!($url = Url::createFromPath($link))) {
       // Bail out early if we couldn't find a matching route.
-- 
GitLab