diff --git a/core/lib/Drupal/Core/KeyValueStore/KeyValueStoreInterface.php b/core/lib/Drupal/Core/KeyValueStore/KeyValueStoreInterface.php index 372ffc5..60c150c 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 236b0aa..e6e07ef 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 d66ab59..29bcde6 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 6b64398..4c54271 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 9bd2412..d146e03 100644 --- a/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/StorageTestBase.php +++ b/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/StorageTestBase.php @@ -152,6 +152,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);