Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added evaluate option for all non-iterable field types and checkboxes #473

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions templates/forms/default/field.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{% set vertical = field.style == 'vertical' %}

{% if not blueprints or (blueprints.schema.type(field.type)['input@'] ?? true) is same as(true) %}
{% set default = field.default %}
{% set default = default ?? (field.evaluate and field.default and field.default is not iterable ? evaluate_twig(field.default) : field.default) %}
{% set toggleable = field.toggleable ?? false %}
{% if toggleable %}
{% set originalValue = originalValue is defined ? originalValue : value %}
Expand Down Expand Up @@ -110,7 +110,7 @@
{% block global_attributes %}
data-grav-field="{{ field.type }}"
data-grav-disabled="{{ toggleable and toggleableChecked }}"
data-grav-default="{{ field.default|json_encode()|e('html_attr') }}"
data-grav-default="{{ default ? default|json_encode()|e('html_attr') }}"
{% endblock %}

{% block input_attributes %}
Expand Down
17 changes: 13 additions & 4 deletions templates/forms/fields/checkboxes/checkboxes.html.twig
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
{% extends "forms/field.html.twig" %}

{% set originalValue = value %}
{% set value = (value is null ? field.default : value) %}
{% if field.use == 'keys' and field.default %}
{% set value = field.default|merge(value) %}
{% endif %}

{% set default = {} %}
{% for key, default_value in field.default %}
{% set default_value = (default_value and field.evaluate) ? evaluate_twig(default_value) : default_value %}
{% if default_value %}
{% if field.use == 'keys' %}
{# NOTE: The brackets around the key are important! #}
{% set default = default|merge({(key): default_value} ) %}
{% else %}
{% set default = default|merge([default_value]) %}
{% endif %}
{% endif %}
{% endfor %}

{% block global_attributes %}
{{ parent() }}
Expand Down
5 changes: 4 additions & 1 deletion templates/forms/fields/hidden/hidden.html.twig
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{% extends "forms/field.html.twig" %}

{# NOTE: evaluate is used here instead of evaluate_twig for backwards compatibility #}
{% set default = default ?? (field.evaluate and field.default and field.default is not iterable ? evaluate(field.default) : field.default) %}

{% block field %}

{% set value = value ?: (field.value ?? (field.evaluate ? evaluate(field.default) : field.default)) %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rhukster Above old code is broken (and can only be fixed in base field), which means that we kind of need to implement this one.

{% set value = value ?: field.value %}
{% 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') }}" />
Expand Down
1 change: 0 additions & 1 deletion templates/forms/fields/radio/radio.html.twig
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{% extends "forms/field.html.twig" %}

{% set originalValue = value %}
{% set value = (value is null ? field.default : value) %}

{% block input %}
{% for key, text in field.options %}
Expand Down