Setting Default Values in AEM Component Dialog

Published
Modified
In AEM components, it's often necessary to establish default values. This ensures that when components are dragged onto the page, they display predefined initial values. Subsequently, authors can configure the components based on specific requirements.
Utilizing cq:template is effective in this scenario. Need to verify how the values are stored in the content node upon saving the component dialog and then duplicate the identical structure in cq:template. This ensures that the values will be stored to the content node as soon as component is added to the page.
In the example below, a new item will be added to the multifield once the component is dragged on the page.
_cq_template / .content.xml
<jcr:root xmlns:jcr="http://www.jcp.org/jcr/1.0" jcr:primaryType="nt:unstructured"> <navItems jcr:primaryType="nt:unstructured"> <item0 jcr:primaryType="nt:unstructured" country="Germany" language="German"/> </navItems> </jcr:root>
If there's a need for the same cq:template across various components, the cq:templatePath properties can be utilized. As an illustration, duplicate the _cq_template node and place it in a common place like project/datasource, naming it, for instance, "navigation". Subsequently, within the component properties, add the property like below:
navigation / .content.xml
<jcr:root xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" jcr:primaryType="cq:Component" cq:templatePath="project/datasource/navigation"/>

Frequently Asked Questions (FAQ)

Difference between setting default value for each node and cq:template?

The value of each node won't be displayed or stored in the JCR node until authors open and save the dialog. Conversely, the value of cq:template will be immediately applied once the component is added to the page. Additionally, utilizing cq:template allows for adding default item or setting a default value for a multifield, which isn't supported by the value property of each node.