From 994f683eeac45e27d228ed70c7e946b1edab8bbd Mon Sep 17 00:00:00 2001 From: xjm Date: Wed, 22 Apr 2015 13:24:32 +0100 Subject: [PATCH] Issue #2446783 by mpdonadio, dawehner, koence: Views preview not working without saving new display --- core/modules/views/src/Form/ViewsForm.php | 1 + core/modules/views/src/ViewExecutable.php | 12 +++ .../views_ui/src/Tests/UnsavedPreviewTest.php | 84 +++++++++++++++++++ core/modules/views_ui/src/ViewUI.php | 2 +- 4 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 core/modules/views_ui/src/Tests/UnsavedPreviewTest.php diff --git a/core/modules/views/src/Form/ViewsForm.php b/core/modules/views/src/Form/ViewsForm.php index cdf39eb19ee..49517774441 100644 --- a/core/modules/views/src/Form/ViewsForm.php +++ b/core/modules/views/src/Form/ViewsForm.php @@ -14,6 +14,7 @@ use Drupal\Core\DependencyInjection\DependencySerializationTrait; 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 fe65fe36d2a..71b37c8bd75 100644 --- a/core/modules/views/src/ViewExecutable.php +++ b/core/modules/views/src/ViewExecutable.php @@ -21,6 +21,7 @@ use Drupal\views\ViewEntityInterface; 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 @@ class ViewExecutable implements \Serializable { 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 00000000000..f3f44641caf --- /dev/null +++ b/core/modules/views_ui/src/Tests/UnsavedPreviewTest.php @@ -0,0 +1,84 @@ +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 4c82de70844..8b5569e4d03 100644 --- a/core/modules/views_ui/src/ViewUI.php +++ b/core/modules/views_ui/src/ViewUI.php @@ -598,7 +598,7 @@ class ViewUI implements ViewEntityInterface { $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(); }