From a93614ba88b41254ec2d234eac7323aa695d1600 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Fri, 21 Apr 2017 19:27:31 -0600 Subject: [PATCH] Support proper form handling with nested fields #141 --- CHANGELOG.md | 6 +-- classes/form.php | 60 +++++++++++++------------- templates/forms/default/data.html.twig | 2 +- templates/forms/default/data.txt.twig | 2 +- 4 files changed, 36 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2406ff39..220f7e0a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,8 @@ -# v2.4.1 +# v2.5.0 ## 04/xx/2017 -1. [](#bugfix) - +1. [](#new) + * Support proper form handling with nested fields [#141](https://github.com/getgrav/grav-plugin-form/issues/141) # v2.4.0 ## 04/19/2017 diff --git a/classes/form.php b/classes/form.php index 4607df0b..6d139bf8 100644 --- a/classes/form.php +++ b/classes/form.php @@ -192,36 +192,9 @@ public function reset() $name = $this->items['name']; $grav = Grav::instance(); - // Fix naming for fields (presently only for toplevel fields) + // Fix naming for fields (supports nested fields now!) if (isset($this->items['fields'])) { - foreach ($this->items['fields'] as $key => $field) { - // default to text if not set - if (!isset($field['type'])) { - $field['type'] = 'text'; - } - - $types = $grav['plugins']->formFieldTypes; - - // manually merging the field types - if ($types !== null && key_exists($field['type'], $types)) { - $field += $types[$field['type']]; - } - - // BC for old style of array style field definitions - if (is_numeric($key) && isset($field['name'])) { - array_splice($this->items['fields'], $key); - $key = $field['name']; - } - - // Add name based on key if not already set - if (!isset($field['name'])) { - $field['name'] = $key; - } - - // set any modifications back on the fields array - $this->items['fields'][$key] = $field; - - } + $this->items['fields'] = $this->processFields($this->items['fields']); } $items = $this->items; @@ -242,6 +215,35 @@ public function reset() } + protected function processFields($fields) + { + $types = Grav::instance()['plugins']->formFieldTypes; + + $return = array(); + foreach ($fields as $key => $value) { + + // default to text if not set + if (!isset($value['type'])) { + $value['type'] = 'text'; + } + + // manually merging the field types + if ($types !== null && key_exists($value['type'], $types)) { + $value += $types[$value['type']]; + } + + // Fix numeric indexes + if (is_numeric($key) && isset($value['name'])) { + $key = $value['name']; + } + if (isset($value['fields']) && is_array($value['fields'])) { + $value['fields'] = $this->processFields($value['fields']); + } + $return[$key] = $value; + } + return $return; + } + public function fields() { if (is_null($this->fields)) { diff --git a/templates/forms/default/data.html.twig b/templates/forms/default/data.html.twig index 9f54f282..d8a950fa 100644 --- a/templates/forms/default/data.html.twig +++ b/templates/forms/default/data.html.twig @@ -28,7 +28,7 @@ {% endblock %} {% endif %} {% else %} - {% if field.fields is iterable %} + {% if field.fields %} {{ _self.render_field(form, field.fields) }} {% endif %} {% endif %} diff --git a/templates/forms/default/data.txt.twig b/templates/forms/default/data.txt.twig index ff9eb864..0b03704d 100644 --- a/templates/forms/default/data.txt.twig +++ b/templates/forms/default/data.txt.twig @@ -5,7 +5,7 @@ {%- set value = form.value(field.name ?? index) %} {{- field.name ?? index }}: {{ string((value is iterable ? value|json_encode : value)) ~ "\r\n" }} {%- else %} - {%- if field.fields is iterable %} + {%- if field.fields %} {{- _self.render_field(form, field.fields) }} {%- endif %} {%- endif %}