-
Templating and Integration Guide
- Part 1: Concepts
- Part 2: Modules
- Part 3: Templates and Studio
- Part 4: Content Definitions and Views
- Part 5: Advanced Techniques
- Part 6: Setting up an integration environment
- Part 7: Step by step integration
- Further reading and resources
Initializers
ChoiceList initializers allow developers to extend the way dropdown lists (or combobox) are populated when end users are editing / creating content.
Jahia allows specifying in definitions that a property must use a choice list (which will be rendered by a dropdown list) to select its value.
The simplest example is to define a choice list for the selector and to list the allowed values in the constraint of the definition:
[cnt:dog] - coat (string, choicelist) < long, short, medium
This example works well, but values in the drop down list will be displayed exactly as they are declared in the CND file. For internationalized websites, or to provide more self-speaking entries in the dropdown list, we can use resource bundles to modify the choice list rendering
[cnt:dog] - coat (string, choicelist[resourceBundle]) < long, short, medium
In this case, Jahia will use the resource bundle files located in the same module as the CND file and will treat the parameters (long, short, medium) as resource bundle keys and therefore replace them by the specified values in the resource bundle files.
How do ChoiceList Initializers work ?
ChoiceList Initializers are implementations of the <<<ChoiceListInitializer>>> interface. The ChoiceListInitializerService manages all implementations.
This service is defined in the <<<applicationcontext-jcr.xml>>> file:
<bean id="rbInitializer" class="org.jahia.services.content.nodetypes.initializers.ResourceBundleChoiceListInitializerImpl"/>
<bean id="choiceListInitializers" class="org.jahia.services.content.nodetypes.initializers.ChoiceListInitializerService" factory-method="getInstance">
<property name="initializers">
<map>
<entry key="resourceBundle" value-ref="rbInitializer"/>
</map>
</property>
</bean>
When initializing the <<<ChoiceListInitializerService>>> we pass a map of initializers we want to use in the system. Each Initializer will be associated with a keyword.
Here the <<<ResourceBundleChoiceListInitializerImpl>>> will be attached to the keyword <<<resourceBundle>>>.
When writing your definition you can chain your Initializers to achieve complex interactions. Each initializer will receive the list of values from its predecessors.
This way you can have one initializer that fills the values, and other in the pipe that changes some of those values or add properties to them.
Here an example for the Templates choicelist in the layout panel:
[jmix:renderable] >jmix:layout, jmix:contentMixin mixin - j:view (string, choicelist[templates,resourceBundle,image])
This means that for displaying this dropdown list we first call the <<<templates>>> initializer that is responsible for filling the available values.
Then this list of values is passed to the <<<resourceBundle>>> initializer, which will try to replace the labels displayed in the dropdown list by ones found in the resource bundles if available. Then all these updated values will go to the <<<image>>> initializer that will add an image to be associated with each value.
Chaining two fields using initializers
With initializers it is also possible to establish a relationship between two properties. The content of a field can be initialized depending of a selection on a previous field.
[jmix:renderableList] mixin itemtype = layout - j:subNodesView (string, choicelist[templates=subnodes,resourceBundle,image,dependentProperties='j:allowedTypes']) nofulltext
In the above example the subNodesView has a choicelist initializer defined, and a dependent property. If there is a change in the allowedTypes property, the list will be re-initialized
Adding new initializers
Jahia let you add your own initializer in a simple and flexible way using either:
- a Node initializer
- a Script initializer
The node initializer
The node initializer can populate a drop down list with the contents to a node in the JCR, by specifying the path of this node. The combobox list is then constructed from the target node's children.
This approach has a flexibility advantages:
- Easily manageable by the webmaster via the Content Explorer interface or a custom UI built for this purpose
- Benefits from Jahia’s internationalization feature
The script initializer
This is the most flexible entry point provided by Jahia to initialize a property. This script can be written in any language supported by the Java Scripting API (JSR 223). Therefore, the limit is only the developer’s imagination. This subject is beyond the scope of this Guide, you can find complete documentation on Jahia.com or in the sources of Jahia (Jahia/src/site/apt/initializers-renderers/other_initializers.apt)

