is it possible to get default values in the json without touching the dialog | Community
Skip to main content
Level 2
June 27, 2024
Solved

is it possible to get default values in the json without touching the dialog

  • June 27, 2024
  • 5 replies
  • 2153 views

Hi Team, 

Hope you all are doing good. 

I need a help on one requirement. 

We have one component and it's dialog contains two tabs, (tab1, tab2 respectively).

We are using this component on experience fragement to get headless cms. 

 

With the current implementation, there is sling model exporter defined already for this component

json output is in below format, 

"tab1":{

"id" :

}

"tab2":{

"firstname":

"lastname":

}

For achieving tab2 in above json format i have written another sling model and used it as @ChildResource in sling model exporter defined.

I tried both ways by using @1497330(values=" ") and also tried with value="" in content.xml of dialog. 

Both options need to tick on the dialog then default data is visible in the json format. 

Could you please give any idea/ solution to achieve the requuirement without touching the tick of the dialog. 

Thanks in advance. 

Regards, 

Pavan

#HeadlessCMS, #Experiencefragment 

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by aanchal-sikka

Hi @aanchal-sikka , 

for getting tab2 values as json format shared above. 

I made sling model, used the same as @ChildResource in existing model exporter class. 

Observation for state of component before saving is it just creating a tab1 node. 

After saving the dialog, it will create tab2 node inside tab1 node and default values appear as a properties of tab2 node. 

Regards, 

Pavan


@phampi 

 

Please see if you can use cq:template to create a default node. 

The ChildResource annotation is understanding that the resource does not exist. Hence, the Sling Model might also not be coming into action

 

http://www.sgaemsolutions.com/2018/02/cqtemplate-and-cqtemplatepath.html

5 replies

aanchal-sikka
Community Advisor
Community Advisor
June 27, 2024

@phampi 

 

Can you please elaborate "

Both options need to tick on the dialog then default data is visible in the json format. "

Aanchal Sikka
phampiAuthor
Level 2
June 27, 2024

Hi @aanchal-sikka , 

Thanks for reply. 

Let me elaborate more. 

I tried below 2 options.

Option 1 : Using value=" " in content.xml of the dialog

Option 2 : Using @Default(values=" ") in the sling model created for getting tab2 values. 

for both options, we need to tick (highlighted below) of the dialog, then default values appears in json format

Regards, 

Pavan

 

 

 

aanchal-sikka
Community Advisor
Community Advisor
June 27, 2024

@phampi 

 

Can you please check whats the difference in the state of the component before and after you save the Dialog? I mean difference in values of component properties when viewed via CRXDE

Aanchal Sikka
MukeshYadav_
Community Advisor
Community Advisor
June 27, 2024

Hi @phampi ,

If understood correctly you want to return exporter json default value even if component dialog is not authored(dialog not submitted).

 

just make sure you imported correctly and no typo in fields.

import org.apache.sling.models.annotations.Default;

 

@ValueMapValue
@Default(values = StringUtils.EMPTY)
private String heading;
phampiAuthor
Level 2
June 27, 2024

Hi @mukeshyadav_ , 

Thanks for reply. 

Yes, i did it with proper imports. 

 

Regards, 

Pavan

rk_pandian
Level 4
June 27, 2024

Hi @phampi,

 

You can try the below ways:

  1. Add a _cq_template.xml for you component and mock the dialog structure including the child node and add default values and check if it works.
    • This works for normal components, but I haven't tried when there is a @Childresource involved, so you have to try.
    • Also this will get reflected only for the newly authored components, for existing components this file will not be used.
  2. Add an init() method with @PostConstruct annotation in your SlingModel and handle the logic there.

 

Regards,

Ramkumar

arunpatidar
Community Advisor
Community Advisor
June 27, 2024

Hi @phampi 
If need to add default value in the sling exporter then you @Default annotation

@Default(booleanValues = true) 

 

Ref: https://github.com/Adobe-Consulting-Services/acs-aem-samples/blob/master/core/src/main/java/com/adobe/acs/samples/models/SampleSlingModelExporter.java

Arun Patidar
kautuk_sahni
Community Manager
Community Manager
July 8, 2024

@phampi Did you find the suggestion helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you! 

Kautuk Sahni