Skip to content
Snippets Groups Projects
Commit 4a3ee315 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2085867 by damiankloip: Add a default parameter to keyvalue get() method.

parent 2977e2ab
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -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.
......
......@@ -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;
}
/**
......
......@@ -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;
}
......
......@@ -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;
}
/**
......
......@@ -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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment