diff --git a/core/modules/views/src/Form/ViewsForm.php b/core/modules/views/src/Form/ViewsForm.php index cdf39eb19ee099a85724eb79d457d1841b941c5c..49517774441bb970c07b09ec7e443bbca186d9e8 100644 --- a/core/modules/views/src/Form/ViewsForm.php +++ b/core/modules/views/src/Form/ViewsForm.php @@ -14,6 +14,7 @@ use Drupal\Core\Form\FormInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Routing\UrlGeneratorInterface; +use Drupal\Core\Url; use Drupal\views\ViewExecutable; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\RequestStack; diff --git a/core/modules/views/src/ViewExecutable.php b/core/modules/views/src/ViewExecutable.php index fe65fe36d2a4b99c7a883390c208b2dd98ff87d8..71b37c8bd7507a05cfb4c75c8e701ecc8d462094 100644 --- a/core/modules/views/src/ViewExecutable.php +++ b/core/modules/views/src/ViewExecutable.php @@ -21,6 +21,7 @@ use Drupal\Component\Utility\Tags; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Routing\Exception\RouteNotFoundException; /** * Represents a view as a whole. @@ -1763,6 +1764,17 @@ public function hasUrl($args = NULL, $display_id = NULL) { return FALSE; } + // Look up the route name to make sure it exists. The name may exist, but + // not be available yet in some instances when editing a view and doing + // a live preview. + $provider = \Drupal::service('router.route_provider'); + try { + $provider->getRouteByName($display_handler->getRouteName()); + } + catch (RouteNotFoundException $e) { + return FALSE; + } + return TRUE; } diff --git a/core/modules/views_ui/src/Tests/UnsavedPreviewTest.php b/core/modules/views_ui/src/Tests/UnsavedPreviewTest.php new file mode 100644 index 0000000000000000000000000000000000000000..f3f44641caff0560f9fb953ee37e34302cd9eb6b --- /dev/null +++ b/core/modules/views_ui/src/Tests/UnsavedPreviewTest.php @@ -0,0 +1,84 @@ +<?php + +/** + * @file + * Contains of \Drupal\views_ui\Tests\UnsavedPreviewTest. + */ + +namespace Drupal\views_ui\Tests; + +use Drupal\views\Tests\ViewTestBase; + +/** + * Tests covering Preview of unsaved Views. + * + * @group views_ui + */ +class UnsavedPreviewTest extends ViewTestBase { + + /** + * Views used by this test. + * + * @var array + */ + public static $testViews = ['content']; + + /** + * An admin user with the 'administer views' permission. + * + * @var \Drupal\user\UserInterface + */ + protected $adminUser; + + /** + * {@inheritdoc} + */ + public static $modules = array('node', 'views_ui'); + + /** + * Sets up a Drupal site for running functional and integration tests. + */ + protected function setUp() { + parent::setUp(FALSE); + + $this->adminUser = $this->drupalCreateUser(['administer views']); + $this->drupalLogin($this->adminUser); + } + + /** + * Tests previews of unsaved new page displays. + */ + public function testUnsavedPageDisplayPreview() { + $this->drupalCreateContentType(['type' => 'page']); + for ($i = 0; $i < 5; $i++) { + $this->drupalCreateNode(); + } + + $this->drupalGet('admin/structure/views/view/content'); + $this->assertResponse(200); + + $this->drupalPostForm(NULL, [], t('Add Page')); + $this->assertResponse(200); + + $this->drupalGet('admin/structure/views/nojs/display/content/page_2/path'); + $this->assertResponse(200); + + $this->drupalPostForm(NULL, ['path' => 'foobarbaz'], t('Apply')); + $this->assertResponse(200); + + $this->drupalPostForm(NULL, [], t('Update preview')); + $this->assertResponse(200); + $this->assertText(t('This display has no path')); + + $this->drupalGet('admin/structure/views/view/content/edit/page_2'); + $this->assertResponse(200); + + $this->drupalPostForm(NULL, [], t('Save')); + $this->assertResponse(200); + + $this->drupalPostForm(NULL, [], t('Update preview')); + $this->assertResponse(200); + $this->assertLinkByHref('foobarbaz'); + } + +} diff --git a/core/modules/views_ui/src/ViewUI.php b/core/modules/views_ui/src/ViewUI.php index 4c82de70844a32e48c0f23afeb5ae05fa5aec16d..8b5569e4d036307eb6ed08cc522619b1c6dbff56 100644 --- a/core/modules/views_ui/src/ViewUI.php +++ b/core/modules/views_ui/src/ViewUI.php @@ -598,7 +598,7 @@ public function renderPreview($display_id, $args = array()) { $executable->setArguments($args); // Store the current view URL for later use: - if ($executable->display_handler->getOption('path')) { + if ($executable->hasUrl() && $executable->display_handler->getOption('path')) { $path = $executable->getUrl(); }