how to add default value to RTE | Community
Skip to main content
January 11, 2025
Solved

how to add default value to RTE

  • January 11, 2025
  • 3 replies
  • 1733 views

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>

 

 

 

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 sarav_prakash

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

Populate `text` and `textIsRich` property. 

 

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

 

3 replies

sarav_prakash
Community Advisor
sarav_prakashCommunity AdvisorAccepted solution
Community Advisor
January 11, 2025

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

Populate `text` and `textIsRich` property. 

 

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

 

BineniPrAuthor
January 13, 2025

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

 

sarav_prakash
Community Advisor
Community Advisor
January 14, 2025

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


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

kapil_rajoria
Community Advisor
Community Advisor
January 12, 2025

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.$);
BineniPrAuthor
January 13, 2025

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

kapil_rajoria
Community Advisor
Community Advisor
January 14, 2025

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:


dialog:

.content.xml:

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); } }
AmitVishwakarma
Community Advisor
Community Advisor
January 19, 2025
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.