From 4a3ee315a28318689b80c603ab8d397cfb182156 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Tue, 15 Oct 2013 13:27:28 +0100
Subject: [PATCH] Issue #2085867 by damiankloip: Add a default parameter to
 keyvalue get() method.

---
 .../Drupal/Core/KeyValueStore/KeyValueStoreInterface.php    | 6 ++++--
 core/lib/Drupal/Core/KeyValueStore/MemoryStorage.php        | 4 ++--
 core/lib/Drupal/Core/KeyValueStore/NullStorageExpirable.php | 2 +-
 core/lib/Drupal/Core/KeyValueStore/StorageBase.php          | 4 ++--
 .../Drupal/system/Tests/KeyValueStore/StorageTestBase.php   | 3 +++
 5 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/core/lib/Drupal/Core/KeyValueStore/KeyValueStoreInterface.php b/core/lib/Drupal/Core/KeyValueStore/KeyValueStoreInterface.php
index 372ffc519e37..60c150cb10d7 100644
--- a/core/lib/Drupal/Core/KeyValueStore/KeyValueStoreInterface.php
+++ b/core/lib/Drupal/Core/KeyValueStore/KeyValueStoreInterface.php
@@ -25,11 +25,13 @@ public function getCollectionName();
    *
    * @param string $key
    *   The key of the data to retrieve.
+   * @param mixed $default
+   *   The default value to use if the key is not found.
    *
    * @return mixed
-   *   The stored value, or NULL if no value exists.
+   *   The stored value, or the default value if no value exists.
    */
-  public function get($key);
+  public function get($key, $default = NULL);
 
   /**
    * Returns the stored key/value pairs for a given set of keys.
diff --git a/core/lib/Drupal/Core/KeyValueStore/MemoryStorage.php b/core/lib/Drupal/Core/KeyValueStore/MemoryStorage.php
index 236b0aadab42..e6e07ef7cb4c 100644
--- a/core/lib/Drupal/Core/KeyValueStore/MemoryStorage.php
+++ b/core/lib/Drupal/Core/KeyValueStore/MemoryStorage.php
@@ -22,8 +22,8 @@ class MemoryStorage extends StorageBase {
   /**
    * Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::get().
    */
-  public function get($key) {
-    return array_key_exists($key, $this->data) ? $this->data[$key] : NULL;
+  public function get($key, $default = NULL) {
+    return array_key_exists($key, $this->data) ? $this->data[$key] : $default;
   }
 
   /**
diff --git a/core/lib/Drupal/Core/KeyValueStore/NullStorageExpirable.php b/core/lib/Drupal/Core/KeyValueStore/NullStorageExpirable.php
index d66ab597b795..29bcde67999c 100644
--- a/core/lib/Drupal/Core/KeyValueStore/NullStorageExpirable.php
+++ b/core/lib/Drupal/Core/KeyValueStore/NullStorageExpirable.php
@@ -36,7 +36,7 @@ public function __construct($collection) {
   /**
    * Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::get().
    */
-  public function get($key) {
+  public function get($key, $default = NULL) {
     return NULL;
   }
 
diff --git a/core/lib/Drupal/Core/KeyValueStore/StorageBase.php b/core/lib/Drupal/Core/KeyValueStore/StorageBase.php
index 6b643983e95b..4c54271f0a3e 100644
--- a/core/lib/Drupal/Core/KeyValueStore/StorageBase.php
+++ b/core/lib/Drupal/Core/KeyValueStore/StorageBase.php
@@ -36,9 +36,9 @@ public function getCollectionName() {
   /**
    * Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::get().
    */
-  public function get($key) {
+  public function get($key, $default = NULL) {
     $values = $this->getMultiple(array($key));
-    return isset($values[$key]) ? $values[$key] : NULL;
+    return isset($values[$key]) ? $values[$key] : $default;
   }
 
   /**
diff --git a/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/StorageTestBase.php b/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/StorageTestBase.php
index 6ae1a86251bf..86e6ce82b257 100644
--- a/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/StorageTestBase.php
+++ b/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/StorageTestBase.php
@@ -157,6 +157,9 @@ public function testNonExistingKeys() {
     // Verify that a non-existing key returns NULL as value.
     $this->assertNull($stores[0]->get('foo'));
 
+    // Verify that a non-existing key with a default returns the default.
+    $this->assertIdentical($stores[0]->get('foo', 'bar'), 'bar');
+
     // Verify that a FALSE value can be stored.
     $stores[0]->set('foo', FALSE);
     $this->assertIdentical($stores[0]->get('foo'), FALSE);
-- 
GitLab