Dynamically Populating Select List Fields in the Configuration UI

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:

  1. Use an @Component annotation to register the ConfigurationFieldOptionsProvider.class service and include two properties:

    configuration.field.name: The name of the attribute in the configuration interface

    configuration.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
    )
    
  2. Implement the ConfigurationFieldOptionsProvider interface:

    public class MyConfigurationFieldOptionsProvider implements 
    ConfigurationFieldOptionsProvider {
        ..
    }
    
  3. The getOptions method returns a list of Options consisting of the label and value fields. The labels provided here are translated to optionLabels, and the values as optionValues, 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 of Options, using the asset’s type name as the label and the class name as the value. It comes from the EnabledClassNamesConfigurationFieldOptionsProvider, which populates the configuration field labeled Enable Google Cloud Natural Language Text Auto Tagging For with all the asset types that have registered a TextExtractor.

    Figure 1: The select list in the Google Cloud Natural Language Text Auto Tagging entry is populated programmatically, using the ConfigurationFieldOptionsProvider.

    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.

« Upgrading a Legacy AppIntroduction to Content Publication Management »
Was this article helpful?
0 out of 0 found this helpful