Using custom enum select picker for Wiki Macro parameters

Last modified by Vincent Massol on 2024/02/20

There might be cases where you want to display a Wiki Macro parameter as a select picker of a set of possible values. However, for this you will need a Java Enum defined somewhere in the wiki, that will correspond to those possible values your parameter can take. The easiest way to have that (for now) is to create a Java Enum and bundle it as a jar dependency of your project.

Here is, for example, how to implement a custom Ascending / Descending select picker for a sort order parameter.

Step 1: Java enum

Write your custom java enum:

package your.enum.package;

public enum SortOrderEnum
{
    asc,
    desc;
}

The values you choose for your enum will correspond to the values saved for your parameter. They are the one that will end up being editable from the wiki editor, so you might prefer using camelCase values instead of ALL_CAPS.

Then bundle it as a jar, and import it in your instance (by manually placing it in the WEB-INF/lib directory, or using the extension manager).

Step 2: Wiki Macro parameter type

Simply set your parameter type to your.enum.package.SortOrderEnum in the Wiki Macro page.

The enum picker will be automatically used in the wysiwyg editor when editing your macro parameter.

Step 3: Translations

For now, values are being displayed as is. To display a custom title for each value, you'll have to setup translation keys.

Create a page with a XWiki.TranslationDocumentClass object, where translation keys are the full name of your enum concatenated with the values:

your.enum.package.SortOrderEnum.asc=Ascending
your.enum.package.SortOrderEnum.desc=Descending

Get Connected