field.html.twig 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. {% if not field.validate.ignore %}
  2. {% use 'forms/layouts/field-variables.html.twig' %}
  3. {% block field_override_variables_before %}{% endblock %}
  4. {% set field_name = (scope ~ field.name)|fieldName %}
  5. {% set vertical = field.style == 'vertical' %}
  6. {% if not blueprints or (blueprints.schema.type(field.type)['input@'] ?? true) is same as(true) %}
  7. {% set toggleable = field.toggleable ?? false %}
  8. {% if toggleable %}
  9. {% set originalValue = originalValue is defined ? originalValue : value %}
  10. {% set toggleableChecked = originalValue is not null %}
  11. {% elseif field.overridable %}
  12. {% set toggleable = true %}
  13. {% set default = form.getDefaultValue(field.name) ?? field.default %}
  14. {% set toggleableChecked = value is not null and value != default %}
  15. {% endif %}
  16. {% set cookie_name = 'forms-' ~ form.name ~ '-' ~ field.name %}
  17. {% set value = value ?? (get_cookie(cookie_name) is not null ? get_cookie(cookie_name) : field.default) %}
  18. {% if (field.yaml or field.validate.type == 'yaml') and value is iterable %}
  19. {% set value = value|toYaml %}
  20. {% endif %}
  21. {% else %}
  22. {% set toggleable = false %}
  23. {% endif %}
  24. {# DEPRECATED: Needed by old form fields; remove when backwards compatibility breaks are allowed #}
  25. {% set isDisabledToggleable = toggleable and not toggleableChecked %}
  26. {% if toggleable %}
  27. {% set form_field_toggleable %}
  28. {% include 'forms/default/toggleable.html.twig' with {checked: toggleableChecked} %}
  29. {% endset %}
  30. {% endif %}
  31. {% set errors = attribute(form.messages, field.name) %}
  32. {% set required = client_side_validation and field.validate.required in ['on', 'true', 1] %}
  33. {% set autofocus = (inline_errors == false) and field.autofocus in ['on', 'true', 1] %}
  34. {% if inline_errors and errors %}
  35. {% set autofocus = true %}
  36. {% endif %}
  37. {% set embed_outer_field_classes %}
  38. {% block outer_field_classes %}{% endblock %}
  39. {% endset %}
  40. {# Field Classes #}
  41. {%- if errors %}{% set form_field_outer_core = form_field_outer_core ~ ' has-errors' %}{% endif -%}
  42. {%- if toggleable %}{% set form_field_outer_core = form_field_outer_core ~ ' form-field-toggleable' %}{% endif -%}
  43. {% set layout_form_field_outer_classes = field.outerclasses %}
  44. {% set layout_form_field_outer_classes = layout_form_field_outer_classes|trim ~ ' ' ~ form_field_outer_classes %}
  45. {% set layout_form_field_outer_classes = layout_form_field_outer_classes|trim ~ ' ' ~ embed_outer_field_classes %}
  46. {# Show Label logic #}
  47. {% set show_label = field.label is not same as(false) and field.display_label is not same as(false )%}
  48. {# Label Classes #}
  49. {% set layout_form_field_outer_label_classes = ((form_field_outer_label_classes ?: 'form-label') ~ ' ' ~ field.labelclasses)|trim %}
  50. {% set layout_form_field_label_classes = (form_field_label_classes ?: 'inline')|trim %}
  51. {% set form_field_label_trim = toggleable ? 'toggleable' %}
  52. {# Field Outer Data classes #}
  53. {% set layout_form_field_outer_data_classes = ((form_field_outer_data_classes ?: ' form-data') ~ ' ' ~ field.dataclasses)|trim %}
  54. {# Field Wrapper classes #}
  55. {% set layout_form_field_wrapper_classes = ((form_field_wrapper_classes ?: ' form-input-wrapper') ~ ' ' ~ field.wrapper_classes)|trim %}
  56. {# Field input classes #}
  57. {% if field|of_type('array') %}
  58. {% if field.classes %}
  59. {% set field = field|merge({'classes': field.classes ~ ' ' ~ block('field_input_classes')|trim }) %}
  60. {% else %}
  61. {% set field = field|merge({'classes': block('field_input_classes') }) %}
  62. {% endif %}
  63. {% endif %}
  64. {% set layout_form_field_input_classes = (form_field_input_classes ~ ' ' ~ field.classes)|trim %}
  65. {# Inline error classes #}
  66. {% set form_field_inline_error_classes = form_field_inline_error_classes ?: ' form-errors' %}
  67. {# Field extra classes #}
  68. {% set form_field_extra_wrapper_classes = 'form-extra-wrapper ' ~ field.wrapper_classes %}
  69. {# Field For #}
  70. {% set form_field_for = toggleable ? 'toggleable_' ~ field.name : field.id|e %}
  71. {# Field Label #}
  72. {% set form_field_label = field.markdown ? field.label|markdown(false) : field.label %}
  73. {% set form_field_label = form_field_label|default(field.name|capitalize)|t %}
  74. {# Field Help #}
  75. {% if field.help %}
  76. {% set form_field_help = field.markdown ? field.help|t|markdown(false)|e : field.help|t|e %}
  77. {% endif %}
  78. {# Field Requied #}
  79. {% set form_field_required = field.validate.required in ['on', 'true', 1] ? true : false %}
  80. {# Field Description #}
  81. {% set form_field_description = field.markdown ? field.description|t|markdown(false)|raw : field.description|t|raw %}
  82. {% extends 'forms/layouts/field.html.twig' %}
  83. {% block global_attributes %}
  84. data-grav-field="{{ field.type }}"
  85. data-grav-disabled="{{ toggleable and toggleableChecked }}"
  86. data-grav-default="{{ field.default|json_encode()|e('html_attr') }}"
  87. {% endblock %}
  88. {% block input_attributes %}
  89. class="{{ layout_form_field_input_classes|trim }} {{ field.size }}"
  90. {% if field.id is defined %}id="{{ field.id|e }}" {% endif %}
  91. {% if field.style is defined %}style="{{ field.style|e }}" {% endif %}
  92. {% if field.disabled or isDisabledToggleable %}disabled="disabled"{% endif %}
  93. {% if field.placeholder %}placeholder="{{ field.placeholder|t|e('html_attr') }}"{% endif %}
  94. {% if autofocus %}autofocus="autofocus"{% endif %}
  95. {% if field.novalidate in ['on', 'true', 1] %}novalidate="novalidate"{% endif %}
  96. {% if field.readonly in ['on', 'true', 1] %}readonly="readonly"{% endif %}
  97. {% if field.autocomplete is defined %}autocomplete="{{ field.autocomplete }}"{% endif %}
  98. {% if field.autocapitalize in ['off', 'characters', 'words', 'sentences'] %}autocapitalize="{{ field.autocapitalize }}"{% endif %}
  99. {% if field.inputmode in ['none', 'text', 'decimal', 'numeric', 'tel', 'search', 'email', 'url'] %}inputmode="{{ field.inputmode }}"{% endif %}
  100. {% if field.tabindex %}tabindex="{{ field.tabindex }}"{% endif %}
  101. {% if field.spellcheck in ['true', 'false'] %}spellcheck="{{ field.spellcheck }}"{% endif %}
  102. {% if required %}required="required"{% endif %}
  103. {% if field.validate.pattern %}pattern="{{ field.validate.pattern|e }}"{% endif %}
  104. {% if field.validate.message %}title="{{ field.validate.message|t|e }}"
  105. {% elseif field.title is defined %}title="{{ field.title|t|e }}" {% endif %}
  106. {# Support key/value and .name/.value styles #}
  107. {% if field.attributes is defined %}
  108. {% for key,attribute in field.attributes %}
  109. {% if attribute|of_type('array') %}
  110. {{ attribute.name }}="{{ attribute.value|e('html_attr') }}"
  111. {% else %}
  112. {{ key }}="{{ attribute|e('html_attr') }}"
  113. {% endif %}
  114. {% endfor %}
  115. {% endif %}
  116. {# Support for Custom data attributes#}
  117. {% if field.datasets %}
  118. {% for key, attribute in field.datasets %}
  119. data-{{ key }}="{{ attribute|e('html_attr') }}"
  120. {% endfor %}
  121. {% endif %}
  122. {% endblock %}
  123. {% endif %}