From 9204b87f3a21d5fb2fbaa791cf990cb3066bda63 Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Thu, 27 Sep 2018 22:51:35 +0300 Subject: [PATCH] Hide form fields in blueprints by using dynamic property like `scope@: object` or `scope-ignore@: object`, right now only Flex sets the scope --- CHANGELOG.md | 2 +- system/src/Grav/Common/Data/Blueprint.php | 30 +++++++++++++++++++ .../src/Grav/Framework/Flex/FlexDirectory.php | 1 + 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 02bd6598d..2a27cc34f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +16,7 @@ * Added `Grav\Framework\Collection\AbstractIndexCollection` class * Added `Grav\Framework\Object\ObjectIndex` class * Added `Grav\Framework\Flex` classes - * Added proper support for hiding form fields in blueprints by using dynamic property like `security@: admin.foobar` to any field + * Added support for hiding form fields in blueprints by using dynamic property like `security@: admin.foobar`, `scope@: object` or `scope-ignore@: object` to any field 1. [](#improved) * Doctrine filecache is now namespaced with prefix to support purging diff --git a/system/src/Grav/Common/Data/Blueprint.php b/system/src/Grav/Common/Data/Blueprint.php index b48581b6f..789cfe67f 100644 --- a/system/src/Grav/Common/Data/Blueprint.php +++ b/system/src/Grav/Common/Data/Blueprint.php @@ -19,9 +19,16 @@ class Blueprint extends BlueprintForm /** @var string */ protected $context = 'blueprints://'; + protected $scope; + /** @var BlueprintSchema */ protected $blueprintSchema; + public function setScope($scope) + { + $this->scope = $scope; + } + /** * Set default values for field types. * @@ -280,6 +287,29 @@ class Blueprint extends BlueprintForm } } + /** + * @param array $field + * @param string $property + * @param array $call + */ + protected function dynamicScope(array &$field, $property, array &$call) + { + if ($property && $property !== 'ignore') { + return; + } + + $scopes = (array)$call['params']; + $matches = \in_array($this->scope, $scopes, true); + if ($this->scope && $property !== 'ignore') { + $matches = !$matches; + } + + if ($matches) { + $this->addPropertyRecursive($field, 'validate', ['ignore' => true]); + return; + } + } + protected function addPropertyRecursive(array &$field, $property, $value) { if (\is_array($value) && isset($field[$property]) && \is_array($field[$property])) { diff --git a/system/src/Grav/Framework/Flex/FlexDirectory.php b/system/src/Grav/Framework/Flex/FlexDirectory.php index b354ff8c6..ada6ffec5 100644 --- a/system/src/Grav/Framework/Flex/FlexDirectory.php +++ b/system/src/Grav/Framework/Flex/FlexDirectory.php @@ -134,6 +134,7 @@ class FlexDirectory implements FlexAuthorizeInterface if (empty($this->blueprints_init[$type])) { $this->blueprints_init[$type] = true; + $blueprint->setScope('object'); $blueprint->init(); if (empty($blueprint->fields())) { throw new RuntimeException(sprintf('Flex: Blueprint for %s is missing', $this->type));