{% set originalValue = originalValue is defined ? originalValue : value %}
{% set toggleableChecked = field.toggleable and (originalValue is not null and originalValue is not empty) %}
{% set isDisabledToggleable = field.toggleable and not toggleableChecked %}
{% set value = (value is null ? field.default : value) %}
{% set vertical = field.style == 'vertical' %}
{% set field_name = (scope ~ field.name)|fieldName %}

{% if (field.yaml or field.validate.type == 'yaml') and value is iterable%}
    {% set value = value|toYaml %}
{% endif %}

{% block field %}
    <div class="form-field grid{% if vertical %} vertical{% endif %}{% if field.toggleable %} form-field-toggleable{% endif %} {{ field.outerclasses }} {{ field.field_classes }}">
        {% block contents %}
            <div class="form-label{% if not vertical %} block size-1-3{% endif %}">
                {% if field.toggleable %}
                    <span class="checkboxes toggleable" data-grav-field="toggleable" data-grav-field-name="{{ field_name }}">
                        <input type="checkbox"
                               id="toggleable_{{ field.name }}"
                               {% if toggleableChecked %}value="1"{% endif %}
                               name="toggleable_{{ field_name }}"
                               {% if toggleableChecked %}checked="checked"{% endif %}
                        >
                        <label for="toggleable_{{ field.name }}"></label>
                    </span>
                {% endif %}
                <label{{ (field.toggleable ? ' class="toggleable" for="toggleable_' ~ field.name ~ '"')|raw }}>
                {% block label %}
                    {% if field.help %}
                        {% if field.markdown %}
                            <span class="hint--bottom" data-hint="{{ field.help|tu|markdown(false)|e('html') }}">{{ field.label|tu|markdown(false)|raw }}</span>
                        {% else %}
                            <span class="hint--bottom" data-hint="{{ field.help|tu|e('html') }}">{{ field.label|tu|raw }}</span>
                        {% endif %}
                    {% else %}
                        {% if field.markdown %}
                            {{ field.label|tu|markdown(false)|raw }}
                        {% else %}
                            {{ field.label|tu|raw }}
                        {% endif %}
                    {% endif %}
                    {{ field.validate.required in ['on', 'true', 1] ? '<span class="required">*</span>' }}
                {% endblock %}
                </label>
                {% if field.sublabel %}
                <div class="form-sublabel">
                    {% if field.markdown %}
                        {{ field.sublabel|tu|markdown(false)|raw }}
                    {% else %}
                        {{ field.sublabel|tu|raw }}
                    {% endif %}
                </div>
                {% endif %}
            </div>
            <div class="form-data{% if not vertical %} block size-2-3{% endif %}"
                {% block global_attributes %}
                data-grav-field="{{ field.type }}"
                data-grav-disabled="{{ toggleableChecked }}"
                data-grav-default="{{ field.default|json_encode|e('html_attr') }}"
                {% endblock %}
            >
                {% block group %}
                    {% block input %}
                        <div class="form-input-wrapper {{ field.size }} {{ field.wrapper_classes }}">
                            {% block prepend %}{% endblock prepend %}
                            <input
                                {# required attribute structures #}
                                name="{{ field_name }}"
                                value="{{ value|join(', ') }}"
                                {% if field.key %}
                                    data-key-observe="{{ (scope ~ field_name)|fieldName }}"
                                {% endif %}
                                {# input attribute structures #}
                                {% block input_attributes %}
                                    {% if field.classes is defined %}class="{{ field.classes }}" {% endif %}
                                    {% if field.id is defined %}id="{{ field.id|e }}" {% endif %}
                                    {% if field.style is defined %}style="{{ field.style|e }}" {% endif %}
                                    {% if field.disabled or isDisabledToggleable %}disabled="disabled"{% endif %}
                                    {% if field.placeholder %}placeholder="{{ field.placeholder|tu }}"{% endif %}
                                    {% if field.autofocus in ['on', 'true', 1] %}autofocus="autofocus"{% endif %}
                                    {% if field.novalidate in ['on', 'true', 1] %}novalidate="novalidate"{% endif %}
                                    {% if field.readonly in ['on', 'true', 1] %}readonly="readonly"{% endif %}
                                    {% if field.autocomplete in ['on', 'off', 'new-password'] %}autocomplete="{{ field.autocomplete }}"{% endif %}
                                    {% if field.validate.required in ['on', 'true', 1] %}required="required"{% endif %}
                                    {% if field.validate.pattern %}pattern="{{ field.validate.pattern }}"{% endif %}
                                    {% if field.validate.message %}title="{{ field.validate.message|e|tu }}"
                                    {% elseif field.title is defined %}title="{{ field.title|e|tu }}" {% endif %}
                                {% endblock %}
                            />
                            {% block append %}{% endblock append %}
                        </div>
                    {% endblock %}
                {% endblock %}
                {% if field.description %}
                    <div class="form-extra-wrapper {{ field.size }} {{ field.wrapper_classes }}">
                        <span class="form-description">
                            {% if field.markdown %}
                                {{ field.description|tu|markdown(false)|raw }}
                            {% else %}
                                {{ field.description|tu|raw }}
                            {% endif %}
                        </span>
                    </div>
                {% endif %}
            </div>
        {% endblock %}
    </div>
{% endblock %}
