{# THIS FILE REPLACES THE DEFAULT SELECT TEMPLATE OF BOLT # This file is a copy of Bolt Version 3.5.3 # Changes: # - added 'initial' configuration values - search 'CUSTOM-INITIAL' in this file #} {#=== OPTIONS ========================================================================================================#} {% set option = { info: field.info|default(''), label: field.label, multiple: (field.multiple is defined and field.multiple), sortable: (field.sortable is defined and field.sortable), required: field.required|default(false), values: field.values|default([]), default: field.default|default(null), } %} {#=== INIT ===========================================================================================================#} {# Conditional is an ugly hack to be removed with Forms cutover #} {% if values is not defined %} {% set values = context.values.select_choices[contentkey]|default([]) %} {% endif %} {# Get the current selection. Either a single value, or an array. #} {% set selection = context.content.get(contentkey)|default(option.default) %} {# Make sure the value is either `null` (for empty), or cast to an array for a string. Prevent breakage when switching from 'single' to 'mulptiple' #} {% if selection is not iterable and selection is not empty %} {% set selection = [ selection ] %} {% endif %} {# If the current selection contains an existing id, we must use _only_ the id, and not accept a fallback. #} {% set onlyids = selection|first in values|keys %} {# Build the select options array, in two steps. #} {% set options = [] %} {# Step 1: If we have a current selection, we need to add these _first_, maintaining their order #} {% for id in selection %} {% if values[id]|default() %} {% set options = options|merge([{ 'value': id, 'text': values[id], 'selected': true, }]) %} {% endif %} {% endfor %} {# Step 2: Next, add all the values, _not_ in the selection. For new records, or records with no selection, this will add them all. #} {% for id, value in values if (id not in selection) %} {# CUSTOM-INITIAL - This section adds selected values from config 'initial' but only if empty and crerating a new record! #} {% set selected = (context.content.id == null) and (selection == false) and (id in field.initial|default([])) %} {% set options = options|merge([{ 'value': id, 'text': value, 'selected': selected, }]) %} {% endfor %} {# BUIC options #} {% set buic_opt_select = { 'all': option.multiple, 'clear': true, 'id': key, 'multiple': option.multiple, 'name': option.multiple ? name ~ '[]' : name, 'options': options, 'required': option.required, } %} {#=== FIELDSET =======================================================================================================#} {% extends '@bolt/_base/_fieldset.twig' %} {% block fieldset_type 'select' %} {% block fieldset_widget 'fieldSelect' %} {% set fieldset_conf = { autocomplete: field.autocomplete|default(false), sortable: field.sortable|default(false) } %} {% block fieldset_label_text labelkey %} {% block fieldset_label_info option.info %} {% block fieldset_label_class 'col-sm-3' %} {% block fieldset_label_for key %} {% block fieldset_controls %} {% from '@bolt/_buic/_select.twig' import buic_select %}