Skip to content

Commit

Permalink
Merge branch 'release/5.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Aug 31, 2021
2 parents 7de0dee + 5015630 commit 5b9ce04
Show file tree
Hide file tree
Showing 26 changed files with 338 additions and 152 deletions.
16 changes: 14 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
# v5.1.0
## 08/31/2021

1. [](#new)
* Require **Grav 1.7.19**
* Added support for custom form layouts
* Added Twig function `prepare_form_fields()` and `prepare_form_field()` to prepare form fields and field array
* Added Twig function `include_form_field()` to get all include paths for the field type
* Make `nonce` to a customizable field
1. [](#bugfix)
* Fixed bad cookie value for remembering the position of nested tabs

# v5.0.3
## 05/15/2021
## 06/15/2021

1. [](#improved)
* Removed the windows `\r\n` line breaks + extraneous escaping in `data.txt.twig`
* Use base64_encode filter rather than function
* Use `base64_encode` filter rather than function

# v5.0.2
## 04/23/2021
Expand Down
4 changes: 2 additions & 2 deletions blueprints.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Form
slug: form
type: plugin
version: 5.0.3
version: 5.1.0
description: Enables the forms handling
icon: check-square
author:
Expand All @@ -14,7 +14,7 @@ bugs: https://github.com/getgrav/grav-plugin-form/issues
license: MIT

dependencies:
- { name: grav, version: '>=1.7.0' }
- { name: grav, version: '>=1.7.19' }

form:
validation: strict
Expand Down
1 change: 1 addition & 0 deletions classes/Form.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Grav\Plugin\Form;

use ArrayAccess;
Expand Down
4 changes: 1 addition & 3 deletions classes/FormFactory.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

declare(strict_types=1);
<?php declare(strict_types=1);

namespace Grav\Plugin\Form;

Expand Down
1 change: 1 addition & 0 deletions classes/Forms.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Grav\Plugin\Form;

use Grav\Common\Page\Interfaces\PageInterface;
Expand Down
133 changes: 133 additions & 0 deletions classes/TwigExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php declare(strict_types=1);

namespace Grav\Plugin\Form;

use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
use function is_string;

/**
* Class GravExtension
* @package Grav\Common\Twig\Extension
*/
class TwigExtension extends AbstractExtension
{
/**
* Return a list of all filters.
*
* @return array
*/
public function getFilters(): array
{
return [];
}

/**
* Return a list of all functions.
*
* @return array
*/
public function getFunctions(): array
{
return [
new TwigFunction('prepare_form_fields', [$this, 'prepareFormFields']),
new TwigFunction('prepare_form_field', [$this, 'prepareFormField']),
new TwigFunction('include_form_field', [$this, 'includeFormField']),
];
}

/**
* Filters form fields for the current parent.
*
* @param array $fields Form fields
* @param string|null $parent Parent field name if available
* @return array
*/
public function prepareFormFields($fields, $parent = null): array
{
$list = [];

if (is_iterable($fields)) {
foreach ($fields as $name => $field) {
$field = $this->prepareFormField($field, $name, $parent);
if ($field) {
$list[$field['name']] = $field;
}
}
}

return $list;
}

/**
* Filters field name by changing dot notation into array notation.
*
* @param array $field Form field
* @param string|int|null $name Field name (defaults to field.name)
* @param string|null $parent Parent field name if available
* @param array|null $options List of options to override
* @return array|null
*/
public function prepareFormField($field, $name = null, $parent = null, array $options = []): ?array
{
// Make sure that the field is a valid form field type and is not being ignored.
if (empty($field['type']) || ($field['validate']['ignore'] ?? false)) {
return null;
}

// Check if we have just a list of fields (no name given).
if (is_int($name)) {
$name = null;
}

// Make sure that the field has a name.
$name = $name ?? $field['name'] ?? null;
if (!is_string($name) || $name === '') {
return null;
}

// Prefix name with the parent name if needed.
if (str_starts_with($name, '.')) {
$name = $parent ? $parent . $name : (string)substr($name, 1);
} elseif (isset($options['key'])) {
$name = str_replace('*', $options['key'], $name);
}

unset($options['key']);

// Loop through options
foreach ($options as $key => $option) {
$field[$key] = $option;
}

// Always set field name.
$field['name'] = $name;

return $field;
}

/**
* @param string $type
* @param string|null $layout
* @param string|null $default
* @return string[]
*/
public function includeFormField(string $type, string $layout = null, string $default = null): array
{
$list = [
"forms/fields/{$type}/{$layout}-{$type}.html.twig",
"forms/fields/{$type}/{$type}.html.twig",
];
if ($default) {
$list = array_merge(
$list,
[
"forms/fields/{$default}/{$layout}-{$default}.html.twig",
"forms/fields/{$default}/{$default}.html.twig",
]
);
}

return $list;
}
}
10 changes: 10 additions & 0 deletions form.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Grav\Framework\Route\Route;
use Grav\Plugin\Form\Form;
use Grav\Plugin\Form\Forms;
use Grav\Plugin\Form\TwigExtension;
use ReCaptcha\ReCaptcha;
use ReCaptcha\RequestMethod\CurlPost;
use RecursiveArrayIterator;
Expand Down Expand Up @@ -84,6 +85,7 @@ public static function getSubscribedEvents()
['autoload', 100000],
['onPluginsInitialized', 0]
],
'onTwigExtensions' => ['onTwigExtensions', 0],
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0]
];
}
Expand Down Expand Up @@ -360,6 +362,14 @@ public function onTwigInitialized(): void

}

/**
* @return void
*/
public function onTwigExtensions(): void
{
$this->grav['twig']->twig->addExtension(new TwigExtension());
}

/**
* Add current directory to twig lookup paths.
*
Expand Down
2 changes: 0 additions & 2 deletions templates/forms/default/field.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,4 @@
{% endif %}
{% endblock %}



{% endif %}
30 changes: 16 additions & 14 deletions templates/forms/default/fields.html.twig
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
{% for field_name, field in fields %}
{% if field.type and not field.validate.ignore %}
{%- if field_name starts with '.' -%}
{% set field_name = name ? name ~ field_name : field_name[1:] %}
{% set field = field|merge({ name: field_name }) %}
{% endif %}
{% set fields = prepare_form_fields(fields, name) %}
{% if fields|length %}
{% block outer_markup_field_open %}{% endblock %}
{% for field_name, field in fields %}
{% set value = form ? form.value(field.name) : data.value(field.name) %}
{% set field_templates = include_form_field(field.type, field_layout, fallback_field ?? 'text') %}

{% set value = form ? form.value(field_name) : data.value(field_name) %}
{% block inner_markup_field_open %}{% endblock %}
{% block field %}
{% include ["forms/fields/#{field.type}/#{field.type}.html.twig", 'forms/fields/text/text.html.twig'] %}
{% endblock %}
{% block inner_markup_field_close %}{% endblock %}
{% endif %}
{% endfor %}
{% block inner_markup_field_open %}{% endblock %}
{% block field %}
{% include field_templates %}
{% endblock %}
{% block inner_markup_field_close %}{% endblock %}
{% endfor %}
{% block outer_markup_field_close %}{% endblock %}
{% else %}
{% block empty_fields_markup %}{% endblock %}
{% endif %}
27 changes: 12 additions & 15 deletions templates/forms/default/form.html.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% if form is null %}
{% set form = grav.session.getFlashObject('form') %}
{% endif %}
{% set form = form ?? grav.session.getFlashObject('form') %}
{% set layout = layout ?? form.layout ?? 'default' %}
{% set field_layout = layout %}

{# Keep here for Backwards Compatibility #}
{% include 'partials/form-messages.html.twig' %}
Expand Down Expand Up @@ -67,17 +67,14 @@
{% set override_inner_markup_fields %}
{% block inner_markup_fields %}
{% for field_name, field in form.fields %}
{% set field_name = field.name ?? field_name %}
{% if field_name and not field.validate.ignore %}
{%- if field_name starts with '.' -%}
{% set field_name = field_name[1:] %}
{% set field = field|merge({ name: field_name }) %}
{% endif %}
{% set field = prepare_form_field(field, field_name) %}
{% if field %}
{% set value = form ? form.value(field.name) : data.value(field.name) %}
{% set field_templates = include_form_field(field.type, field_layout) %}

{% set value = form ? form.value(field_name) : data.value(field_name) %}
{% block inner_markup_field_open %}{% endblock %}
{% block field %}
{% include "forms/fields/#{field.type}/#{field.type}.html.twig" ignore missing %}
{% include field_templates ignore missing %}
{% endblock %}
{% block inner_markup_field_close %}{% endblock %}
{% endif %}
Expand Down Expand Up @@ -132,10 +129,10 @@
{{ override_inner_markup_fields_start|raw }}
{{ override_inner_markup_fields|raw }}

{% include "forms/fields/formname/formname.html.twig" %}
{% include "forms/fields/formtask/formtask.html.twig" %}
{% include 'forms/fields/uniqueid/uniqueid.html.twig' %}
{{ nonce_field(form.getNonceAction() ?? 'form', form.getNonceName() ?? 'form-nonce')|raw }}
{% include include_form_field('formname', field_layout, 'hidden') %}
{% include include_form_field('formtask', field_layout, 'hidden') %}
{% include include_form_field('uniqueid', field_layout, 'hidden') %}
{% include include_form_field('nonce', field_layout, 'hidden') %}

{{ override_inner_markup_fields_end|raw }}
{% endblock %}
Expand Down
8 changes: 8 additions & 0 deletions templates/forms/field.html.twig
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
{#
DO NOT MODIFY!
Default layout can be found in form plugin or your theme:
templates/forms/layouts/field/default-field.html.twig
#}
{% extends "forms/default/field.html.twig" %}
9 changes: 4 additions & 5 deletions templates/forms/fields/column/column.html.twig
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{% extends "forms/field.html.twig" %}

{% block field %}
{% if field.fields %}
<div class="form-column {{ field.classes }}">
{% include 'forms/default/fields.html.twig' with {name: field.name, fields: field.fields} %}
</div>
{% endif %}
{% embed 'forms/default/fields.html.twig' with {name: field.name, fields: field.fields} %}
{% block outer_markup_field_open %}<div class="form-column {{ field.classes }}">{% endblock %}
{% block outer_markup_field_close %}</div>{% endblock %}
{% endembed %}
{% endblock %}
2 changes: 0 additions & 2 deletions templates/forms/fields/columns/columns.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

{% block field %}
<div class="form-columns {{ field.classes }}">
{% if field.fields %}
{% include 'forms/default/fields.html.twig' with {name: field.name, fields: field.fields} %}
{% endif %}
</div>
{% endblock %}
4 changes: 1 addition & 3 deletions templates/forms/fields/conditional/conditional.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
<div class="{{ field.classes }}">
{% endif %}

{% if field.fields %}
{% include 'forms/default/fields.html.twig' with {name: field.name, fields: field.fields} %}
{% endif %}
{% include 'forms/default/fields.html.twig' with {name: field.name, fields: field.fields} %}

{% if field.classes %}
</div>
Expand Down
8 changes: 3 additions & 5 deletions templates/forms/fields/fieldset/fieldset.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@

{% block field %}
<fieldset {% if field.id is defined %}id="{{ field.id }}"{% endif %} {% if field.classes is defined %}class="{{ field.classes }}" {% endif %}>
{% if field.legend %}
{% if field.legend %}
<legend>{{ field.legend|t }}</legend>
{% endif %}
{% endif %}

{% if field.fields %}
{% include 'forms/default/fields.html.twig' with {name: field.name, fields: field.fields} %}
{% endif %}
{% include 'forms/default/fields.html.twig' with {name: field.name, fields: field.fields} %}
</fieldset>
{% endblock %}
1 change: 1 addition & 0 deletions templates/forms/fields/nonce/nonce.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ nonce_field(form.getNonceAction() ?? 'form', form.getNonceName() ?? 'form-nonce')|raw }}
9 changes: 4 additions & 5 deletions templates/forms/fields/section/section.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
<p>{{ field.text|t|raw }}</p>
{% endif %}

{% if field.fields %}
<div class="form-section">
{% include 'forms/default/fields.html.twig' with {name: field.name, fields: field.fields} %}
</div>
{% endif %}
{% embed 'forms/default/fields.html.twig' with {name: field.name, fields: field.fields} %}
{% block outer_markup_field_open %}<div class="form-section">{% endblock %}
{% block outer_markup_field_close %}</div>{% endblock %}
{% endembed %}
</div>
{% endif %}
{% endblock %}
Loading

0 comments on commit 5b9ce04

Please sign in to comment.