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.

Sibling node name id and identifying them in path browser

Avatar

Level 3

Hi All,

When same component is used more than one time under same parsys, it is created using unique id like mycomponent_1526332 and so on.

Now when content author tries to reference any of those 4-5 instances created in another page using path browser, they can not differentiate between them as it shows like mycompnent_1234433, mycompnent_3243555 etc.

Question is :

1. Is it possible to control how this ID is created when node is created and we can give more meaningful name like mycomponent_01, mycomponent_02 ?

2. Is it possible to display some meaningful name in the path browser other than node name (which is only possible to know using crx/de access) ?

I am using AEM 6.3 SP2 CF1

16 Replies

Avatar

Community Advisor

Hi,

You can do this using JCR APIs, as soon as you drop your component, a node is created in repository, you can listen CREATE event and rename the node name.

You can either use sling or JCR events

Event (Content Repository for Java Technology API Version 2.0)

Creating an AEM JCR Event Listener using a Maven Archetype 12 Project 



Arun Patidar

Avatar

Level 3

Thanks Arun for your reply. I had thought of Listener. But just don't want to add listener which will trigger it for all the requests of Node Creation throughout my multi tenant instance (I know I can request based on Path / resource type etc - but that will still come to the listener at least ones) .

I was looking for rather elegant way where it triggers only for the component I want it by adding some properties / adding :name parameter for Post Servlet / adding some mixins if possible. 

I heard about Post Servlet :name parameter but not sure how to use it when the component is dragged for the first time in parsys. This will be safer option as it will trigger use the :name param where passed and do the usual node naming else where.

Hope I am making the point clear.

Avatar

Community Advisor

Hi,

Yes, it is there for that but AEM uses nameHint parameter to create name while we drag and drop components.

We can easily overwrite this nameHint parameter using sling Filter but I'll check other option to overwrite nameHint this option and get back to you.

Algorithm for Node Name Creation

If request is posted with an URL ending in slash / or slash-star /*, the SlingPostServlet derives a name for the node to be created upon the request applying the following algorithm:

  1. If a :name parameter is supplied, the (first) value of this parameter is used unmodified as the name for the new node. If the name is illegally formed with respect to JCR name requirements, an exception will be thrown when trying to create the node. The assumption with the :name parameter is, that the caller knows what he (or she) is supplying and should get the exact result if possible.
  2. Otherwise if a :nameHint parameter is supplied, the (first) value of this parameter is used to generate the node name. A name filtering is applied to this hint to ensure a valid JCR node name.
  3. Otherwise a series of request parameters supplied to set content is inspected for a possible name. The list of the names of these parameter is configurable with the SlingPostServlet and defaults to title, jcr:title, name, description, jcr:description, abstract. The first request parameter with a non-empty value is used and filtered to get the valid JCR name.
  4. Otherwise an ever increasing auto generated number is used. Filtering is also applied to this numeric name.


Arun Patidar

Avatar

Level 3

Thanks for your time Arun. Yes, I was referring to this link only. It would be great if you can elaborate the options to overwrite nameHint.

Avatar

Community Advisor

Hi Ronak,

You are about to start tingling with how AEM OOTB granite UI is about to tell AEM to store data. That's probably, not the best idea in the history of AEM.

What could be better is to improve pathfinder/extend pathfinder, and bring in some rendering options(UI options) for your content authors. This functionality was partially there for classic UI, but got lost, not implemented in Touch UI.

Instead of modifying how back end stores data, make a nice presentation for your pathfinder to let content author know what they are about to choose.


Regards,Peter

Avatar

Level 10

Whats the purpose of "when content author tries to reference any of those 4-5 instances created in another page using path browser,"

This does not sound like best practice at all. There should be no need to an author to reference an image component -- for example - located in another page.

Avatar

Community Advisor

I agree with Scott. If I need to reuse/reference the same content, I would probably go with either content fragment or experience fragments.



Arun Patidar

Avatar

Level 3

The idea is to create a page under something like /content/mysite/global/ and author all the required instances of that component under that. Then in the product specific pages, simply use reference to this.

We thought to go with content fragment but faced below challenges.

1. This is not a single field reference. It is a mini component with textfield, RTE and pathbrowser in turn. So we though to use CFM but faced limitations.

2. Also, we are on SP2 CF1 right now where CFM is disabled for some reason because of the CFP we have added.

Hence, we thought to create a custom component and reference it.

Avatar

Level 10

Its still very bad practice to read a component under  /content/mysite/somepage and try and re-use that component in another page. Each component has its own props. For example - if you want an image on PAGE A and then an image component on PAGE B  and PAGE C - reading the component on PAGE A under /content/MySite/PageA and putting that component onto content/MySite/PageB and content/MySite/PageC  - not best practice. You should use an experience fragment if you want to put duplicate components on other pages.

Avatar

Level 3

Thanks Peter. So we are talking about overriding path browser component rather than looking at how the nodes are created. when you say nice presentation, what is your idea ? Are you saying to try to display component title / some other field instead of node name ?

Avatar

Level 8

If you are authoring all common components on this page /content/mysite/global/  and referring authored components on other pages then I feel we can use EventListener I know you guys have already discussed on this but based on the below assumptions I feel it is a good approach for this requirement because

As per my understanding, we are going to author components only on the global page and I feel for every website there would be only one global page. if this is the case then we can configure to execute listener for the only global page. I don't see much performance impact here and also we need to execute listener only when the node is created and for other operations, we do not need to execute. I think you need to consider below things also when we choose listener how frequently you are going to delete and add components. as these are common components so I feel there would be less number of delete and add operations. if you think the operations are heavy also I prefer Listener as I am going to execute this only on the author not on publish. I don't know why are we avoiding Listeners here. correct me If I am wrong

Avatar

Level 10

Raja - nice answer. For ppl reading this - Adobe still recommended using components in a page as described in this online tutorial - Getting Started with AEM Sites - WKND Tutorial . Use editable templates and then in each editable template - define a Policy that lets authors have access to a set of components. Then at the page level - authors drag and drop components onto a page and set the component properties via a Touch UI component dialog - that reflects best practice.

Furthermore - for code reuse - you can use experience fragments too within pages based on editable templates.

Avatar

Level 3

Agree but as discussed earlier wanted a more fine tuned way of doing it other than Listener / Post Processor. Seems like there is no direct way to achieve this. Still waiting for any discussion around @ nameHint param.

Avatar

Community Advisor

Hi,

nameHint parameter you can get in filter servlet but modifying or adding name parameter can be risky. I would suggest you should go with JCR Events

Sling Filter to get nameParam

@Override

  public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain filterChain)

  throws IOException, ServletException {

  final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;

  final SlingHttpServletResponse slingResponse = (SlingHttpServletResponse) response;

  // logger.info("filter start : {} ", slingRequest.getParameter("./sling:resourceType"));

   if(slingRequest.getParameter("./sling:resourceType")!=null) {

   if(slingRequest.getParameter("./sling:resourceType").toString().equals("AEM63App/components/content/title")) {

   logger.info("nameHint param : {} ", slingRequest.getParameter(":nameHint"));

  }

  }

  filterChain.doFilter(request, response);

  // logger.info("filter end");

  }

Modify/Overwrite existing param

How To: Safely add or modify Servlet request parameter values | Dynamic | JSF 2 | | OCPsoft



Arun Patidar

Avatar

Level 8

I always prefer KISS policy instead of making things complex, I do not like overwriting nameHint param for a simple requirement if you find any solution on nameHitn param, request you please share with us. fine-tuned way means in terms of what  ? are you expecting on performance ?  or maintenance? or something else?