From 956af788a72468caaf10c3f137606232c2edcd9b Mon Sep 17 00:00:00 2001
From: damiankloip <damiankloip@1037976.no-reply.drupal.org>
Date: Mon, 3 Sep 2012 10:47:02 +0200
Subject: [PATCH] Issue #1768392 by damiankloip, dawehner: Fixed Views do not
 create uuids for existing views or save any uuid.

---
 .../views/Tests/Handler/FilterDateTest.php      |  1 +
 lib/Drupal/views/Tests/Plugin/PagerTest.php     |  1 +
 lib/Drupal/views/Tests/TranslatableTest.php     |  1 +
 lib/Drupal/views/Tests/ViewStorageTest.php      |  9 +++++++++
 lib/Drupal/views/ViewStorageController.php      | 17 +++++++++++++++++
 5 files changed, 29 insertions(+)

diff --git a/lib/Drupal/views/Tests/Handler/FilterDateTest.php b/lib/Drupal/views/Tests/Handler/FilterDateTest.php
index b5c1ea19623f..1ab7cfb192b2 100644
--- a/lib/Drupal/views/Tests/Handler/FilterDateTest.php
+++ b/lib/Drupal/views/Tests/Handler/FilterDateTest.php
@@ -171,6 +171,7 @@ function views_test_between() {
     $view->core = 8;
     $view->api_version = '3.0';
     $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
+    $view->uuid = NULL;
 
     /* Display: Master */
     $handler = $view->newDisplay('default', 'Master', 'default');
diff --git a/lib/Drupal/views/Tests/Plugin/PagerTest.php b/lib/Drupal/views/Tests/Plugin/PagerTest.php
index 9e8602ace24a..01e51999bbd5 100644
--- a/lib/Drupal/views/Tests/Plugin/PagerTest.php
+++ b/lib/Drupal/views/Tests/Plugin/PagerTest.php
@@ -112,6 +112,7 @@ public function viewsStorePagerSettings() {
     $view->is_cacheable = FALSE;
     $view->api_version = '3.0';
     $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
+    $view->uuid = NULL;
 
     /* Display: Master */
     $handler = $view->newDisplay('default', 'Master', 'default');
diff --git a/lib/Drupal/views/Tests/TranslatableTest.php b/lib/Drupal/views/Tests/TranslatableTest.php
index 822b9df852ec..b8c9aab7ca93 100644
--- a/lib/Drupal/views/Tests/TranslatableTest.php
+++ b/lib/Drupal/views/Tests/TranslatableTest.php
@@ -150,6 +150,7 @@ public function view_unpack_translatable() {
     $view->base_table = 'node';
     $view->api_version = '3.0';
     $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
+    $view->uuid = NULL;
 
     /* Display: Master */
     $handler = $view->newDisplay('default', 'Master1', 'default');
diff --git a/lib/Drupal/views/Tests/ViewStorageTest.php b/lib/Drupal/views/Tests/ViewStorageTest.php
index 584612c3580b..7d3aee6346f7 100644
--- a/lib/Drupal/views/Tests/ViewStorageTest.php
+++ b/lib/Drupal/views/Tests/ViewStorageTest.php
@@ -130,6 +130,10 @@ protected function loadTests() {
 
     // Check that all of these machine names match.
     $this->assertIdentical(array_keys($all_configuration_entities), array_map($prefix_map, $all_config), 'All loaded elements match.');
+
+    // Make sure that loaded default views get a uuid.
+    $view = views_get_view('frontpage');
+    $this->assertTrue($view->uuid());
   }
 
   /**
@@ -166,6 +170,11 @@ protected function createTests() {
       $this->assertTrue($display instanceof ViewDisplay, format_string('Display @display is an instance of ViewDisplay.', array('@display' => $key)));
     }
 
+    // Check the uuid of the loaded View.
+    $created->set('name', 'glossary_new');
+    $created->save();
+    $created_loaded = $this->loadView('glossary_new');
+    $this->assertIdentical($created->uuid(), $created_loaded->uuid(), 'The created uuid has been saved correctly.');
   }
 
   /**
diff --git a/lib/Drupal/views/ViewStorageController.php b/lib/Drupal/views/ViewStorageController.php
index 5bdd1f8acb47..2bdb3a8ca4fe 100644
--- a/lib/Drupal/views/ViewStorageController.php
+++ b/lib/Drupal/views/ViewStorageController.php
@@ -9,17 +9,33 @@
 
 use Drupal\config\ConfigStorageController;
 use Drupal\entity\EntityInterface;
+use Drupal\Component\Uuid\Uuid;
 
 /**
  * Defines the storage controller class for ViewStorage entities.
  */
 class ViewStorageController extends ConfigStorageController {
 
+  /**
+   * Holds a UUID factory instance.
+   *
+   * @var Drupal\Component\Uuid\Uuid
+   */
+  protected $uuidFactory = NULL;
+
   /**
    * Overrides Drupal\config\ConfigStorageController::attachLoad();
    */
   protected function attachLoad(&$queried_entities, $revision_id = FALSE) {
     foreach ($queried_entities as $id => $entity) {
+      // Create a uuid if we don't have one.
+      if (empty($entity->{$this->uuidKey})) {
+        // Only get an instance of uuid once.
+        if (!isset($this->uuidFactory)) {
+          $this->uuidFactory = new Uuid();
+        }
+        $entity->{$this->uuidKey} = $this->uuidFactory->generate();
+      }
       // @todo This property is left in for CTools export UI.
       $entity->type = t('Normal');
       $this->attachDisplays($entity);
@@ -65,6 +81,7 @@ public function save(EntityInterface $entity) {
       'human_name',
       'core',
       'display',
+      'uuid',
     );
 
     foreach ($config_properties as $property) {
-- 
GitLab