Expand my Community achievements bar.

SOLVED

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

Avatar

Level 3

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 @default(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 

 

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@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


Aanchal Sikka

View solution in original post

10 Replies

Avatar

Community Advisor

@phampi 

 

Can you please elaborate "

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


Aanchal Sikka

Avatar

Level 3

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

phampi_0-1719466951207.png

Regards, 

Pavan

 

 

 

Avatar

Community Advisor

@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

Avatar

Level 3

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

Avatar

Correct answer by
Community Advisor

@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


Aanchal Sikka

Avatar

Community Advisor

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;

Avatar

Level 3

Hi @MukeshYadav_ , 

Thanks for reply. 

Yes, i did it with proper imports. 

 

Regards, 

Pavan

Avatar

Level 4

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

Avatar

Community Advisor

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/adob...



Arun Patidar

Avatar

Administrator

@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