Expand my Community achievements bar.

SOLVED

How to mandate use of a "required component" on each page

Avatar

Level 4

Please share "How-To's", and ideas on the best ways to implement the following functionality

- A component that is able to walk-through the pages of any AEM site that it's pointed to, (maybe walking a list of sites), and verify that one or more "required" components exist on every page on each site.

- Another component that is able to check for the existence of a given analytics tag on every AEM page, and then update the page or template, and add the tag if it's not already on the page.

 

I'd want the second component to be mandated for use on every page, and I believe there is a way to have it added to every template, and then lock it down with an ACL, so that it can't be removed. I also believe there is a way to have "required component" included, as "boilerplate", so that they are on every template by default, but due to my limited experience, I'm not sure if this is the best way to achieve this, (i.e. best practice). Thanks for your suggestions, code examples and/or links to tutorials in advance.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hello,

These are three interesting questions,

Googling on the topic I have found following blog post: http://www.aemmastery.com/2014/07/29/restricting-aem-component-number-instances-page/ seems to answer your second question StackOverflow on the same topic: http://stackoverflow.com/questions/18713857/how-to-restrict-a-component-to-add-only-once-per-page.

Tried to answer your questions too:

Let's address the first one:

Sounds like it can be divided into smaller problems:

  1. How to traverse(walk through the pages) site?
  2. How to verify that page has component X?

1) You can traverse from any path e.g get all child pages using Page API [1] and doing recursion with getChildren method call or use QueryBuilder API [2] to get pages.

This really depends on what you need, recursion would be quicker in terms of execution speed, but QueryBuilder would be easier to implement.

2) Each AEM Author page can be loaded[3][4] with following parameter ?debug=layout [5[ this would load up page with special annotations denoting each component path. Now, the task is to match with regex output of the page with the expected component X resource path.

The same logic can be applied to the tag based solution, only you would need to parse the html respose for tags and then use POST request to populate data.

Now, with your third questions it is possible to hard code the component in the template itself. E.g. reference the component directly in your template code.

[1] https://docs.adobe.com/docs/en/cq/5-6-1/javadoc/com/day/cq/wcm/api/Page.html

[2] https://docs.adobe.com/docs/en/cq/5-6-1/dam/customizing_and_extendingcq5dam/query_builder.html

[3] https://sling.apache.org/documentation/the-sling-engine/dispatching-requests.html

[4] https://sling.apache.org/apidocs/sling5/org/apache/sling/api/scripting/SlingScriptHelper.html

[5] http://blogs.adobe.com/experiencedelivers/experience-management/cq_developer_tricks/

 

Hope this help you in a good way .

Thanks,

Peter

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

Hello,

These are three interesting questions,

Googling on the topic I have found following blog post: http://www.aemmastery.com/2014/07/29/restricting-aem-component-number-instances-page/ seems to answer your second question StackOverflow on the same topic: http://stackoverflow.com/questions/18713857/how-to-restrict-a-component-to-add-only-once-per-page.

Tried to answer your questions too:

Let's address the first one:

Sounds like it can be divided into smaller problems:

  1. How to traverse(walk through the pages) site?
  2. How to verify that page has component X?

1) You can traverse from any path e.g get all child pages using Page API [1] and doing recursion with getChildren method call or use QueryBuilder API [2] to get pages.

This really depends on what you need, recursion would be quicker in terms of execution speed, but QueryBuilder would be easier to implement.

2) Each AEM Author page can be loaded[3][4] with following parameter ?debug=layout [5[ this would load up page with special annotations denoting each component path. Now, the task is to match with regex output of the page with the expected component X resource path.

The same logic can be applied to the tag based solution, only you would need to parse the html respose for tags and then use POST request to populate data.

Now, with your third questions it is possible to hard code the component in the template itself. E.g. reference the component directly in your template code.

[1] https://docs.adobe.com/docs/en/cq/5-6-1/javadoc/com/day/cq/wcm/api/Page.html

[2] https://docs.adobe.com/docs/en/cq/5-6-1/dam/customizing_and_extendingcq5dam/query_builder.html

[3] https://sling.apache.org/documentation/the-sling-engine/dispatching-requests.html

[4] https://sling.apache.org/apidocs/sling5/org/apache/sling/api/scripting/SlingScriptHelper.html

[5] http://blogs.adobe.com/experiencedelivers/experience-management/cq_developer_tricks/

 

Hope this help you in a good way .

Thanks,

Peter

Avatar

Level 4

Peter, thank you SO much. This is an extremely complete response, and even though I'm a beginner, I should be able to follow this. Again, thanks for the great answer and I hope I can quickly get to the point of helping others in the same manner .