mirror of
https://github.com/getgrav/grav.git
synced 2025-10-26 07:56:07 +01:00
BlueprintForm: Add support for dynamic fields
This commit is contained in:
18
composer.lock
generated
18
composer.lock
generated
@@ -366,16 +366,16 @@
|
||||
},
|
||||
{
|
||||
"name": "maximebf/debugbar",
|
||||
"version": "v1.11.0",
|
||||
"version": "v1.11.1",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/maximebf/php-debugbar.git",
|
||||
"reference": "07741d84d39d10f00551c94284cdefcc69703e77"
|
||||
"reference": "d9302891c1f0a0ac5a4f66725163a00537c6359f"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/maximebf/php-debugbar/zipball/07741d84d39d10f00551c94284cdefcc69703e77",
|
||||
"reference": "07741d84d39d10f00551c94284cdefcc69703e77",
|
||||
"url": "https://api.github.com/repos/maximebf/php-debugbar/zipball/d9302891c1f0a0ac5a4f66725163a00537c6359f",
|
||||
"reference": "d9302891c1f0a0ac5a4f66725163a00537c6359f",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
@@ -423,7 +423,7 @@
|
||||
"debug",
|
||||
"debugbar"
|
||||
],
|
||||
"time": "2015-12-10 09:50:24"
|
||||
"time": "2016-01-22 12:22:23"
|
||||
},
|
||||
{
|
||||
"name": "monolog/monolog",
|
||||
@@ -637,12 +637,12 @@
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/rockettheme/toolbox.git",
|
||||
"reference": "ceb00d34e24bf137e1fc368411d1a94497d991c8"
|
||||
"reference": "cfb20f0396f1dc8e556cdf0e785f311e10742eee"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/rockettheme/toolbox/zipball/ceb00d34e24bf137e1fc368411d1a94497d991c8",
|
||||
"reference": "ceb00d34e24bf137e1fc368411d1a94497d991c8",
|
||||
"url": "https://api.github.com/repos/rockettheme/toolbox/zipball/cfb20f0396f1dc8e556cdf0e785f311e10742eee",
|
||||
"reference": "cfb20f0396f1dc8e556cdf0e785f311e10742eee",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
@@ -677,7 +677,7 @@
|
||||
"php",
|
||||
"rockettheme"
|
||||
],
|
||||
"time": "2016-02-18 13:35:02"
|
||||
"time": "2016-02-25 18:01:13"
|
||||
},
|
||||
{
|
||||
"name": "symfony/console",
|
||||
|
||||
@@ -160,4 +160,60 @@ class Blueprint extends BlueprintForm
|
||||
|
||||
return $files;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $field
|
||||
* @param string $property
|
||||
* @param array $call
|
||||
*/
|
||||
protected function dynamicData(array &$field, $property, array &$call)
|
||||
{
|
||||
$params = $call['params'];
|
||||
|
||||
if (is_array($params)) {
|
||||
$function = array_shift($params);
|
||||
} else {
|
||||
$function = $params;
|
||||
$params = [];
|
||||
}
|
||||
|
||||
list($o, $f) = preg_split('/::/', $function, 2);
|
||||
if (!$f) {
|
||||
if (function_exists($o)) {
|
||||
$data = call_user_func_array($o, $params);
|
||||
}
|
||||
} else {
|
||||
if (method_exists($o, $f)) {
|
||||
$data = call_user_func_array(array($o, $f), $params);
|
||||
}
|
||||
}
|
||||
|
||||
// If function returns a value,
|
||||
if (isset($data)) {
|
||||
if (isset($field[$property]) && is_array($field[$property]) && is_array($data)) {
|
||||
// Combine field and @data-field together.
|
||||
$field[$property] += $data;
|
||||
} else {
|
||||
// Or create/replace field with @data-field.
|
||||
$field[$property] = $data;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $field
|
||||
* @param string $property
|
||||
* @param array $call
|
||||
*/
|
||||
protected function dynamicConfig(array &$field, $property, array &$call)
|
||||
{
|
||||
$value = $call['params'];
|
||||
|
||||
$default = isset($field[$property]) ? $field[$property] : null;
|
||||
$config = Grav::instance()['config']->get($value, $default);
|
||||
|
||||
if (!is_null($config)) {
|
||||
$field[$property] = $config;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,6 +34,10 @@ abstract class BlueprintForm implements \ArrayAccess, ExportInterface
|
||||
*/
|
||||
protected $overrides = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $dynamic = [];
|
||||
|
||||
/**
|
||||
* Load file and return its contents.
|
||||
@@ -112,23 +116,55 @@ abstract class BlueprintForm implements \ArrayAccess, ExportInterface
|
||||
}
|
||||
}
|
||||
|
||||
// Finally initialize blueprint.
|
||||
return $this->init();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize blueprint.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
// Import blueprints.
|
||||
$this->deepInit($this->items);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize blueprints with its dynamic fields.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
foreach ($this->dynamic as $key => $data) {
|
||||
// Locate field.
|
||||
$path = explode('/', $key);
|
||||
$current = &$this->items;
|
||||
foreach ($path as $field) {
|
||||
if (is_object($current)) {
|
||||
// Handle objects.
|
||||
if (!isset($current->{$field})) {
|
||||
$current->{$field} = array();
|
||||
}
|
||||
$current = &$current->{$field};
|
||||
} else {
|
||||
// Handle arrays and scalars.
|
||||
if (!is_array($current)) {
|
||||
$current = array($field => array());
|
||||
} elseif (!isset($current[$field])) {
|
||||
$current[$field] = array();
|
||||
}
|
||||
$current = &$current[$field];
|
||||
}
|
||||
}
|
||||
|
||||
// Set dynamic property.
|
||||
foreach ($data as $property => $call) {
|
||||
$action = 'dynamic' . ucfirst($call['action']);
|
||||
|
||||
if (method_exists($this, $action)) {
|
||||
$this->{$action}($current, $property, $call);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get form.
|
||||
*
|
||||
@@ -304,6 +340,12 @@ abstract class BlueprintForm implements \ArrayAccess, ExportInterface
|
||||
$ordering = $item;
|
||||
unset($items[$key]);
|
||||
break;
|
||||
default:
|
||||
$list = explode('-', trim($name, '@'), 2);
|
||||
$action = array_shift($list);
|
||||
$property = array_shift($list);
|
||||
|
||||
$this->dynamic[implode('/', $path)][$property] = ['action' => $action, 'params' => $item];
|
||||
}
|
||||
|
||||
} elseif (is_array($item)) {
|
||||
|
||||
@@ -16,6 +16,16 @@ class BlueprintSchema extends BaseBlueprints implements ExportInterface
|
||||
{
|
||||
use Export;
|
||||
|
||||
protected $ignoreFormKeys = [
|
||||
'label' => true,
|
||||
'title' => true,
|
||||
'help' => true,
|
||||
'placeholder' => true,
|
||||
'placeholder_key' => true,
|
||||
'placeholder_value' => true,
|
||||
'fields' => true
|
||||
];
|
||||
|
||||
/**
|
||||
* Validate data against blueprints.
|
||||
*
|
||||
|
||||
@@ -92,6 +92,6 @@ class Blueprints
|
||||
$blueprint->setContext($this->search);
|
||||
}
|
||||
|
||||
return $blueprint->load();
|
||||
return $blueprint->load()->init();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user