From 97969786b3b8dc12d900e4f6b9445ad0613a3daf Mon Sep 17 00:00:00 2001
From: webchick <webchick@24967.no-reply.drupal.org>
Date: Sat, 8 Dec 2012 15:21:07 -0800
Subject: [PATCH] Issue #1858054 by tim.plunkett, dawehner: Remove
 ViewExecutable::cloneView().

---
 .../views/Plugin/views/display/Attachment.php |  7 ++---
 .../views/display/DisplayPluginBase.php       |  2 +-
 .../views/Plugin/views/display/Feed.php       |  3 +--
 .../views/lib/Drupal/views/ViewExecutable.php | 19 +++-----------
 core/modules/views/views.module               | 26 +++++--------------
 core/modules/views/views_ui/admin.inc         |  3 +--
 6 files changed, 14 insertions(+), 46 deletions(-)

diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/display/Attachment.php b/core/modules/views/lib/Drupal/views/Plugin/views/display/Attachment.php
index a0f6a2fb03b6..1e5e0e156802 100644
--- a/core/modules/views/lib/Drupal/views/Plugin/views/display/Attachment.php
+++ b/core/modules/views/lib/Drupal/views/Plugin/views/display/Attachment.php
@@ -7,6 +7,7 @@
 
 namespace Drupal\views\Plugin\views\display;
 
+use Drupal\views\ViewExecutable;
 use Drupal\Core\Annotation\Plugin;
 use Drupal\Core\Annotation\Translation;
 
@@ -230,7 +231,7 @@ public function submitOptionsForm(&$form, &$form_state) {
   /**
    * Attach to another view.
    */
-  public function attachTo($display_id) {
+  public function attachTo(ViewExecutable $view, $display_id) {
     $displays = $this->getOption('displays');
 
     if (empty($displays[$display_id])) {
@@ -241,10 +242,6 @@ public function attachTo($display_id) {
       return;
     }
 
-    // Get a fresh view because our current one has a lot of stuff on it because it's
-    // already been executed.
-    $view = $this->view->cloneView();
-
     $args = $this->getOption('inherit_arguments') ? $this->view->args : array();
     $view->setArguments($args);
     $view->setDisplay($this->display['id']);
diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/display/DisplayPluginBase.php b/core/modules/views/lib/Drupal/views/Plugin/views/display/DisplayPluginBase.php
index fa16cbeb5257..16dafd2daf07 100644
--- a/core/modules/views/lib/Drupal/views/Plugin/views/display/DisplayPluginBase.php
+++ b/core/modules/views/lib/Drupal/views/Plugin/views/display/DisplayPluginBase.php
@@ -380,7 +380,7 @@ public function usesAreas() {
   /**
    * Allow displays to attach to other views.
    */
-  public function attachTo($display_id) { }
+  public function attachTo(ViewExecutable $view, $display_id) { }
 
   /**
    * Static member function to list which sections are defaultable
diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/display/Feed.php b/core/modules/views/lib/Drupal/views/Plugin/views/display/Feed.php
index 7c89f900d1c5..b2a02519eb83 100644
--- a/core/modules/views/lib/Drupal/views/Plugin/views/display/Feed.php
+++ b/core/modules/views/lib/Drupal/views/Plugin/views/display/Feed.php
@@ -244,7 +244,7 @@ public function submitOptionsForm(&$form, &$form_state) {
   /**
    * Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::attachTo().
    */
-  public function attachTo($display_id) {
+  public function attachTo(ViewExecutable $clone, $display_id) {
     $displays = $this->getOption('displays');
     if (empty($displays[$display_id])) {
       return;
@@ -254,7 +254,6 @@ public function attachTo($display_id) {
     // attach a feed icon.
     $plugin = $this->getPlugin('style');
     if ($plugin) {
-      $clone = $this->view->cloneView();
       $clone->setDisplay($this->display['id']);
       $clone->buildTitle();
       $plugin->attach_to($display_id, $this->getPath(), $clone->getTitle());
diff --git a/core/modules/views/lib/Drupal/views/ViewExecutable.php b/core/modules/views/lib/Drupal/views/ViewExecutable.php
index f2fe9b21044a..f19b7db4a282 100644
--- a/core/modules/views/lib/Drupal/views/ViewExecutable.php
+++ b/core/modules/views/lib/Drupal/views/ViewExecutable.php
@@ -1492,7 +1492,9 @@ public function attachDisplays() {
     // Give other displays an opportunity to attach to the view.
     foreach ($this->displayHandlers as $id => $display) {
       if (!empty($this->displayHandlers[$id])) {
-        $this->displayHandlers[$id]->attachTo($this->current_display);
+        // Create a clone for the attachments to manipulate. 'static' refers to the current class name.
+        $cloned_view = new static($this->storage);
+        $this->displayHandlers[$id]->attachTo($cloned_view, $this->current_display);
       }
     }
     $this->is_attachment = FALSE;
@@ -1831,21 +1833,6 @@ public function createDuplicate() {
     return entity_create('view', $data);
   }
 
-  /**
-   * Safely clone a view.
-   *
-   * This will completely wipe a view clean so it can be considered fresh.
-   *
-   * @return Drupal\views\ViewExecutable
-   *   The cloned view.
-   */
-  public function cloneView() {
-    $storage = clone $this->storage;
-    $executable = new ViewExecutable($storage);
-    $storage->set('executable', $executable);
-    return $executable;
-  }
-
   /**
    * Unset references so that a $view object may be properly garbage
    * collected.
diff --git a/core/modules/views/views.module b/core/modules/views/views.module
index 7223e53537b0..f1286d103935 100644
--- a/core/modules/views/views.module
+++ b/core/modules/views/views.module
@@ -479,11 +479,6 @@ function views_menu_alter(&$callbacks) {
         $our_paths[$path] = TRUE;
       }
     }
-  }
-
-  // Save memory: Destroy those views.
-  foreach ($views as $data) {
-    list($view, $display_id) = $data;
     $view->destroy();
   }
 }
@@ -1411,21 +1406,12 @@ function views_get_applicable_views($type) {
       continue;
     }
 
-    // Loop on array keys because something seems to muck with $view->display
-    // a bit in PHP4.
-    foreach (array_keys($display) as $id) {
-      $plugin = drupal_container()->get('plugin.manager.views.display')->getDefinition($display[$id]['display_plugin']);
-      if (!empty($plugin[$type])) {
-        // This view uses_hook_menu. Clone it so that different handlers
-        // don't trip over each other, and add it to the list.
-        $v = $view->get('executable')->cloneView();
-        if ($v->setDisplay($id) && $v->display_handler->isEnabled()) {
-          $result[] = array($v, $id);
-        }
-        // In PHP 4.4.7 and presumably earlier, if we do not unset $v
-        // here, we will find that it actually overwrites references
-        // possibly due to shallow copying issues.
-        unset($v);
+    // Check each display to see if it meets the criteria and is enabled.
+    $executable = $view->get('executable');
+    $executable->setDisplay();
+    foreach ($executable->displayHandlers as $id => $handler) {
+      if (!empty($handler->definition[$type]) && $handler->isEnabled()) {
+        $result[] = array($executable, $id);
       }
     }
   }
diff --git a/core/modules/views/views_ui/admin.inc b/core/modules/views/views_ui/admin.inc
index b5b46c7896cf..6e7cafd532b7 100644
--- a/core/modules/views/views_ui/admin.inc
+++ b/core/modules/views/views_ui/admin.inc
@@ -1460,8 +1460,7 @@ function views_ui_config_item_form($form, &$form_state) {
           // We want this relationship option to get saved even if the user
           // skips submitting the form.
           $executable->setItemOption($display_id, $type, $id, 'relationship', $rel);
-          $temp_view = $view->cloneView();
-          views_ui_cache_set($temp_view);
+          views_ui_cache_set($view);
         }
 
         $form['options']['relationship'] = array(
-- 
GitLab