Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Can I pre-populate the values or default in my dialog based on the template.

Avatar

Level 2

Hello All,

 

I have a scenario where I need to change the default values in a dialog based on a template type.

 

Example: 

If I drop my component onto Template 1, the default values of 2 fields in the dialog be 30,40 and when I drag and drop the same component on to a different template then the values in the dialog needs to be changed to 30,40

 

Template 1:

adithyaa4585051_2-1581627031869.png

Template 2:

adithyaa4585051_4-1581627088141.png

Thanks,

Adithya.

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor
3 Replies

Avatar

Level 3

@adithyaa4585051, It would be better to do the checks from backend and provide overrides in the dialogs for the authors. 

 

But if would like to maintain it only at dialog level, we might have to manipulate few things in the dialog listeners.

Create a dialog listener, get the current page json and retrieve the resourceType or template name and you can add the required values in the fields based on the template/resourcetype comparison.

 

Sample Markup:

$(document).on("dialog-ready", function() {
    //Hard coded the path
    var url = "http://localhost:4502/content/aem-flash/en/jcr:content.json";
    var templateName = "";
    $.getJSON(url, function(json) {
        templateName = json['cq:template'];
        console.log('templateName is {}', templateName);
        // Check the template / resource type and update the values in dialog
    });
});

 

But, if you do this way, this might override the second time if the author updates the value earlier, so you may have to set a condition to update them only when the values are not present(Ideally the first time we dragged and dropped the component)

 

I would suggest, to do the checks from the backend and provide overrides for author instead of the above approach.

Avatar

Employee Advisor

You can create a granite:rendercondition where you can check for the template type and set the default value. 

 

Search rendercondition in crx/de search to see some OOTB samples. eg. /libs/cq/gui/components/renderconditions

Avatar

Correct answer by
Community Advisor