Skip to content

Commit

Permalink
Merge branch 'release/2.5.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Apr 24, 2017
2 parents 6b67ecb + cd2be1b commit f11e707
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 38 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# v2.5.0
## 04/24/2017

1. [](#new)
* Support proper form handling with nested fields [#141](https://github.com/getgrav/grav-plugin-form/issues/141)
1. [](#bugfix)
* Added check for valid Grav forms before trying to create a form object

# v2.4.0
## 04/19/2017

Expand Down
2 changes: 1 addition & 1 deletion blueprints.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Form
version: 2.4.0
version: 2.5.0
description: Enables the forms handling
icon: check-square
author:
Expand Down
60 changes: 31 additions & 29 deletions classes/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)) {
Expand Down
14 changes: 8 additions & 6 deletions form.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public function onPagesInitialized()
}

// Handle posting if needed.
if (!empty($_POST)) {
if (!empty($_POST) && isset($_POST['data'])) {

$current_form_name = $this->getFormName($this->grav['page']);
$this->json_response = [];
Expand All @@ -191,11 +191,13 @@ public function onPagesInitialized()
$this->form = new Form($this->grav['page']);
}

if ($this->grav['uri']->extension() === 'json' && isset($_POST['__form-file-uploader__'])) {
$this->json_response = $this->form->uploadFiles();
} else {
$this->form->post();
$submitted = true;
if ($this->form) {
if ($this->grav['uri']->extension() === 'json' && isset($_POST['__form-file-uploader__'])) {
$this->json_response = $this->form->uploadFiles();
} else {
$this->form->post();
$submitted = true;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion templates/forms/default/data.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
{% endblock %}
{% endif %}
{% else %}
{% if field.fields is iterable %}
{% if field.fields %}
{{ _self.render_field(form, field.fields) }}
{% endif %}
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion templates/forms/default/data.txt.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down

0 comments on commit f11e707

Please sign in to comment.