{## # Selected IDs will be always present, initially it will be whatever is stored in the field # It may be a comma separated string, so we need to split that into an array #} {% if selectedIds is not defined %} {% set selectedIds = [] %} {% endif %} {% if not sprig.isRequest %} {% set selectedIds = selectedIds|merge({ (_fieldHandle): _originalIds }) %} {% endif %} {% if selectedIds[_fieldHandle] is not iterable %} {% if selectedIds[_fieldHandle] is not empty %} {% set selectedIds = selectedIds|merge({ (_fieldHandle): selectedIds[_fieldHandle]|split(',') }) %} {% else %} {% set selectedIds = selectedIds|merge({ (_fieldHandle): [] }) %} {% endif %} {% endif %} {# If the field values are submitted, then we need to process them first #} {% if fields is defined and fields[_fieldHandle] %} {% set combinedIds = fields[_fieldHandle] %} {% for id in selectedIds[_fieldHandle] %} {% if id not in combinedIds %} {% set combinedIds = combinedIds|push(id) %} {% endif %} {% endfor %} {% set selectedIds = selectedIds|merge({ (_fieldHandle): combinedIds }) %} {% endif %} {# Process the removed ID if there is one #} {% if removedId is defined %} {% set removedCat = craft.categories.id(removedId).one() %} {% set childIds = removedCat.descendants.ids() %} {% set filterIds = childIds|push(removedCat.id) %} {% set selectedIds = selectedIds|merge({ (_fieldHandle): selectedIds[_fieldHandle]|filter((v, k) => v not in filterIds) }) %} {% endif %} {# Now we can fetch the categories #} {% set categories = [] %} {% if selectedIds[_fieldHandle] %} {# TODO: multi-site #} {% set categories = craft.categories.id(selectedIds[_fieldHandle]).anyStatus().all() %} {# Enforce branch limit #} {% set categories = market.prepStructuredElementsForField(categories, _branchLimit) %} {# Write over the selectedIds var with the prepared categories #} {% set selectedIds = selectedIds|merge({ (_fieldHandle): categories|map(c => c.id) }) %} {% endif %} {## # At this point we need to store them in a field as a string so we can update # this from the asset index child component and trigger a refresh #} {{ hiddenInput('selectedIds['~_fieldHandle~']', selectedIds[_fieldHandle]|join(','), { id: 'field-'~_fieldId~'-selectedIds' }) }}
{# Output the selected categories #} {# Category browser #}
{# Button and spinner #}
{# Button to trigger it #}
{# The actual category browser / index #}
{# Overlay #} {# Panel #}
{# Panel header #}

Add a category

{# Middle and footer #} {{ sprig('_market/_components/categories/field-index', { _fieldId: _fieldId, _fieldHandle: _fieldHandle, _branchLimit: _branchLimit, _groupUid: _groupUid, _originalIds: selectedIds[_fieldHandle]|join(','), }, { 'id' : 'field-'~_fieldId~'-categories-index', 'class' : 'flex flex-col h-full field-categories-index', 'x-on:htmx:after-swap' : 'applyScroll' }) }}