From 9b650e91ef7d3d1c308b77193b3e771fdf8bc770 Mon Sep 17 00:00:00 2001
From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org>
Date: Thu, 26 May 2016 12:00:46 +0100
Subject: [PATCH] Issue #2730497 by dawehner: REST Views override existing REST
 routes

---
 .../Plugin/views/display/PathPluginBase.php   |  4 +-
 .../Plugin/display/PathPluginBaseTest.php     | 38 +++++++++++++++++++
 2 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/core/modules/views/src/Plugin/views/display/PathPluginBase.php b/core/modules/views/src/Plugin/views/display/PathPluginBase.php
index da230c4ac9ef..9ee7fd201f51 100644
--- a/core/modules/views/src/Plugin/views/display/PathPluginBase.php
+++ b/core/modules/views/src/Plugin/views/display/PathPluginBase.php
@@ -237,8 +237,8 @@ public function alterRoutes(RouteCollection $collection) {
       $route_path = RouteCompiler::getPathWithoutDefaults($route);
       $route_path = RouteCompiler::getPatternOutline($route_path);
       // Ensure that we don't override a route which is already controlled by
-      // views.
-      if (!$route->hasDefault('view_id') && ('/' . $view_path == $route_path)) {
+      // views. Also ensure that we don't override for example REST routes.
+      if (!$route->hasDefault('view_id') && ('/' . $view_path == $route_path) && (!$route->getMethods() || in_array('GET', $route->getMethods()))) {
         $parameters = $route->compile()->getPathVariables();
 
         // @todo Figure out whether we need to merge some settings (like
diff --git a/core/modules/views/tests/src/Unit/Plugin/display/PathPluginBaseTest.php b/core/modules/views/tests/src/Unit/Plugin/display/PathPluginBaseTest.php
index 90d2ae74c469..9241a3537352 100644
--- a/core/modules/views/tests/src/Unit/Plugin/display/PathPluginBaseTest.php
+++ b/core/modules/views/tests/src/Unit/Plugin/display/PathPluginBaseTest.php
@@ -260,6 +260,44 @@ public function testAlterRoute() {
     $this->assertSame($collection->get('test_route_2'), $route_2);
   }
 
+  /**
+   * Tests the altering of a REST route.
+   */
+  public function testAlterRestRoute() {
+    $collection = new RouteCollection();
+    $route = new Route('test_route', ['_controller' => 'Drupal\Tests\Core\Controller\TestController::content']);
+    $route->setMethods(['POST']);
+    $collection->add('test_route', $route);
+
+    list($view) = $this->setupViewExecutableAccessPlugin();
+
+    $display = [];
+    $display['display_plugin'] = 'page';
+    $display['id'] = 'page_1';
+    $display['display_options'] = [
+      'path' => 'test_route',
+    ];
+    $this->pathPlugin->initDisplay($view, $display);
+
+    $this->pathPlugin->collectRoutes($collection);
+    $view_route_names = $this->pathPlugin->alterRoutes($collection);
+    $this->assertEquals([], $view_route_names);
+
+    // Ensure that the test_route is not overridden.
+    $this->assertCount(2, $collection);
+    $route = $collection->get('test_route');
+    $this->assertTrue($route instanceof Route);
+    $this->assertFalse($route->hasDefault('view_id'));
+    $this->assertFalse($route->hasDefault('display_id'));
+    $this->assertSame($collection->get('test_route'), $route);
+
+    $route = $collection->get('view.test_id.page_1');
+    $this->assertTrue($route instanceof Route);
+    $this->assertEquals('test_id', $route->getDefault('view_id'));
+    $this->assertEquals('page_1', $route->getDefault('display_id'));
+    $this->assertEquals('my views title', $route->getDefault('_title'));
+  }
+
   /**
    * Tests the alter route method with preexisting title callback.
    */
-- 
GitLab