-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
338 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<?php | ||
|
||
namespace Grav\Plugin\Form; | ||
|
||
use ArrayAccess; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -155,6 +155,4 @@ | |
{% endif %} | ||
{% endblock %} | ||
|
||
|
||
|
||
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{ nonce_field(form.getNonceAction() ?? 'form', form.getNonceName() ?? 'form-nonce')|raw }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.