From d9a727416ef9b983241d736410d33b724d9e6500 Mon Sep 17 00:00:00 2001
From: webchick <webchick@24967.no-reply.drupal.org>
Date: Wed, 9 Jan 2013 11:12:46 -0800
Subject: [PATCH] Issue #1867304 by dawehner, damiankloip: Assess newDisplay
 method on ViewExecutable.

---
 .../lib/Drupal/Component/Plugin/PluginBag.php | 30 ++++++++++++++++
 .../Drupal/views/Plugin/Core/Entity/View.php  | 19 +++++++---
 .../Plugin/views/wizard/WizardPluginBase.php  |  3 ++
 .../Drupal/views/Tests/ViewStorageTest.php    | 36 ++++++++++---------
 .../views/lib/Drupal/views/ViewExecutable.php | 34 ------------------
 5 files changed, 68 insertions(+), 54 deletions(-)

diff --git a/core/lib/Drupal/Component/Plugin/PluginBag.php b/core/lib/Drupal/Component/Plugin/PluginBag.php
index 54718be6fbaa..50df14ad5e31 100644
--- a/core/lib/Drupal/Component/Plugin/PluginBag.php
+++ b/core/lib/Drupal/Component/Plugin/PluginBag.php
@@ -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().
    *
diff --git a/core/modules/views/lib/Drupal/views/Plugin/Core/Entity/View.php b/core/modules/views/lib/Drupal/views/Plugin/Core/Entity/View.php
index 683795299ead..e1e61a1b36b5 100644
--- a/core/modules/views/lib/Drupal/views/Plugin/Core/Entity/View.php
+++ b/core/modules/views/lib/Drupal/views/Plugin/Core/Entity/View.php
@@ -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;
   }
 
   /**
diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/wizard/WizardPluginBase.php b/core/modules/views/lib/Drupal/views/Plugin/views/wizard/WizardPluginBase.php
index d7c35a5289b7..7df19f3b27a7 100644
--- a/core/modules/views/lib/Drupal/views/Plugin/views/wizard/WizardPluginBase.php
+++ b/core/modules/views/lib/Drupal/views/Plugin/views/wizard/WizardPluginBase.php
@@ -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) {
diff --git a/core/modules/views/lib/Drupal/views/Tests/ViewStorageTest.php b/core/modules/views/lib/Drupal/views/Tests/ViewStorageTest.php
index e7ac8f0191ec..4bfad2cd1ca6 100644
--- a/core/modules/views/lib/Drupal/views/Tests/ViewStorageTest.php
+++ b/core/modules/views/lib/Drupal/views/Tests/ViewStorageTest.php
@@ -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'));
diff --git a/core/modules/views/lib/Drupal/views/ViewExecutable.php b/core/modules/views/lib/Drupal/views/ViewExecutable.php
index a6a55a399afc..0c22e3b0e492 100644
--- a/core/modules/views/lib/Drupal/views/ViewExecutable.php
+++ b/core/modules/views/lib/Drupal/views/ViewExecutable.php
@@ -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];
-  }
-
 }
-- 
GitLab