Default value in Reference component

varuns46785756

18-07-2019

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,

Accepted Solutions (1)

Accepted Solutions (1)

Arun_Patidar

MVP

24-07-2019

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");

  }

Answers (9)

Answers (9)

varuns46785756

23-07-2019

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.

Arun_Patidar

MVP

23-07-2019

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>

varuns46785756

23-07-2019

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,

Arun_Patidar

MVP

22-07-2019

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>

varuns46785756

22-07-2019

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,

Arun_Patidar

MVP

19-07-2019

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

jayQ

19-07-2019

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.