diff --git a/core/lib/Drupal/Core/Config/Config.php b/core/lib/Drupal/Core/Config/Config.php index 02afbd1..1e180cd 100644 --- a/core/lib/Drupal/Core/Config/Config.php +++ b/core/lib/Drupal/Core/Config/Config.php @@ -207,6 +207,8 @@ public function isNew() { * A key of 'foo.bar' would return the string 'baz'. However, a key of 'foo' * would return array('bar' => 'baz'). * If no key is specified, then the entire data array is returned. + * @param mixed $default + * (optional) A default value to return if no value is found. * * The configuration system does not retain data types. Every saved value is * casted to a string. In most cases this is not an issue; however, it can @@ -218,7 +220,7 @@ public function isNew() { * @return mixed * The data that was requested. */ - public function get($key = '') { + public function get($key = '', $default = NULL) { if (!$this->isLoaded) { $this->load(); } @@ -231,11 +233,11 @@ public function get($key = '') { else { $parts = explode('.', $key); if (count($parts) == 1) { - return isset($this->overriddenData[$key]) ? $this->overriddenData[$key] : NULL; + return isset($this->overriddenData[$key]) ? $this->overriddenData[$key] : $default; } else { $value = NestedArray::getValue($this->overriddenData, $parts, $key_exists); - return $key_exists ? $value : NULL; + return $key_exists ? $value : $default; } } } diff --git a/core/modules/config/lib/Drupal/config/Tests/ConfigCRUDTest.php b/core/modules/config/lib/Drupal/config/Tests/ConfigCRUDTest.php index 363b889..05d38b6 100644 --- a/core/modules/config/lib/Drupal/config/Tests/ConfigCRUDTest.php +++ b/core/modules/config/lib/Drupal/config/Tests/ConfigCRUDTest.php @@ -40,6 +40,13 @@ function testCRUD() { $config = \Drupal::config($name); $this->assertIdentical($config->isNew(), TRUE); + // Verify the correct default values are returned for non-existent keys. + $this->assertIdentical($config->get('non-existent'), NULL); + $this->assertIdentical($config->get('non-existent', 'default'), 'default'); + // A call to get with no key should still return an array() as NULL default + // will not be used. + $this->assertIdentical($config->get(), array()); + // Create a new configuration object. $config->set('value', 'initial'); $config->save();