Commit d72692c6 authored by webchick's avatar webchick
Browse files

Issue #2082157 by damiankloip: Move View::newDisplay() to ViewExecutable.

parent c9b99fc6
......@@ -255,37 +255,6 @@ protected function generateDisplayId($plugin_id) {
return $id;
}
/**
* Creates a new display and a display handler instance for it.
*
* @param string $plugin_id
* (optional) The plugin type from the Views plugin annotation. Defaults to
* 'page'.
* @param string $title
* (optional) The title of the display. Defaults to NULL.
* @param string $id
* (optional) The ID to use, e.g., 'default', 'page_1', 'block_2'. Defaults
* to NULL.
*
* @return string|\Drupal\views\Plugin\views\display\DisplayPluginBase
* A new display plugin instance if executable is set, the new display ID
* otherwise.
*/
public function newDisplay($plugin_id = 'page', $title = NULL, $id = NULL) {
$id = $this->addDisplay($plugin_id, $title, $id);
// We can't use get() here as it will create an ViewExecutable instance if
// there is not already one.
if (isset($this->executable)) {
$executable = $this->getExecutable();
$executable->initDisplay();
$executable->displayHandlers->addInstanceID($id);
return $executable->displayHandlers->get($id);
}
return $id;
}
/**
* {@inheritdoc}
*/
......
......@@ -697,14 +697,14 @@ protected function addDisplays(View $view, $display_options, $form, $form_state)
$executable = $view->getExecutable();
// Display: Master
$default_display = $view->newDisplay('default', 'Master', 'default');
$default_display = $executable->newDisplay('default', 'Master', 'default');
foreach ($display_options['default'] as $option => $value) {
$default_display->setOption($option, $value);
}
// Display: Page
if (isset($display_options['page'])) {
$display = $view->newDisplay('page', 'Page', 'page_1');
$display = $executable->newDisplay('page', 'Page', 'page_1');
// The page display is usually the main one (from the user's point of
// view). Its options should therefore become the overall view defaults,
// so that new displays which are added later automatically inherit them.
......@@ -712,14 +712,14 @@ protected function addDisplays(View $view, $display_options, $form, $form_state)
// Display: Feed (attached to the page).
if (isset($display_options['feed'])) {
$display = $view->newDisplay('feed', 'Feed', 'feed_1');
$display = $executable->newDisplay('feed', 'Feed', 'feed_1');
$this->setOverrideOptions($display_options['feed'], $display, $default_display);
}
}
// Display: Block.
if (isset($display_options['block'])) {
$display = $view->newDisplay('block', 'Block', 'block_1');
$display = $executable->newDisplay('block', 'Block', 'block_1');
// When there is no page, the block display options should become the
// overall view defaults.
if (!isset($display_options['page'])) {
......
......@@ -75,7 +75,7 @@ function testEqual() {
public function testEqualGroupedExposed() {
$filters = $this->getGroupedExposedFilters();
$view = views_get_view('test_view');
$view->storage->newDisplay('page', 'Page', 'page_1');
$view->newDisplay('page', 'Page', 'page_1');
// Filter: Name, Operator: =, Value: Ringo
$filters['name']['group_info']['default_group'] = 1;
......@@ -128,7 +128,7 @@ function testNotEqual() {
public function testEqualGroupedNotExposed() {
$filters = $this->getGroupedExposedFilters();
$view = views_get_view('test_view');
$view->storage->newDisplay('page', 'Page', 'page_1');
$view->newDisplay('page', 'Page', 'page_1');
// Filter: Name, Operator: !=, Value: Ringo
$filters['name']['group_info']['default_group'] = 2;
......
......@@ -79,7 +79,7 @@ public function testFilterNumericSimple() {
public function testFilterNumericExposedGroupedSimple() {
$filters = $this->getGroupedExposedFilters();
$view = views_get_view('test_view');
$view->storage->newDisplay('page', 'Page', 'page_1');
$view->newDisplay('page', 'Page', 'page_1');
// Filter: Age, Operator: =, Value: 28
$filters['age']['group_info']['default_group'] = 1;
......@@ -172,7 +172,7 @@ public function testFilterNumericBetween() {
public function testFilterNumericExposedGroupedBetween() {
$filters = $this->getGroupedExposedFilters();
$view = views_get_view('test_view');
$view->storage->newDisplay('page', 'Page', 'page_1');
$view->newDisplay('page', 'Page', 'page_1');
// Filter: Age, Operator: between, Value: 26 and 29
$filters['age']['group_info']['default_group'] = 2;
......@@ -200,7 +200,7 @@ public function testFilterNumericExposedGroupedBetween() {
public function testFilterNumericExposedGroupedNotBetween() {
$filters = $this->getGroupedExposedFilters();
$view = views_get_view('test_view');
$view->storage->newDisplay('page', 'Page', 'page_1');
$view->newDisplay('page', 'Page', 'page_1');
// Filter: Age, Operator: between, Value: 26 and 29
$filters['age']['group_info']['default_group'] = 3;
......@@ -288,7 +288,7 @@ public function testFilterNumericEmpty() {
public function testFilterNumericExposedGroupedEmpty() {
$filters = $this->getGroupedExposedFilters();
$view = views_get_view('test_view');
$view->storage->newDisplay('page', 'Page', 'page_1');
$view->newDisplay('page', 'Page', 'page_1');
// Filter: Age, Operator: empty, Value:
$filters['age']['group_info']['default_group'] = 4;
......@@ -304,7 +304,7 @@ public function testFilterNumericExposedGroupedEmpty() {
public function testFilterNumericExposedGroupedNotEmpty() {
$filters = $this->getGroupedExposedFilters();
$view = views_get_view('test_view');
$view->storage->newDisplay('page', 'Page', 'page_1');
$view->newDisplay('page', 'Page', 'page_1');
// Filter: Age, Operator: empty, Value:
$filters['age']['group_info']['default_group'] = 5;
......
......@@ -88,7 +88,7 @@ protected function getBasicPageView() {
// the exposed forms cache.
drupal_static_reset('views_exposed_form_cache');
$view->storage->newDisplay('page', 'Page', 'page_1');
$view->newDisplay('page', 'Page', 'page_1');
return $view;
}
......
......@@ -7,7 +7,6 @@
namespace Drupal\views\Tests;
use Symfony\Component\HttpFoundation\Response;
use Drupal\views\ViewExecutable;
use Drupal\views\ViewExecutableFactory;
use Drupal\views\DisplayBag;
......@@ -19,6 +18,8 @@
use Drupal\views\Plugin\views\query\Sql;
use Drupal\views\Plugin\views\pager\PagerPluginBase;
use Drupal\views\Plugin\views\query\QueryPluginBase;
use Drupal\views_test_data\Plugin\views\display\DisplayTest;
use Symfony\Component\HttpFoundation\Response;
/**
* Tests the ViewExecutable class.
......@@ -244,6 +245,23 @@ public function testDisplays() {
$this->assertTrue($view->style_plugin instanceof Grid);
// @todo Change this rowPlugin type too.
$this->assertTrue($view->rowPlugin instanceof Fields);
// Test the newDisplay() method.
$view = $this->container->get('entity.manager')->getStorageController('view')->create(array('id' => 'test_executable_displays'));
$executable = $view->getExecutable();
$executable->newDisplay('page');
$executable->newDisplay('page');
$executable->newDisplay('display_test');
$this->assertTrue($executable->displayHandlers->get('default') instanceof DefaultDisplay);
$this->assertFalse(isset($executable->displayHandlers->get('default')->default_display));
$this->assertTrue($executable->displayHandlers->get('page_1') instanceof Page);
$this->assertTrue($executable->displayHandlers->get('page_1')->default_display instanceof DefaultDisplay);
$this->assertTrue($executable->displayHandlers->get('page_2') instanceof Page);
$this->assertTrue($executable->displayHandlers->get('page_2')->default_display instanceof DefaultDisplay);
$this->assertTrue($executable->displayHandlers->get('display_test_1') instanceof DisplayTest);
$this->assertTrue($executable->displayHandlers->get('display_test_1')->default_display instanceof DefaultDisplay);
}
/**
......
......@@ -169,7 +169,7 @@ protected function displayTests() {
// Check whether a display can be added and saved to a View.
$view = entity_load('view', 'test_view_storage_new');
$new_id = $view->newDisplay('page', 'Test', 'test');
$new_id = $view->addDisplay('page', 'Test', 'test');
$display = $view->get('display');
// Ensure the right display_plugin is created/instantiated.
......@@ -248,27 +248,6 @@ protected function displayMethodTests() {
// $view->addDisplay('feed', 'feed title');
// $this->assertEqual($view->generateDisplayId('feed'), 'feed_2', 'The generated ID for the first instance of a plugin type should have an suffix of _2.');
// Tests Drupal\views\Entity\View::newDisplay().
$view = $this->controller->create(array());
$view->newDisplay('default');
$display = $view->newDisplay('page');
$this->assertEqual($display, 'page_1');
$display = $view->newDisplay('page');
$this->assertEqual($display, 'page_2');
$display = $view->newDisplay('feed');
$this->assertEqual($display, 'feed_1');
$executable = $view->getExecutable();
$executable->initDisplay();
$this->assertTrue($executable->displayHandlers->get('page_1') instanceof Page);
$this->assertTrue($executable->displayHandlers->get('page_1')->default_display instanceof DefaultDisplay);
$this->assertTrue($executable->displayHandlers->get('page_2') instanceof Page);
$this->assertTrue($executable->displayHandlers->get('page_2')->default_display instanceof DefaultDisplay);
$this->assertTrue($executable->displayHandlers->get('feed_1') instanceof Feed);
$this->assertTrue($executable->displayHandlers->get('feed_1')->default_display instanceof DefaultDisplay);
// Tests item related methods().
$view = $this->controller->create(array('base_table' => 'views_test_data'));
$view->addDisplay('default');
......
......@@ -700,6 +700,31 @@ public function setDisplay($display_id = NULL) {
return FALSE;
}
/**
* Creates a new display and a display handler instance for it.
*
* @param string $plugin_id
* (optional) The plugin type from the Views plugin annotation. Defaults to
* 'page'.
* @param string $title
* (optional) The title of the display. Defaults to NULL.
* @param string $id
* (optional) The ID to use, e.g., 'default', 'page_1', 'block_2'. Defaults
* to NULL.
*
* @return \Drupal\views\Plugin\views\display\DisplayPluginBase
* A new display plugin instance if executable is set, the new display ID
* otherwise.
*/
public function newDisplay($plugin_id = 'page', $title = NULL, $id = NULL) {
$this->initDisplay();
$id = $this->storage->addDisplay($plugin_id, $title, $id);
$this->displayHandlers->addInstanceID($id);
return $this->displayHandlers->get($id);
}
/**
* Gets the current style plugin.
*
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment