You’ve always been able to provide a select list for your configuration options
by entering each label and value directly in the @Meta.AD
annotation of the
Configuration interface.
@Meta.AD(
deflt = "enabled-with-warning", name = "csv-export",
optionLabels = {"enabled", "enabled-with-warning", "disabled"},
optionValues = {"enabled", "enabled-with-warning", "disabled"},
required = false
)
public String csvExport();
Now, thanks to the ConfigurationFieldOptionsProvider
interface,
you can populate select list configurations dynamically, using custom logic.
Follow these steps to populate the select list fields dynamically in your configuration UI:
-
Use an
@Component
annotation to register theConfigurationFieldOptionsProvider.class
service and include two properties:configuration.field.name
: The name of the attribute in the configuration interfaceconfiguration.pid
: The ID of the corresponding configuration interface (usually the fully qualified class name)For example,
@Component( property = { "configuration.field.name=enabledClassNames", "configuration.pid=com.liferay.asset.auto.tagger.google.cloud.natural.language.internal.configuration.GCloudNaturalLanguageAssetAutoTaggerCompanyConfiguration", "configuration.pid=com.liferay.asset.auto.tagger.opennlp.internal.configuration.OpenNLPDocumentAssetAutoTaggerCompanyConfiguration" }, service = ConfigurationFieldOptionsProvider.class )
-
Implement the
ConfigurationFieldOptionsProvider
interface:public class MyConfigurationFieldOptionsProvider implements ConfigurationFieldOptionsProvider { .. }
-
The
getOptions
method returns a list ofOption
s consisting of the label and value fields. The labels provided here are translated tooptionLabels
, and the values asoptionValues
, in the configuration interface.public List<Option> getOptions() { List<AssetRendererFactory<?>> assetRendererFactories = AssetRendererFactoryRegistryUtil.getAssetRendererFactories( CompanyThreadLocal.getCompanyId()); Stream<AssetRendererFactory<?>> stream = assetRendererFactories.stream(); return stream.filter( assetRendererFactory -> { TextExtractor textExtractor = _textExtractorTracker.getTextExtractor( assetRendererFactory.getClassName()); return textExtractor != null; } ).map( assetRendererFactory -> new Option() { @Override public String getLabel(Locale locale) { return assetRendererFactory.getTypeName(locale); } @Override public String getValue() { return assetRendererFactory.getClassName(); } } ).collect( Collectors.toList() ); }
This code gets a list of
AssetRendererFactory
objects and iterates through the list, populating a new list ofOption
s, using the asset’s type name as the label and the class name as the value. It comes from theEnabledClassNamesConfigurationFieldOptionsProvider
, which populates the configuration field labeled Enable Google Cloud Natural Language Text Auto Tagging For with all the asset types that have registered aTextExtractor
.Figure 1: The select list in the Google Cloud Natural Language Text Auto Tagging entry is populated programmatically, using the `ConfigurationFieldOptionsProvider`.
The ConfigurationFieldOptionsProvider
allows you to populate select lists with
configuration options defined by your custom logic.