<commit_before>{% import '_includes/forms' as forms %}

{% set nameLabel = nameLabel ?? name ?? null %}
{% set handle = handle ?? null %}
{% set instructions = instructions ?? null %}
{% set instructionsHandle = instructionsHandle ?? handle ?? null %}

{% set default = default ?? null %}
{% set required = required ?? null %}
{% set attribute = attribute ?? null %}
{% set field = field ?? null %}
{% set fieldClass = fieldClass ?? null %}

{# Variables to help with fetching value/defaults and namespacing. Should provide an array that reflects the #}
{# mapping data structure. For normal fields, this is just the handle, but Matrix Table, and others are more complex #}
{# ie: matrixField.blocks.blockType.fields.field.node - woah! #}
{% set path = path ?? [handle] %}
{% set namespacePath = path | join('][') %}
{% set nodePath = path | join('.') ~ '.node' %}
{% set defaultPath = path | join('.') ~ '.default' %}
{% set optionsPath = path | join('.') ~ '.options' %}

{% set namespace = 'fieldMapping[' ~ namespacePath ~ ']' %}
{% set value = hash_get(feed.fieldMapping, nodePath) ?: null %}
{% set defaultValue = hash_get(feed.fieldMapping, defaultPath) ?: null %}

{% set classes = classes ?? [] %}
{% set elementFields = elementFields ?? null %}
{% set isSubElementField = isSubElementField ?? null %}

{% if isSubElementField %}
    {% set classes = classes | merge([ 'element-sub-field' ]) %}
{% endif %}

<tr class="{{ classes | join(' ') }}">
    <td class="col-field">
        <div class="field">
            {% if isSubElementField %}
                <i class="fa fa-level-up" aria-hidden="true"></i>
            {% endif %}

            <div class="heading">
                <label class="{{ required ? 'required' }}">{{ nameLabel | t('site') }}</label>

                {% if attribute and instructions %}
                    <div class="instructions">
                        <p>{{ instructions | raw }}</p>
                    </div>
                {% endif %}

                {% if fieldClass and instructionsHandle %}
                    <div class="instructions">
                        <code>{{ instructionsHandle }}</code>
                    </div>
                {% endif %}

                {% if elementFields | length %}
                    <div class="subelement-toggle">
                        {{ forms.lightswitchField({
                            small: true,
                            label: 'Element fields ({num})' | t('feed-me', { num: elementFields | length }),
                        }) }}
                    </div>
                {% endif %}
            </div>

            <div class="additional-mapping-fields">
                {% namespace namespace %}
                    {% block fieldSettings %}
                        {% if attribute %}
                            <input type="text" name="attribute" value="true">
                        {% endif %}

                        {% if field and fieldClass %}
                            <input type="text" name="field" value="{{ className(field) }}">
                        {% endif %}
                    {% endblock %}
                {% endnamespace %}

                {% block additionalFieldSettings %}

                {% endblock %}
            </div>
        </div>
    </td>

    <td class="col-map">
        {% namespace namespace %}
            {{ forms.selectField({
                name: 'node',
                value: value,
                options: feedData,
                class: 'selectize fullwidth',
            }) }}

            <div class="field-extra-settings">
                {% block extraSettings %}

                {% endblock %}
            </div>
        {% endnamespace %}
    </td>

    <td class="col-default">
        {% namespace namespace %}
            <div class="default-fields">
                {% block defaultSettings %}

                    {% if default.type is defined %}
                        {% if default.type == 'text' %}

                            {{ forms.textField({
                                name: 'default',
                                value: defaultValue,
                            }) }}

                        {% elseif default.type == 'select' %}

                            {{ forms.selectField({
                                name: 'default',
                                class: '',
                                value: defaultValue,
                                options: default.options,
                            }) }}

                        {% elseif default.type == 'checkbox' %}

                            {{ forms.checkboxField({
                                name: 'default',
                                checked: defaultValue,
                            }) }}

                        {% elseif default.type == 'checkboxGroup' %}

                            {{ forms.checkboxGroup({
                                name: 'default',
                                values: defaultValue,
                                options: default.options,
                            }) }}

                        {% elseif default.type == 'dateTime' %}

                            {{ forms.dateTimeField({
                                name: 'default',
                                value: craft.feedme.formatDateTime(defaultValue),
                            }) }}

                        {% elseif default.type == 'lightswitch' %}

                            {{ forms.lightswitchField({
                                name: 'default',
                                checked: defaultValue,
                            }) }}

                        {% elseif default.type == 'elementselect' %}

                            {% set elements = [] %}

                            {% for elementId in defaultValue %}
                                {% set elements = elements | merge([ craft.app.elements.getElementById(elementId) ]) %}
                            {% endfor %}

                            {{ forms.elementSelectField({
                                id: 'default',
                                name: 'default',
                                elementType: default.options.elementType,
                                selectionLabel: default.options.selectionLabel,
                                elements: elements,
                            }) }}

                        {% endif %}
                    {% endif %}

                {% endblock %}
            </div>
        {% endnamespace %}
    </td>
</tr>

{% block elementFields %}
    {% if field is defined and elementFields | length %}
        {% for elementField in elementFields %}
            {% set fieldClass = craft.feedme.fields.getRegisteredField(className(elementField)) %}
            {% set template = fieldClass.getMappingTemplate() %}

            {% if parentPath is defined %}
                {% set path = parentPath | merge([ 'fields', elementField.handle ]) %}
            {% else %}
                {% set path = [ handle, 'fields', elementField.handle ] %}
            {% endif %}

            {# Be smart about what we include to child field templates #}
            {% include template ignore missing with {
                name: elementField.name,
                handle: elementField.handle,
                instructionHandle: elementField.handle,
                isSubElementField: true,
                path: path,

                feed: feed,
                feedData: feedData,
                field: elementField,
                fieldClass: fieldClass,
                feedData: feedData,
            } only %}
        {% endfor %}
    {% endif %}
{% endblock %}
<commit_msg>Fix default value and values not respecting falsey values in field mapping<commit_after>{% import '_includes/forms' as forms %}

{% set nameLabel = nameLabel ?? name ?? null %}
{% set handle = handle ?? null %}
{% set instructions = instructions ?? null %}
{% set instructionsHandle = instructionsHandle ?? handle ?? null %}

{% set default = default ?? null %}
{% set required = required ?? null %}
{% set attribute = attribute ?? null %}
{% set field = field ?? null %}
{% set fieldClass = fieldClass ?? null %}

{# Variables to help with fetching value/defaults and namespacing. Should provide an array that reflects the #}
{# mapping data structure. For normal fields, this is just the handle, but Matrix Table, and others are more complex #}
{# ie: matrixField.blocks.blockType.fields.field.node - woah! #}
{% set path = path ?? [handle] %}
{% set namespacePath = path | join('][') %}
{% set nodePath = path | join('.') ~ '.node' %}
{% set defaultPath = path | join('.') ~ '.default' %}
{% set optionsPath = path | join('.') ~ '.options' %}

{% set namespace = 'fieldMapping[' ~ namespacePath ~ ']' %}
{% set value = hash_get(feed.fieldMapping, nodePath) %}
{% set defaultValue = hash_get(feed.fieldMapping, defaultPath) %}

{% set classes = classes ?? [] %}
{% set elementFields = elementFields ?? null %}
{% set isSubElementField = isSubElementField ?? null %}

{% if isSubElementField %}
    {% set classes = classes | merge([ 'element-sub-field' ]) %}
{% endif %}

<tr class="{{ classes | join(' ') }}">
    <td class="col-field">
        <div class="field">
            {% if isSubElementField %}
                <i class="fa fa-level-up" aria-hidden="true"></i>
            {% endif %}

            <div class="heading">
                <label class="{{ required ? 'required' }}">{{ nameLabel | t('site') }}</label>

                {% if attribute and instructions %}
                    <div class="instructions">
                        <p>{{ instructions | raw }}</p>
                    </div>
                {% endif %}

                {% if fieldClass and instructionsHandle %}
                    <div class="instructions">
                        <code>{{ instructionsHandle }}</code>
                    </div>
                {% endif %}

                {% if elementFields | length %}
                    <div class="subelement-toggle">
                        {{ forms.lightswitchField({
                            small: true,
                            label: 'Element fields ({num})' | t('feed-me', { num: elementFields | length }),
                        }) }}
                    </div>
                {% endif %}
            </div>

            <div class="additional-mapping-fields">
                {% namespace namespace %}
                    {% block fieldSettings %}
                        {% if attribute %}
                            <input type="text" name="attribute" value="true">
                        {% endif %}

                        {% if field and fieldClass %}
                            <input type="text" name="field" value="{{ className(field) }}">
                        {% endif %}
                    {% endblock %}
                {% endnamespace %}

                {% block additionalFieldSettings %}

                {% endblock %}
            </div>
        </div>
    </td>

    <td class="col-map">
        {% namespace namespace %}
            {{ forms.selectField({
                name: 'node',
                value: value,
                options: feedData,
                class: 'selectize fullwidth',
            }) }}

            <div class="field-extra-settings">
                {% block extraSettings %}

                {% endblock %}
            </div>
        {% endnamespace %}
    </td>

    <td class="col-default">
        {% namespace namespace %}
            <div class="default-fields">
                {% block defaultSettings %}

                    {% if default.type is defined %}
                        {% if default.type == 'text' %}

                            {{ forms.textField({
                                name: 'default',
                                value: defaultValue,
                            }) }}

                        {% elseif default.type == 'select' %}

                            {{ forms.selectField({
                                name: 'default',
                                class: '',
                                value: defaultValue,
                                options: default.options,
                            }) }}

                        {% elseif default.type == 'checkbox' %}

                            {{ forms.checkboxField({
                                name: 'default',
                                checked: defaultValue,
                            }) }}

                        {% elseif default.type == 'checkboxGroup' %}

                            {{ forms.checkboxGroup({
                                name: 'default',
                                values: defaultValue,
                                options: default.options,
                            }) }}

                        {% elseif default.type == 'dateTime' %}

                            {{ forms.dateTimeField({
                                name: 'default',
                                value: craft.feedme.formatDateTime(defaultValue),
                            }) }}

                        {% elseif default.type == 'lightswitch' %}

                            {{ forms.lightswitchField({
                                name: 'default',
                                checked: defaultValue,
                            }) }}

                        {% elseif default.type == 'elementselect' %}

                            {% set elements = [] %}

                            {% for elementId in defaultValue %}
                                {% set elements = elements | merge([ craft.app.elements.getElementById(elementId) ]) %}
                            {% endfor %}

                            {{ forms.elementSelectField({
                                id: 'default',
                                name: 'default',
                                elementType: default.options.elementType,
                                selectionLabel: default.options.selectionLabel,
                                elements: elements,
                            }) }}

                        {% endif %}
                    {% endif %}

                {% endblock %}
            </div>
        {% endnamespace %}
    </td>
</tr>

{% block elementFields %}
    {% if field is defined and elementFields | length %}
        {% for elementField in elementFields %}
            {% set fieldClass = craft.feedme.fields.getRegisteredField(className(elementField)) %}
            {% set template = fieldClass.getMappingTemplate() %}

            {% if parentPath is defined %}
                {% set path = parentPath | merge([ 'fields', elementField.handle ]) %}
            {% else %}
                {% set path = [ handle, 'fields', elementField.handle ] %}
            {% endif %}

            {# Be smart about what we include to child field templates #}
            {% include template ignore missing with {
                name: elementField.name,
                handle: elementField.handle,
                instructionHandle: elementField.handle,
                isSubElementField: true,
                path: path,

                feed: feed,
                feedData: feedData,
                field: elementField,
                fieldClass: fieldClass,
                feedData: feedData,
            } only %}
        {% endfor %}
    {% endif %}
{% endblock %}

{'fraction_non_alphanumeric': 0.12360059535365302, 'fraction_numerical': 0.0, 'mean_word_length': 1.1864742501414827, 'pattern_counts': {'":': 0, '<': 71, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1874706', 'n_tokens_mistral': 3834, 'n_tokens_neox': 3286, 'n_words': 865}