{# Reusable image-picker field for admin forms. Renders a hidden input + inline thumbnail (when set) + Pick / Change / Clear buttons + a per-field that lazy-loads the shared attachments picker grid via htmx. Card-click on the grid sets the hidden input, updates the thumbnail, and closes the dialog. Use via {% from "admin/_image_picker_field.html" import image_picker_field %} then `{{ image_picker_field(name=..., value=..., attachment=..., label=..., picker_url=..., site_slug=..., helper=..., thumb_storage_key=...) }}`. One form-field per page expected; the dialog id is derived from `name` so multiple fields could coexist in principle. `picker_url` is the fully-built attachments-picker URL (site_slug already substituted by the caller). The macro can't build it itself because the site-edit page has no `site_slug` in its view args — only post/page edit views do. `site_slug` lets the macro build the site-scoped attachment-bytes URLs for the inline thumbnail and for the after-pick preview swap. The delivery `/attachments/` route resolves the site from the Host header, which doesn't match on the admin host, so the macro must use the path-scoped admin route instead. `thumb_storage_key` (optional) overrides the storage key used for the inline preview image. The caller passes the smallest done WebP rendition's storage_key so the form doesn't drag the full original (often >1 MB) over the wire to render a 160x120 thumbnail. Falls back to `attachment.storage_key` when None, which keeps the macro working for attachments whose renditions haven't processed yet. #} {% macro image_picker_field(name, value, attachment, label, picker_url, site_slug, helper="", thumb_storage_key=None) %} {%- set dialog_id = "image-picker-dialog--" ~ name -%} {%- set field_id = "image-picker-field--" ~ name -%}
{% if attachment %} {{ attachment.alt_text or attachment.filename }} {{ attachment.filename }} {% endif %}
{% if helper %}{{ helper }}{% endif %}

Pick an image

Loading…
{% endmacro %}