Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Default value in Reference component

Avatar

Level 5

Hi All,

I am using the reference component in AEM6.4. I have added the reference component in my header.html file. Whenever first time I am going to open the header.html page it will open the page with this component and I need to author the reference path in it, can I add default path in this component, so whenever author will open the page first time, then component will populate with default values in the path section. I have tried with "value" and defaultValue" property on node level but nothing is working. Please suggest.

<div class="contain">

<sly data-sly-resource="${'prelogin' @ resourceType='/apps/project/components/content/reference'}"></sly>

</div>

Regards,

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi,

You can write a servlet which query site to looks for pages which doesn't have prelogin and add prelogin node with default property  using java API

SQL2 Query could be like below:

SELECT * FROM [cq:Page] AS s where isdescendantnode([/content/mfHTL63]) and s.[jcr:content/sling:resourceType] like '%mfHTL63/components/structure/page%' and s.[jcr:content/prelogin/sling:resourceType] not like '%mfHTL63/components/content/reference%'

Servlet Example

aem63app-repo/SimpleSQL2SearchServlet.java at master · arunpatidar02/aem63app-repo · GitHub

Node API example

final NodeIterator result = query.execute().getNodes();

  while(result.hasNext()){

       Node node=result.nextNode().getChild("jcr:content").addNode("prelogin", "nt:unstructured");

       node.setProperty("reference", "/content/mfHTL63/en/jcr:content/header");

  }



Arun Patidar

View solution in original post

10 Replies

Avatar

Level 5

I think you need to provide more details. But I also think you can hard-code the default value inside "reference" custom component.

Avatar

Level 2

Hi,

Agree with Karl515-ZrIyBv​ on providing more details.

Supposing that reference component has a backend side, for example a sling model, you could decide if, the path property of the component is empty, use a default path instead.

Avatar

Community Advisor

Hi,

value should work only if the component is not edited. You might have edited component before and trying with value property. It is not gonna work.

you can try with cq:template property as well, which add the default property as soon as prelogin node created in content.

AEM Developer Learning : cq:template and cq:templatePath Properties of a Component in AEM

But try with new page or delete the prelogin node from content first then try with value and cq:template



Arun Patidar

Avatar

Level 5

Hi All,

Please check my requirement:

I am using the reference component directly in my header.html, so that whenever author will create a page by using my pre-login-text template( this is static template not editable), it will auto populate with the default value, so author will not going to add any reference path in the edit mode of the page.

header.html:( my static template pointing to header.html file)

<div class="contain">

<sly data-sly-resource="${'prelogin' @ resourceType='/apps/project/components/global/reference'}"></sly>

</div>

and below is my reference component:

1796723_pastedImage_3.png

but whenever I am creating new page , it always showing empty path field. I have tried with "value" property also.

1796724_pastedImage_4.png

because I am using static template here then I have tried to add value on template level and its working fine, now can I do like this(adding value on template level) , is it correct in AEM6.4. Please suggest.

1796728_pastedImage_7.png

1796729_pastedImage_8.png

Regards,

Avatar

Community Advisor

Hi,

yes, you can do that but for the existing page, you can see 500 error.

you can put a check though to avoid that.

and you can add prelogin in page component's htl code like

<sly data-sly-resource="prelogin"></sly>



Arun Patidar

Avatar

Level 5

HI Arun,

yes, you are correct its working only for new pages for existing pages its not able to find out the node, getting 500 error.

How can I add check here, do I need to add at page component level?

Regards,

Avatar

Community Advisor

you need to check if prelogin resource exists on not using java or server-side JS Use API.

Example : JS use API

Create a prelogin.js file wherever your HTL code file(header.html) is there and update below code in files

prelogin.js

use(function () {

    var res = resource.getChild("prelogin");

    var exists=true;

    if(res==null){

       exists=false;

    }

    return {

         exists:exists

    };

});

header.html  - HTL snippet

<sly data-sly-use.prelogin="prelogin.js" data-sly-test="${prelogin.exists==true}" data-sly-resource="prelogin"></sly>



Arun Patidar

Avatar

Level 5

Hi Arun,

thanks again and check is working fine but how can I add "prelogin" reference node/component in existing pages, is their any way to inject "prelogin" in existing pages without authoring.

Avatar

Correct answer by
Community Advisor

Hi,

You can write a servlet which query site to looks for pages which doesn't have prelogin and add prelogin node with default property  using java API

SQL2 Query could be like below:

SELECT * FROM [cq:Page] AS s where isdescendantnode([/content/mfHTL63]) and s.[jcr:content/sling:resourceType] like '%mfHTL63/components/structure/page%' and s.[jcr:content/prelogin/sling:resourceType] not like '%mfHTL63/components/content/reference%'

Servlet Example

aem63app-repo/SimpleSQL2SearchServlet.java at master · arunpatidar02/aem63app-repo · GitHub

Node API example

final NodeIterator result = query.execute().getNodes();

  while(result.hasNext()){

       Node node=result.nextNode().getChild("jcr:content").addNode("prelogin", "nt:unstructured");

       node.setProperty("reference", "/content/mfHTL63/en/jcr:content/header");

  }



Arun Patidar