From fff9fa0ca5090a45985c433824ca36e7ebc1c663 Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Wed, 22 Jan 2020 10:13:40 +0200 Subject: [PATCH] Added `Blueprint::getDefaultValue()` method --- system/src/Grav/Common/Data/Blueprint.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/system/src/Grav/Common/Data/Blueprint.php b/system/src/Grav/Common/Data/Blueprint.php index c58e8e974..bb4503c98 100644 --- a/system/src/Grav/Common/Data/Blueprint.php +++ b/system/src/Grav/Common/Data/Blueprint.php @@ -73,6 +73,29 @@ class Blueprint extends BlueprintForm return $this; } + /** + * @param string $name + * @return array|mixed|null + * @since 1.7 + */ + public function getDefaultValue(string $name) + { + $path = explode('.', $name) ?: []; + $current = $this->getDefaults(); + + foreach ($path as $field) { + if (\is_object($current) && isset($current->{$field})) { + $current = $current->{$field}; + } elseif (\is_array($current) && isset($current[$field])) { + $current = $current[$field]; + } else { + return null; + } + } + + return $current; + } + /** * Get nested structure containing default values defined in the blueprints. *