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.

Count of Number of times component is reused in a page

Avatar

Level 3

Hi Team. I have a requirement where  i need to have the count of number of times a component is dragged and dropped in a page

Suppose i have a link component and its dialog has a field(say analytics-field)

If i drag and drop component for first time in a page, that analytics-field in link component dialog should be auto-populated with value "Link1"

If i drag and drop component for second time in a page, that analytics-field in link component dialog should be auto-populated with value "Link2"

If i drag and drop component for third time in a page, that analytics-field in link component dialog should be auto-populated with value "Link3"

.

.

.

.If i drag and drop component for nth time in a page, that analytics-field in link component dialog should be auto-populated with value "Linkn"

Any way to achieve this,

Thanks

6 Replies

Avatar

Community Advisor

Hi Preetham

     As far as I could understand , this can be achieved only via a custom logic in the back-end. Write a sling model for your component, and in the init method write the query to check the count of the component (Using query builder API, find the component count ) in current-page and  for each count using Properties API and Node set the value of the property you want to manipulate

Below predicates might help

path = <current page path>

type = "nt:unstructured"

resourceType=< relative path of your component> (never start from /apps)

p.limit=-1

Thanks

Veena

Avatar

Level 3

Thanks Veena, Will try this and ill come back if im stuck or if i have any doubts

Avatar

Community Advisor

Sure. Happy to help anytime. Let us know ..

Avatar

Level 6

This can be achived only with custom logic. There is no particular API to return number of counts.

  • use node iteration logic for jcr node of page path.
  • from there you can compare each node resource Type to your component resource path. it matches then counter+
  • by the end of iteration logic you will have number of times component is used on the page.

Avatar

Level 3

Thanks Bro, Will try and come back to you if im stuck somewhere