"attachmentGalleryPicker" Macro

Last modified by Eleni Cojocariu on 2026/09/23 12:40

Reference

Description

The attachmentGalleryPicker macro displays a search field and a grid of the attachments it finds, from which the reader picks one. It is the building block for a form that asks for an attachment: an image to illustrate an entry, a document to link from an application. In the macro list of the editors it appears as "Attachment Picker", in the "Development" category.

Each result shows the name of the attachment under a preview of it: the image itself when the attachment is one, the icon of its media type otherwise. When the reader searches every Page, the attachments of the current Page are listed first. InformationThe results come from the search index. An attachment that has not been indexed yet is not among them.

The reader chooses where to search: in the current Page only, or in every Page they are allowed to view. Picking an attachment that does not belong to the current Page displays a warning, because an application storing that reference keeps it pointing at the other Page.

attachment-picker-global-selection-warning.png

To ask for an attachment from a form rather than from a grid, use the Suggest Attachments widget, which turns a text input into an attachment suggester, or the attachmentSelector macro, which writes the choice into an xobject property.

Usage

{{attachmentGalleryPicker/}}

{{attachmentGalleryPicker id="myid"/}}

{{attachmentGalleryPicker filter="image/*"/}}

{{attachmentGalleryPicker limit="5"/}}

{{attachmentGalleryPicker target="Space.Page"/}}

The macro takes no content, and works standalone only: written inside a paragraph it reports an error instead of rendering the picker.

Give the macro an id when the surrounding application has to know which picker it is talking to: the id is set on the element the macro renders, which is how a script reaches this picker among several, and how it listens to the selection events described below.

Parameters

NameMandatoryAllowed ValuesDefault ValueDescription
idnoA valid HTML idnoneSet on the element the macro renders, so that a script can reach this picker and listen to its events.
filternoA comma-separated list of media types, wildcards included, such as image/*,application/pdfnoneRestricts the grid to the attachments whose media type matches one of them. Without it, every attachment is a candidate.
limitnoA positive integer20The maximum number of attachments displayed at once.
targetnoA document referenceThe current documentThe Page the picker treats as the current one, which decides what the "current page" scope searches and which selections raise the warning.

Examples

Pick Any Attachment of the Wiki

Without a parameter, the picker searches every attachment the reader is allowed to see.

{{attachmentGalleryPicker/}}

attachment-picker-default.png

Pick an Image

A form asking for an illustration accepts images only. The filter is applied to the media type of the attachment, so image/* keeps every image format.

{{attachmentGalleryPicker id="illustration" filter="image/*"/}}

attachment-picker-image-filter.png

A filter matching nothing leaves the grid empty, and the picker says so rather than showing an empty area:

attachment-picker-empty-grid.png

Pick a Document

An attachment that is not an image is shown with the icon of its media type instead of a preview.

{{attachmentGalleryPicker filter="application/pdf,text/plain" limit="5"/}}

attachment-picker-icon-fallback.png

Pick From Another Page

An application editing one Page from another needs the picker to work on the Page being edited, not on the Page carrying the form. The target parameter says which one that is, and the "current page" scope and the warning then follow it.

{{attachmentGalleryPicker target="Sandbox.WebHome"/}}

React to the Selection From JavaScript

The picker reports what the reader does through two events, sent on the element the macro renders:

EventSent whenParameter
xwiki:attachmentGalleryPicker:selectedAn attachment is selectedThe reference of the selected attachment
xwiki:attachmentGalleryPicker:unselectedThe selection is clearednone

Give the macro an id and listen on it:

{{attachmentGalleryPicker id="myid"/}}
require(['jquery'], function($) {
  // Selects the picker by the id given to the macro.
  const picker = $('#myid');

  picker.on('xwiki:attachmentGalleryPicker:selected', function(event, attachmentReference) {
    // Store or display the reference of the attachment the reader picked.
  });

  picker.on('xwiki:attachmentGalleryPicker:unselected', function() {
    // React to the reader clearing their selection.
  });
});

Related

Get Connected