Custom Annotations in Sling Model — Adobe Experience Manager
Let's talk about Annotations in Sling Models. On this article, we will create an annotation to get a multifield data and put on a List.
On Adobe Experience Manager, the developer can use the Java API or the Javascript API to integrate the component with the JCR tree.
On this article, let's create an annotation to get the Multifield authored information and put the data into a List variable.
As can be seen on the Multifield documentation, when the component is using it into the Authoring Dialog, the values are stored into child nodes. The annotation will get it and iterate over the children. On this example, we will provide into the Annotation argument, the properties from the multifield node we would like to get.
Below you can see the structure of the component multifield content on the JCR tree:
The "my-component-node" node has a sling:resourceType property as /apps/rheck/components/my-component, with the following HTML (HTL):
The component is calling a Sling Model, as can be seen above. The model MyComponent will be responsible to get the multifield data. Below the code is using an annotation called @Multifield which we will be creating afterwards.
There're two variables to the Multifield annotation. The first is the name, which corresponds to the name of the fieldset, as can be checked into the tree structure. The second one is an array of the properties we would like to get from the content node.
With all set, let's create the annotation and the injector, to make the annotation work.
Now, create a file — can be into the same package — which will be the Injector for the annotation.
Note: the method getName should return an string with the same value of the Source annotation from the annotation interface.
All done. The annotation has been created and the injector will work executing the needed job.
You can use the annotation Reference to inject services and also use the available variables to do what ever you want.
Hope you can create your own annotations and handle the Sling Models data as you would like.