From 03c9147818273ca8888432d40ce748d94bddbea1 Mon Sep 17 00:00:00 2001 From: NicoHood Date: Sat, 9 Jan 2021 13:00:04 +0100 Subject: [PATCH 1/5] Added evaluate option for all non-iterable field types and checkboxes --- templates/forms/default/field.html.twig | 2 +- .../fields/checkboxes/checkboxes.html.twig | 17 +++++++++++++---- templates/forms/fields/hidden/hidden.html.twig | 1 - 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/templates/forms/default/field.html.twig b/templates/forms/default/field.html.twig index faa00949..bb5b58ef 100644 --- a/templates/forms/default/field.html.twig +++ b/templates/forms/default/field.html.twig @@ -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(field.default) : field.default) %} {% set toggleable = field.toggleable ?? false %} {% if toggleable %} {% set originalValue = originalValue is defined ? originalValue : value %} diff --git a/templates/forms/fields/checkboxes/checkboxes.html.twig b/templates/forms/fields/checkboxes/checkboxes.html.twig index ba9a6506..1be01fb3 100644 --- a/templates/forms/fields/checkboxes/checkboxes.html.twig +++ b/templates/forms/fields/checkboxes/checkboxes.html.twig @@ -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(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() }} diff --git a/templates/forms/fields/hidden/hidden.html.twig b/templates/forms/fields/hidden/hidden.html.twig index 8506fa59..3b368a81 100644 --- a/templates/forms/fields/hidden/hidden.html.twig +++ b/templates/forms/fields/hidden/hidden.html.twig @@ -2,7 +2,6 @@ {% block field %} -{% set value = value ?: (field.value ?? (field.evaluate ? evaluate(field.default) : field.default)) %} {% set input_value = value is iterable ? value|join(',') : value|string %} From 57373ef8d87d237ceab7e2f60644ad5a6e79cf95 Mon Sep 17 00:00:00 2001 From: NicoHood Date: Sat, 9 Jan 2021 13:46:22 +0100 Subject: [PATCH 2/5] Fix hidden field fallback 'field.value' --- templates/forms/fields/hidden/hidden.html.twig | 1 + 1 file changed, 1 insertion(+) diff --git a/templates/forms/fields/hidden/hidden.html.twig b/templates/forms/fields/hidden/hidden.html.twig index 3b368a81..2be43011 100644 --- a/templates/forms/fields/hidden/hidden.html.twig +++ b/templates/forms/fields/hidden/hidden.html.twig @@ -2,6 +2,7 @@ {% block field %} +{% set value = value ?: field.value %} {% set input_value = value is iterable ? value|join(',') : value|string %} From b7167846de8e509f8a2124fd651e85d1d61593df Mon Sep 17 00:00:00 2001 From: NicoHood Date: Sat, 9 Jan 2021 18:24:31 +0100 Subject: [PATCH 3/5] Fix data-grav-default attribute --- templates/forms/default/field.html.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/forms/default/field.html.twig b/templates/forms/default/field.html.twig index bb5b58ef..96f101cb 100644 --- a/templates/forms/default/field.html.twig +++ b/templates/forms/default/field.html.twig @@ -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 %} From 9d79f77e5574ddfae97b5f979a7df9bf24d572c7 Mon Sep 17 00:00:00 2001 From: NicoHood Date: Sat, 9 Jan 2021 18:29:14 +0100 Subject: [PATCH 4/5] Fix radio default value when using evaluate --- templates/forms/fields/radio/radio.html.twig | 1 - 1 file changed, 1 deletion(-) diff --git a/templates/forms/fields/radio/radio.html.twig b/templates/forms/fields/radio/radio.html.twig index 45cdd778..e2af94e9 100644 --- a/templates/forms/fields/radio/radio.html.twig +++ b/templates/forms/fields/radio/radio.html.twig @@ -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 %} From 642b4d21a9d2c15c1bfdfd7a798e0b57894387bf Mon Sep 17 00:00:00 2001 From: NicoHood Date: Sat, 9 Jan 2021 20:03:15 +0100 Subject: [PATCH 5/5] Use evaluate_twig instead of evaluate to make syntax for mixed strings simpler --- templates/forms/default/field.html.twig | 2 +- templates/forms/fields/checkboxes/checkboxes.html.twig | 2 +- templates/forms/fields/hidden/hidden.html.twig | 3 +++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/templates/forms/default/field.html.twig b/templates/forms/default/field.html.twig index 96f101cb..42a9677f 100644 --- a/templates/forms/default/field.html.twig +++ b/templates/forms/default/field.html.twig @@ -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 = default ?? (field.evaluate and field.default and field.default is not iterable ? evaluate(field.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 %} diff --git a/templates/forms/fields/checkboxes/checkboxes.html.twig b/templates/forms/fields/checkboxes/checkboxes.html.twig index 1be01fb3..61e4745c 100644 --- a/templates/forms/fields/checkboxes/checkboxes.html.twig +++ b/templates/forms/fields/checkboxes/checkboxes.html.twig @@ -4,7 +4,7 @@ {% set default = {} %} {% for key, default_value in field.default %} - {% set default_value = (default_value and field.evaluate) ? evaluate(default_value) : default_value %} + {% 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! #} diff --git a/templates/forms/fields/hidden/hidden.html.twig b/templates/forms/fields/hidden/hidden.html.twig index 2be43011..6399724a 100644 --- a/templates/forms/fields/hidden/hidden.html.twig +++ b/templates/forms/fields/hidden/hidden.html.twig @@ -1,5 +1,8 @@ {% 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 %}