diff --git a/core/lib/Drupal/Core/KeyValueStore/KeyValueStoreInterface.php b/core/lib/Drupal/Core/KeyValueStore/KeyValueStoreInterface.php index 372ffc519e37c4fa8031955848382b2d479330a6..60c150cb10d7710cc059f947017f19e66bee6a80 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 236b0aadab422e1b05e7e31f67f5ef3e67aaccda..e6e07ef7cb4ce9a522dd081953be5128db83c432 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 d66ab597b7953f0299686d6ba256b1be4ce7656a..29bcde67999cb4eceb99ae68639c72c98b1b2273 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 6b643983e95b0c53fad8961ac30c609359cd4e25..4c54271f0a3ea64c36a204ca758bb52fb8dd4d42 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 6ae1a86251bf76b6e2d804cceea4be846efce9d4..86e6ce82b257c90c24a65047e919d3d545beeecf 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);