Files
core/templates/_partials/fields/_base.html.twig
T
2020-01-17 11:18:51 +01:00

113 lines
3.6 KiB
Twig

{%- apply spaceless -%}
{# This template fragment is used to "extend" the different fields, used in
Bolt's backend. Most of the values get passed in, either through a `Field`,
or otherwise through parameters.
The used variables are: ("P" is parameter, "F" comes from a `Field`)
- type: P - The type of the field
- variant: F - the "variant" of the field, if applicable
- name: P+F - The `name` attribute of the <input> field
- id: P+F - The `id` attribute of the <input> field
- class: P+F - The `class` attribute of the <input> field
- form: P - The `form` attribute of the <input>, defaults to 'editcontent'
- value: P+F - The default value if the <input> field
- label: P+F - Label used as visible anchor to the <input> field
- placeholder: P+F - Placeholder text to use in the <input>
- prefix: P+F - Short text to display as a prefix before a field
- postfix: P+F - Short text to display as a postfix after a field
#}
{# Set type #}
{% set type = field.type|default %}
{% set type_collection = (type in ['collection']) %}
{% set type_set = (type in ['imagelist', 'filelist', 'set']) %}
{# Set variant #}
{% set variant = field.definition.variant|default('normal') %}
{# Set the name #}
{% if not name|default %}
{% set name = 'fields[' ~ field.name|default('unnamed') ~ ']' %}
{% endif %}
{# Set name[value] if id is needed #}
{% if include_id is defined %}
{% set original_name = name %}
{% set name = name ~ '[value]' %}
{% endif %}
{# Set the id #}
{% if not id|default %}
{% set id = 'field-' ~ field.name|default(name)|default('unnamed') %}
{% endif %}
{# Set the form #}
{% if not form|default %}
{% set form = 'editcontent' %}
{% endif %}
{# Set the value #}
{% if not value|default %}
{% set value = field.value|default('') %}
{% if value is iterable and field|type != "select" %}
{% set value = value|first %}
{% endif %}
{% endif %}
{# Set the class #}
{% if not class|default %}
{% set class = field.definition.class|default %}
{% endif %}
{# Set the label #}
{% if not label|default %}
{% set label = field.definition.label|default(field.name|default('unnamed')|ucwords) %}
{% endif %}
{# Set the placeholder #}
{% if not placeholder|default %}
{% set placeholder = field.definition.placeholder|default('') %}
{% endif %}
{# Set the locale #}
{% set localize = field.definition.localize|default %}
{# Set the prefix and postfix attributes #}
{% set prefix = prefix|default(field.definition.prefix|default) %}
{% if prefix and not ('<p' in prefix or '<span' in prefix or '<div' in prefix) %}
{% set prefix = '<span id="' ~ id ~ '_prefix" class="form--helper">' ~ prefix ~ '</span>' %}
{% endif %}
{% set postfix = postfix|default(field.definition.postfix|default) %}
{% if postfix and not ('<p' in postfix or '<span' in postfix or '<div' in postfix) %}
{% set postfix = '<span id="' ~ id ~ '_postfix" class="form--helper">' ~ postfix ~ '</span>' %}
{% endif %}
{%- endapply -%}
<!-- field "{{ label }}" (type: {{ type }}{% if variant != 'normal' %}, variant: {{ variant }}{% endif %}) -->
<div
class="form-group {{ type_set ? 'form-set' }} {{ type_collection ? 'form-collection' }} is-{{ variant }}"
id="field-{{ type }}-{{ field.name|default('unnamed') }}"
>
{{ prefix|raw }}
{% block label %}
{% include '@bolt/_partials/fields/_label.html.twig' %}
{% include '@bolt/_partials/fields/_collection_buttons.html.twig' %}
{% endblock %}
{% block field %}
{% endblock %}
{% if include_id is defined %}
{% include '@bolt/_partials/fields/_hidden_id_field.html.twig' %}
{% endif %}
{{ postfix|raw }}
</div>