From 03c9147818273ca8888432d40ce748d94bddbea1 Mon Sep 17 00:00:00 2001 From: NicoHood Date: Sat, 9 Jan 2021 13:00:04 +0100 Subject: [PATCH] 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 %}