Expand my Community achievements bar.

SOLVED

how to add default value to RTE

Avatar

Level 1

Hi Team

 

I have created a component and dialog contains one RTE and I want to set default value for it. I have tried emptyText, defaultValue, value but nothing is working. Below is my xml. Please check and advise.

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0"
xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
xmlns:granite="http://www.adobe.com/jcr/granite/1.0"
jcr:primaryType="nt:unstructured"
jcr:title="Order Search and List Dialog"
sling:resourceType="cq/gui/components/authoring/dialog">
<content
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/container">
<items jcr:primaryType="nt:unstructured">
<apiErrorMsg jcr:primaryType="nt:unstructured"
sling:resourceType="cq/gui/components/authoring/dialog/richtext"
fieldLabel="Error message for technical errors at backend/API"
fieldDescription="Error message for technical errors at backend/API"
value="There seems to be some error, pls try after sometime or contact our Customer Care."
emptyText="here seems to be some error, pls try after sometime or contact our Customer Care."
name="./apiErrorMsg"
useFixedInlineToolbar="{Boolean}true">
<rtePlugins jcr:primaryType="nt:unstructured">
<format jcr:primaryType="nt:unstructured" features="bold,italic"/>
<justify jcr:primaryType="nt:unstructured" features="-"/>
<links jcr:primaryType="nt:unstructured" features="modifylink,unlink"/>
<lists jcr:primaryType="nt:unstructured" features="*"/>
<misctools jcr:primaryType="nt:unstructured">
<specialCharsConfig jcr:primaryType="nt:unstructured">
<chars jcr:primaryType="nt:unstructured">
<default_copyright
jcr:primaryType="nt:unstructured"
entity="&amp;copy;"
name="copyright"/>
<default_euro
jcr:primaryType="nt:unstructured"
entity="&amp;euro;"
name="euro"/>
<default_registered
jcr:primaryType="nt:unstructured"
entity="&amp;reg;"
name="registered"/>
<default_trademark
jcr:primaryType="nt:unstructured"
entity="&amp;trade;"
name="trademark"/>
</chars>
</specialCharsConfig>
</misctools>
<paraformat jcr:primaryType="nt:unstructured"
features="*">
<formats jcr:primaryType="nt:unstructured">
<default_p
jcr:primaryType="nt:unstructured"
description="Paragraph"
tag="p"/>
<default_h1
jcr:primaryType="nt:unstructured"
description="Heading 1"
tag="h1"/>
<default_h2
jcr:primaryType="nt:unstructured"
description="Heading 2"
tag="h2"/>
<default_h3
jcr:primaryType="nt:unstructured"
description="Heading 3"
tag="h3"/>
<default_h4
jcr:primaryType="nt:unstructured"
description="Heading 4"
tag="h4"/>
<default_h5
jcr:primaryType="nt:unstructured"
description="Heading 5"
tag="h5"/>
<default_h6
jcr:primaryType="nt:unstructured"
description="Heading 6"
tag="h6"/>
<default_blockquote
jcr:primaryType="nt:unstructured"
description="Quote"
tag="blockquote"/>
<default_pre
jcr:primaryType="nt:unstructured"
description="Preformatted"
tag="pre"/>
</formats>
</paraformat>
<table
jcr:primaryType="nt:unstructured"
features="-">
<hiddenHeaderConfig
jcr:primaryType="nt:unstructured"
hiddenHeaderClassName="cq-wcm-foundation-aria-visuallyhidden"
hiddenHeaderEditingCSS="cq-RichText-hiddenHeader--editing"/>
</table>
<tracklinks
jcr:primaryType="nt:unstructured"
features="*"/>
</rtePlugins>
<uiSettings jcr:primaryType="nt:unstructured">
<cui jcr:primaryType="nt:unstructured">
<inline
jcr:primaryType="nt:unstructured"
toolbar="[format#bold,format#italic,format#underline,#justify,#lists,links#modifylink,links#unlink,#paraformat]">
<popovers jcr:primaryType="nt:unstructured">
<justify
jcr:primaryType="nt:unstructured"
items="[justify#justifyleft,justify#justifycenter,justify#justifyright]"
ref="justify"/>
<lists
jcr:primaryType="nt:unstructured"
items="[lists#unordered,lists#ordered,lists#outdent,lists#indent]"
ref="lists"/>
<paraformat
jcr:primaryType="nt:unstructured"
items="paraformat:getFormats:paraformat-pulldown"
ref="paraformat"/>
</popovers>
</inline>
<dialogFullScreen
jcr:primaryType="nt:unstructured"
toolbar="[format#bold,format#italic,format#underline,justify#justifyleft,justify#justifycenter,justify#justifyright,lists#unordered,lists#ordered,lists#outdent,lists#indent,links#modifylink,links#unlink,table#createoredit,#paraformat,image#imageProps]">
<popovers jcr:primaryType="nt:unstructured">
<paraformat
jcr:primaryType="nt:unstructured"
items="paraformat:getFormats:paraformat-pulldown"
ref="paraformat"/>
</popovers>
</dialogFullScreen>
<tableEditOptions
jcr:primaryType="nt:unstructured"
toolbar="[table#insertcolumn-before,table#insertcolumn-after,table#removecolumn,-,table#insertrow-before,table#insertrow-after,table#removerow,-,table#mergecells-right,table#mergecells-down,table#mergecells,table#splitcell-horizontal,table#splitcell-vertical,-,table#selectrow,table#selectcolumn,-,table#ensureparagraph,-,table#modifytableandcell,table#removetable,-,undo#undo,undo#redo,-,table#exitTableEditing,-]"/>
</cui>
</uiSettings>
</apiErrorMsg>
</items>
</content>
</jcr:root>

 

 

 

1 Accepted Solution

Avatar

Correct answer by
Level 8

Create a cq:template node in your component, like this 

sarav_prakash_1-1736630247054.png

Populate `text` and `textIsRich` property. 

 

Upon adding this component on page, automatically populates the text property value on page 

sarav_prakash_2-1736630372131.png

 

View solution in original post

9 Replies

Avatar

Correct answer by
Level 8

Create a cq:template node in your component, like this 

sarav_prakash_1-1736630247054.png

Populate `text` and `textIsRich` property. 

 

Upon adding this component on page, automatically populates the text property value on page 

sarav_prakash_2-1736630372131.png

 

Avatar

Level 1

Hi @sarav_prakash 
Tried this but not working. can you please check my xml of component and suggest me where to add cq:template

 

Avatar

Level 8

Hi @BineniPr , your xml is only cq:dialog node. The cq:template must be placed outside the cq:dialog. Like this 

sarav_prakash_0-1736778755881.png

 

Avatar

Level 1

I have two RTE in my dialog how to handle this. any example for this

Avatar

Level 8

Use different property names for each RTE. Like `firstRTEText`, `secondRTEText` and populate respective property values in cq:template. 

Avatar

Level 7

Hi @BineniPr, you can achieve the same via 2 ways:
1. Using java: use the sling model to update the value of the existing property i.e rte when the component is dragged and dropped on page.

@SlingObject
private ResourceResolver resourceResolver;

@Inject
private Resource resource;

@PostConstruct
public void init() throws PersistenceException {
try {
            ModifiableValueMap valueMap = resource.adaptTo(ModifiableValueMap.class);
            if (valueMap != null && valueMap.containsKey("your_rte")) {
                valueMap.put("your_rte", "new_value"); // update the property
                resourceResolver.commit(); 
            } else {
                LOG.error("Could not adapt resource to ModifiableValueMap: {}", resource.getPath());
            }
        } catch (Exception e) {
            LOG.error("Error setting the property", e);
        }
}


2. Using JS on dialog field

;(function (window, $) {
    'use strict';

    var RteValueUpdater = function () {

        function init() {
            var $rteField = $('.coral-RichText-editable');

            if ($rteField.length > 0) {
                var newValue = '<p>new_value</p>';
                updateRteValue($rteField, newValue);
            }
        }

        function updateRteValue($rteField, value) {
            $rteField.html(value);
            $rteField.trigger('change');
        }

        return {
            init: init
        };
    }();

    RteValueUpdater.init();

})(window, Granite.$);

Avatar

Level 1

Hi @kapil_rajoria 
I have tried java approach but it is not working for me. Could you please check once.

Avatar

Level 7

Hi @BineniPr, please remove the condition:

&& valueMap.containsKey("your_rte")

I see, when the component is dragged and dropped, initially my RTE was empty and no property was there.

I have tested the above code and its working fine:
crx de:

kapil_rajoria_0-1736829349635.png
dialog:

kapil_rajoria_1-1736829385773.png

.content.xml:

kapil_rajoria_2-1736829435195.png

java:

@PostConstruct
    public void init() throws PersistenceException {
        try {
            ModifiableValueMap valueMap = resource.adaptTo(ModifiableValueMap.class);
            if (valueMap != null) {
                valueMap.put("text", "default value set"); // update the property
                resourceResolver.commit();
            } else {
                LOG.error("Could not adapt resource to ModifiableValueMap: {}", resource.getPath());
            }
        } catch (Exception e) {
            LOG.error("Error setting the property", e);
        }
    }

Avatar

Level 7
To set a default value for the RTE in AEM:

Use the value property to set the default content directly, like:

value="Default error message content"

Ensure the name path (./apiErrorMsg) is correct for the field.

Test the dialog without the emptyText property to avoid conflicts.

Verify that no JavaScript or other logic is overwriting the value.

This should ensure the default value is applied correctly in the Rich Text Editor.