Commit d9a72741 authored by webchick's avatar webchick

Issue #1867304 by dawehner, damiankloip: Assess newDisplay method on ViewExecutable.

parent d134c025
......@@ -94,6 +94,36 @@ public function remove($instance_id) {
unset($this->pluginInstances[$instance_id]);
}
/**
* Adds an instance ID to the array of available instance IDs.
*
* @param string $id
* The ID of the plugin instance to add.
*/
public function addInstanceID($id) {
$this->instanceIDs[$id] = $id;
}
/**
* Returns all instance IDs.
*
* @return array
* An array of all available instance IDs.
*/
public function getInstanceIDs() {
return $this->instanceIDs;
}
/**
* Sets the instance IDs property.
*
* @param array $instance_ids
* An associative array of instance IDs.
*/
public function setInstanceIDs(array $instance_ids) {
$this->instanceIDs = $instance_ids;
}
/**
* Implements \ArrayAccess::offsetExists().
*
......
......@@ -300,7 +300,7 @@ protected function generateDisplayId($plugin_id) {
}
/**
* Creates a new display and a display handler for it.
* 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
......@@ -311,12 +311,23 @@ protected function generateDisplayId($plugin_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.
* @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);
return $this->get('executable')->newDisplay($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->get('executable');
$executable->initDisplay();
$executable->displayHandlers->addInstanceID($id);
return $executable->displayHandlers[$id];
}
return $id;
}
/**
......
......@@ -692,6 +692,9 @@ protected function alter_display_options(&$display_options, $form, $form_state)
* Adds the array of display options to the view, with appropriate overrides.
*/
protected function addDisplays(View $view, $display_options, $form, $form_state) {
// Initialize the view executable to get the display plugin instances.
$view->get('executable');
// Display: Master
$default_display = $view->newDisplay('default', 'Master', 'default');
foreach ($display_options['default'] as $option => $value) {
......
......@@ -189,13 +189,15 @@ protected function displayTests() {
// Check whether a display can be added and saved to a View.
$view = $this->loadView('frontpage');
$view->newDisplay('page', 'Test', 'test');
$new_display = $view->get('display');
$new_id = $view->newDisplay('page', 'Test', 'test');
$display = $view->get('display');
// Ensure the right display_plugin is created/instantiated.
$this->assertEqual($new_display['test']['display_plugin'], 'page', 'New page display "test" uses the right display plugin.');
$this->assertTrue($view->get('executable')->displayHandlers[$new_display['test']['id']] instanceof Page, 'New page display "test" uses the right display plugin.');
$this->assertEqual($display[$new_id]['display_plugin'], 'page', 'New page display "test" uses the right display plugin.');
$executable = $view->get('executable');
$executable->initDisplay();
$this->assertTrue($executable->displayHandlers[$new_id] instanceof Page, 'New page display "test" uses the right display plugin.');
$view->set('name', 'frontpage_new');
......@@ -316,19 +318,21 @@ protected function displayMethodTests() {
$view->newDisplay('default');
$display = $view->newDisplay('page');
$this->assertTrue($display instanceof Page);
$this->assertTrue($view->get('executable')->displayHandlers['page_1'] instanceof Page);
$this->assertTrue($view->get('executable')->displayHandlers['page_1']->default_display instanceof DefaultDisplay);
$this->assertEqual($display, 'page_1');
$display = $view->newDisplay('page');
$this->assertTrue($display instanceof Page);
$this->assertTrue($view->get('executable')->displayHandlers['page_2'] instanceof Page);
$this->assertTrue($view->get('executable')->displayHandlers['page_2']->default_display instanceof DefaultDisplay);
$this->assertEqual($display, 'page_2');
$display = $view->newDisplay('feed');
$this->assertTrue($display instanceof Feed);
$this->assertTrue($view->get('executable')->displayHandlers['feed_1'] instanceof Feed);
$this->assertTrue($view->get('executable')->displayHandlers['feed_1']->default_display instanceof DefaultDisplay);
$this->assertEqual($display, 'feed_1');
$executable = $view->get('executable');
$executable->initDisplay();
$this->assertTrue($executable->displayHandlers['page_1'] instanceof Page);
$this->assertTrue($executable->displayHandlers['page_1']->default_display instanceof DefaultDisplay);
$this->assertTrue($executable->displayHandlers['page_2'] instanceof Page);
$this->assertTrue($executable->displayHandlers['page_2']->default_display instanceof DefaultDisplay);
$this->assertTrue($executable->displayHandlers['feed_1'] instanceof Feed);
$this->assertTrue($executable->displayHandlers['feed_1']->default_display instanceof DefaultDisplay);
// Tests item related methods().
$view = $this->controller->create(array('base_table' => 'views_test_data'));
......
......@@ -2174,38 +2174,4 @@ public function setItemOption($display_id, $type, $id, $option, $value) {
$this->setItem($display_id, $type, $id, $item);
}
/**
* Creates and stores a new display.
*
* @param string $id
* The ID for the display being added.
*
* @return \Drupal\views\Plugin\views\display\DisplayPluginBase
* A new display plugin instance.
*/
public function newDisplay($id) {
// Create a handler.
$display = $this->storage->get('display');
$manager = drupal_container()->get("plugin.manager.views.display");
$this->displayHandlers[$id] = $manager->createInstance($display[$id]['display_plugin']);
if (empty($this->displayHandlers[$id])) {
// provide a 'default' handler as an emergency. This won't work well but
// it will keep things from crashing.
$this->displayHandlers[$id] = $manager->createInstance('default');
}
if (!empty($this->displayHandlers[$id])) {
// Initialize the new display handler with data.
$this->displayHandlers[$id]->initDisplay($this, $display[$id]);
// If this is NOT the default display handler, let it know which is
if ($id != 'default') {
// @todo is the '&' still required in php5?
$this->displayHandlers[$id]->default_display = &$this->displayHandlers['default'];
}
}
$this->storage->set('display', $display);
return $this->displayHandlers[$id];
}
}
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