Skip to content

Commit

Permalink
Merge branch 'release/5.1.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Oct 26, 2021
2 parents ecda5a9 + 578743a commit 668f64a
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 21 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# v5.1.3
## 10/26/2021

1. [](#new)
* Require **Grav 1.7.24**
* Added support to disable a form, making it readonly
* Added `|value_and_label` Twig filter to convert options to value/label pairs
1. [](#improved)
* Improved Twig function `include_form_field()` to allow the second parameter to be an array of layouts

# v5.1.2
## 09/29/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.1.2
version: 5.1.3
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.21' }
- { name: grav, version: '>=1.7.24' }

form:
validation: strict
Expand Down
4 changes: 4 additions & 0 deletions classes/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ public function __construct(PageInterface $page, $name = null, $form = null)
$this->items['nonce']['action'] = 'form';
}

if (Utils::isPositive($this->items['disabled'] ?? false)) {
$this->disable();
}

// Initialize form properties.
$this->name = $this->items['name'];
$this->setId($this->items['id']);
Expand Down
67 changes: 49 additions & 18 deletions classes/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Grav\Plugin\Form;

use Grav\Framework\Form\Interfaces\FormInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
use function is_string;

Expand All @@ -12,6 +14,13 @@
*/
class TwigExtension extends AbstractExtension
{
public function getFilters()
{
return [
new TwigFilter('value_and_label', [$this, 'valueAndLabel'])
];
}

/**
* Return a list of all functions.
*
Expand All @@ -20,26 +29,41 @@ class TwigExtension extends AbstractExtension
public function getFunctions(): array
{
return [
new TwigFunction('prepare_form_fields', [$this, 'prepareFormFields']),
new TwigFunction('prepare_form_field', [$this, 'prepareFormField']),
new TwigFunction('prepare_form_fields', [$this, 'prepareFormFields'], ['needs_context' => true]),
new TwigFunction('prepare_form_field', [$this, 'prepareFormField'], ['needs_context' => true]),
new TwigFunction('include_form_field', [$this, 'includeFormField']),
];
}

public function valueAndLabel($value): array
{
if (!is_array($value)) {
return [];
}

$list = [];
foreach ($value as $key => $label) {
$list[] = ['value' => $key, 'label' => $label];
}

return $list;
}

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

if (is_iterable($fields)) {
foreach ($fields as $name => $field) {
$field = $this->prepareFormField($field, $name, $parent);
$field = $this->prepareFormField($context, $field, $name, $parent);
if ($field) {
$list[$field['name']] = $field;
}
Expand All @@ -52,13 +76,14 @@ public function prepareFormFields($fields, $parent = null): array
/**
* Filters field name by changing dot notation into array notation.
*
* @param array $context
* @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
public function prepareFormField(array $context, $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)) {
Expand Down Expand Up @@ -86,6 +111,13 @@ public function prepareFormField($field, $name = null, $parent = null, array $op

unset($options['key']);

// Set fields as readonly if form is in readonly mode.
/** @var FormInterface $form */
$form = $context['form'] ?? null;
if ($form && method_exists($form, 'isEnabled') && !$form->isEnabled()) {
$options['disabled'] = true;
}

// Loop through options
foreach ($options as $key => $option) {
$field[$key] = $option;
Expand All @@ -99,24 +131,23 @@ public function prepareFormField($field, $name = null, $parent = null, array $op

/**
* @param string $type
* @param string|null $layout
* @param string|string[]|null $layouts
* @param string|null $default
* @return string[]
*/
public function includeFormField(string $type, string $layout = null, string $default = null): array
public function includeFormField(string $type, $layouts = null, string $default = null): array
{
$list = [
"forms/fields/{$type}/{$layout}-{$type}.html.twig",
"forms/fields/{$type}/{$type}.html.twig",
];
$list = [];
foreach ((array)$layouts as $layout) {
$list[] = "forms/fields/{$type}/{$layout}-{$type}.html.twig";
}
$list[] = "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",
]
);
foreach ((array)$layouts as $layout) {
$list[] = "forms/fields/{$default}/{$layout}-{$default}.html.twig";
}
$list[] = "forms/fields/{$default}/{$default}.html.twig";
}

return $list;
Expand Down
6 changes: 5 additions & 1 deletion templates/forms/default/form.html.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% set form = form ?? grav.session.getFlashObject('form') %}
{% set layout = layout ?? form.layout ?? 'default' %}
{% set field_layout = layout %}
{% set field_layout = field_layout ?? layout %}

{# Keep here for Backwards Compatibility #}
{% include 'partials/form-messages.html.twig' %}
Expand Down Expand Up @@ -130,17 +130,20 @@
{{ override_inner_markup_fields_start|raw }}
{{ override_inner_markup_fields|raw }}

{% if form.enabled %}
{% 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') %}
{% endif %}

{{ override_inner_markup_fields_end|raw }}
{% endblock %}

{% block embed_buttons %}
{{ override_inner_markup_buttons_start|raw }}

{% if form.enabled %}
{% for button in form.buttons %}
{% if button.outerclasses is defined %}<div class="{{ button.outerclasses }}">{% endif %}

Expand Down Expand Up @@ -188,6 +191,7 @@

{% if button.outerclasses is defined %}</div>{% endif %}
{% endfor %}
{% endif %}

{{ override_inner_markup_buttons_end }}
{% endblock %}
Expand Down

0 comments on commit 668f64a

Please sign in to comment.