Showing posts with label Custom Parsys. Show all posts
Showing posts with label Custom Parsys. Show all posts

Monday, January 11, 2016

Customizing the default parsys text of AEM using Sightly code

I got a requirement to change the default placeholder text "drag component here" of parsys component to show customized text for each different component that includes it through the sightly script. 


One way of changing the default text all over the place is editing/overriding the script in /libs/cq/gui/components/authoring/clientlibs/editor/js/model/Inspectable.js. It would change the text everywhere in the parsys.  But, I wanted to customize this text for each component and it can be achieved with the following steps

Step 1: Override the parsys from the out of the box AEM to your apps folder ie wcm/foundation/components/parsys. 

The new sightly component is placed in the /libs/wcm/foundation/components/parsys.

Step 2: Add the property "sling:resourceSuperType" and the value "wcm/foundation/components/parsys" at the component level or the .content.xml. The code will look like
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root 
    xmlns:sling="http://sling.apache.org/jcr/sling/1.0"
    xmlns:jcr="http://www.jcp.org/jcr/1.0"
    jcr:primaryType="cq:Component"
    jcr:title="Paragraph System - Sightly"
    jcr:description="Include components in a paragraph system."
    allowedChildren="[*]"
    componentGroup=".hidden"
    sling:resourceSuperType="wcm/foundation/components/parsys"/>

Step 3: Select the newpar component folder inside this component and add the property sling:resourceType with the value "wcm/foundation/components/parsys/newpar". This code will look like
<?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"
    jcr:primaryType="cq:Component"
    jcr:title="New Paragraph - Sightly"
    sling:resourceType="wcm/foundation/components/parsys/newpar"
componentGroup=".hidden"/>

Step 4: The default text can be set empty by making the following change. Open the parsys.html files change this line 

<div data-sly-test="${!paragraph.columns && paragraph.resourceType && paragraph.cssClasses}" class="${paragraph.cssClasses}" data-sly-resource="${paragraph.resourcePath @ resourceType=paragraph.resourceType, decorationTagName=''}"></div> to 

<div data-sly-test="${!paragraph.columns && paragraph.resourceType && paragraph.cssClasses}" class="cq-placeholder section" data-emptytext=""  data-sly-resource="${paragraph.resourcePath @ resourceType=paragraph.resourceType, decorationTagName=''}"></div>

That's all required in the overridden/extended parsys component. Now, we need to add some code in our new components that includes the parsys.

Step 5: Create a new component and use the following code for adding the placeholder text is  <div class="cq-placeholder section" data-emptytext="Your Custom Text"></div> 

The sighlty code for including any component would be like <sly data-sly-resource="${'myComponents' @ resourceType='/apps/yourProject/parsys' }"></sly> 

Note: the resourceType would point to the folder where our parsys component is created.