From 8343cfb27812753f2919226c51d75460116784da Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Sat, 18 Dec 2021 17:46:55 -0700 Subject: [PATCH] new Plugin::upstreamConfigVar --- CHANGELOG.md | 1 + system/src/Grav/Common/Plugin.php | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6dc12acd2..d422c7c62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * Added second parameter to `UserObject::isMyself()` method * Added `UserObject::$isAuthorizedCallable` to allow `$user->isAuthorized()` customization * Use secure session cookies in HTTPS by default (`system.session.secure_https: true`) + * Added new `Plugin::upstreamConfigVar()` function to access plugin specific functions for page overrides 2. [](#improved) * Upgraded vendor libs for PHP 8.1 compatibility * Upgraded to **composer v2.1.14** for PHP 8.1 compatibility diff --git a/system/src/Grav/Common/Plugin.php b/system/src/Grav/Common/Plugin.php index ac5a3c474..bd245d761 100644 --- a/system/src/Grav/Common/Plugin.php +++ b/system/src/Grav/Common/Plugin.php @@ -414,6 +414,30 @@ class Plugin implements EventSubscriberInterface, ArrayAccess return true; } + public static function upstreamConfigVar(string $plugin, string $var, PageInterface $page = null, $default = null) + { + if (Utils::isAdminPlugin()) { + $page = Grav::instance()['admin']->page() ?? null; + } else { + $page = $page ?? Grav::instance()['page'] ?? null; + } + + // Try to find var in the page headers + if ($page instanceof PageInterface && $page->exists()) { + // Loop over pages and look for header vars + while ($page && !$page->root()) { + $header = new Data((array)$page->header()); + $value = $header->get("$plugin.$var"); + if (isset($value)) { + return $value; + } + $page = $page->parent(); + } + } + + return Grav::instance()['config']->get("plugins.$plugin.$var", $default); + } + /** * Simpler getter for the plugin blueprint *