Skip to content

Commit

Permalink
Merge branch 'release/5.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Feb 17, 2021
2 parents 998ee12 + 4069705 commit 56f2a25
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 38 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
# v5.0.0
## 02/17/2021

1. [](#new)
* Requires **Grav 1.7.0**
* Allow admins to temporarily disable form process actions by setting the value to `false` [#481](https://github.com/getgrav/grav-plugin-form/pull/481)
1. [](#improved)
* Add `id` attribute to hidden field [#495](https://github.com/getgrav/grav-plugin-form/pull/495)
* Escape text as YAML in multi-line textarea [#464](https://github.com/getgrav/grav-plugin-form/pull/464)
1. [](#bugfix)
* Fixed reCaptcha v3 incompatibility with multiple forms on the same page sharing different actions [#416](https://github.com/getgrav/grav-plugin-form/issues/416)
* Toggle fields do not save `false` if they are `toggleable` [#497](https://github.com/getgrav/grav-plugin-form/issues/497)
* Data template fixes [#494](https://github.com/getgrav/grav-plugin-form/pull/494)
* Fix deprecated Twig method

# v4.3.1
## 01/31/2021

Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ enabled: true
# How to use the Form Plugin

The Learn site has two pages describing how to use the Form Plugin:
- [Forms](http://learn.getgrav.org/advanced/forms)
- [Forms](https://learn.getgrav.org/forms)
- [Add a contact form](http://learn.getgrav.org/forms/forms/example-form)

# Using email
Expand All @@ -30,7 +30,9 @@ Note: when using email functionality in your forms, make sure you have configure

# NOTES:

As of version **Form version 4.0.6**, form labels are now being output with the `|raw` filter. If you wish to show HTML in your form label, ie `Root Folder <root>`, then you need to escape that in your form definition:
As of version **Form 5.0.0** Grav 1.7+ is required.

As of version **Form 4.0.6**, form labels are now being output with the `|raw` filter. If you wish to show HTML in your form label, ie `Root Folder <root>`, then you need to escape that in your form definition:

```yaml
label: Root Folder &lt;root&gt;
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: 4.3.1
version: 5.0.0
testing: false
description: Enables the forms handling
icon: check-square
Expand All @@ -15,7 +15,7 @@ bugs: https://github.com/getgrav/grav-plugin-form/issues
license: MIT

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

form:
validation: strict
Expand Down
5 changes: 5 additions & 0 deletions classes/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,11 @@ public function post()
$data = $data[$action];
}

// do not execute action, if deactivated
if (false === $data) {
continue;
}

$event = new Event(['form' => $this, 'action' => $action, 'params' => $data]);
$grav->fireEvent('onFormProcessed', $event);

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"docs": "https://github.com/getgrav/grav-plugin-form/blob/master/README.md"
},
"require": {
"php": ">=7.1.3",
"php": "^7.3.6 || ^8.0",
"ext-json": "*",
"google/recaptcha": "^1.2"
},
Expand All @@ -34,7 +34,7 @@
},
"config": {
"platform": {
"php": "7.1.3"
"php": "7.3.6"
}
}
}
12 changes: 7 additions & 5 deletions form.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use RocketTheme\Toolbox\File\File;
use RocketTheme\Toolbox\Event\Event;
use RuntimeException;
use Twig\Extension\CoreExtension;
use Twig\TwigFunction;
use function count;
use function function_exists;
Expand Down Expand Up @@ -352,7 +353,7 @@ public function onTwigInitialized(): void
new TwigFunction('forms', [$this, 'getForm'])
);

$this->grav['twig']->twig()->getExtension('Twig_Extension_Core')->setEscaper('yaml', function ($twig, $string, $charset) {
$this->grav['twig']->twig()->getExtension(CoreExtension::class)->setEscaper('yaml', function ($twig, $string, $charset) {
return Yaml::dump($string);
}
);
Expand Down Expand Up @@ -792,10 +793,11 @@ public function getForm($data = null)
return $first_form;
}

//No form on this route. Try looking up in the current page first
/** @var Forms $forms */
$forms = $this->grav['forms'];
return $forms->createPageForm($this->grav['page']);
// Try to get page by defined route first or get current if not found
$page = $this->grav['pages']->find($page_route) ?: $this->grav['page'];

// Try looking up in the defined page
return $this->grav['forms']->createPageForm($page);
}

// return the form you are looking for if available
Expand Down
5 changes: 4 additions & 1 deletion templates/forms/default/data.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
{% set use_keys = field.use is defined and field.use == 'keys' %}
{% for key,value in form.value(scope ~ field.name) %}
{% set index = (use_keys ? key : value) %}
<li>{{ field.options[index]|e }}</li>
<li>{{ field.options[index]|t|e }}</li>
{% endfor %}
</ul>
{% elseif field.type == 'radio' %}
{% set value = form.value(scope ~ field.name) %}
{{ field.options[value]|t|e }}
{% elseif field.type == 'checkbox' %}
{{ (form.value(scope ~ field.name) == 1) ? "GRAV.YES"|t|e : "GRAV.NO"|t|e }}
{% elseif field.type == 'select' %}
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 @@ -9,7 +9,7 @@
{%- if show_field %}
{%- set value = form.value(scope ~ (field.name ?? index)) -%}
{%- if value -%}
{{- field.label|t|e }}: {{ string(value is iterable ? value|json_encode : value|e) ~ "\r\n" }}
{{- field.label|t|e }}: {{ string(value is iterable ? value|json_encode : value|escape('yaml')) ~ "\r\n" }}
{%- endif -%}
{%- endif %}
{%- endif %}
Expand Down
16 changes: 10 additions & 6 deletions templates/forms/fields/captcha/captcha.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
{% do assets.addJs('https://www.google.com/recaptcha/api.js?render='~site_key~'&theme=' ~ theme) %}
{#<script src='https://www.google.com/recaptcha/api.js?render={{ site_key }}&theme={{ theme }}'></script>#}
<script type="application/javascript">
(document.querySelectorAll('form') || []).forEach(function(form, index) {
var submit = function(event) {
window.gRecaptchaInstances = window.gRecaptchaInstances || {};
window.gRecaptchaInstances['{{ form.id }}'] = {
element: document.querySelector('form#{{ form.id }}'),
submit: function (event) {
event.preventDefault();
grecaptcha.ready(function () {
grecaptcha.execute('{{ site_key }}', { action: '{{ action }}' }).then(function(token) {
grecaptcha.execute('{{ site_key }}', {action: '{{ action }}'}).then(function (token) {
var tokenElement = document.createElement('input');
tokenElement.setAttribute('type', 'hidden');
tokenElement.setAttribute('name', 'data[token]');
Expand All @@ -30,16 +32,18 @@
actionElement.setAttribute('name', 'data[action]');
actionElement.setAttribute('value', '{{ action }}');
const form = window.gRecaptchaInstances['{{ form.id }}'].element;
const submit = window.gRecaptchaInstances['{{ form.id }}'].submit;
form.insertBefore(tokenElement, form.firstChild);
form.insertBefore(actionElement, form.firstChild);
form.removeEventListener('submit', submit);
form.submit();
});
});
};
}
};
form.addEventListener('submit', submit);
});
window.gRecaptchaInstances['{{ form.id }}'].element.addEventListener('submit', window.gRecaptchaInstances['{{ form.id }}'].submit);
</script>
{% elseif config.plugins.form.recaptcha.version == '2-invisible' %}
<script type="application/javascript">
Expand Down
2 changes: 1 addition & 1 deletion templates/forms/fields/hidden/hidden.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
{% endif %}
{% set input_value = value is iterable ? value|join(',') : value|string %}

<input data-grav-field="hidden" data-grav-disabled="false" type="hidden" class="input" name="{{ (scope ~ field.name)|fieldName }}" value="{{ input_value|e('html_attr') }}" />
<input data-grav-field="hidden" data-grav-disabled="false" {% if field.id is defined %}id="{{ field.id|e }}" {% endif %}type="hidden" class="input" name="{{ (scope ~ field.name)|fieldName }}" value="{{ input_value|e('html_attr') }}" />
{% endblock %}
23 changes: 5 additions & 18 deletions templates/forms/fields/toggle/toggle.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,16 @@
{% endblock %}

{% block input %}
{% set value = (value is same as(false) ? 0 : value) %}

{% if field.force_bool and get_type(value) == 'string'%}
{% set value = true %}
{% endif %}


<div class="switch-toggle switch-grav {{ field.size }} switch-{{ field.options|length }} {{ field.classes }}">
{% set maxLen = 0 %}
{% for text in field.options %}
{% set translation = text|t|trim %}
{% set maxLen = max(translation|length, maxLen) %}
{% endfor %}

{% set value = value|string %}
{% for key, text in field.options %}
{% set key = key|string %}
{% set id = "toggle_" ~ field.name ~ key %}
{% set translation = text|t|trim %}

Expand All @@ -43,19 +38,11 @@
id="{{ id }}"
name="{{ (scope ~ field.name)|fieldName }}"
{% if field.highlight is defined %}
class="{{ field.highlight == '' ~ key ? 'highlight' : '' }}"
class="{{ field.highlight|string is same as(key) ? 'highlight' : '' }}"
{% endif %}
{% if field.disabled or isDisabledToggleable %}disabled="disabled"{% endif %}
{% if field.toggleable %}
{% if '' ~ key == '' ~ value %}
checked="checked"
{% elseif value is defined and (key == 1 or key == '1') %}
checked="checked"
{% endif %}
{% else %}
{% if '' ~ key == '' ~ value %}
checked="checked"
{% endif %}
{% if key is same as(value) %}
checked="checked"
{% endif %}
{% if required %}required="required"{% endif %}
{% if field.tabindex %}tabindex="{{ field.tabindex }}"{% endif %}
Expand Down

0 comments on commit 56f2a25

Please sign in to comment.