Sibling node name id and identifying them in path browser | Community
Skip to main content
Level 3
October 11, 2018

Sibling node name id and identifying them in path browser

  • October 11, 2018
  • 3 replies
  • 9558 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

3 replies

arunpatidar
Community Advisor
Community Advisor
October 11, 2018

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
RonakBAuthor
Level 3
October 11, 2018

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.

arunpatidar
Community Advisor
Community Advisor
October 12, 2018

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
raj_mandalapu
Level 7
October 16, 2018

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

RonakBAuthor
Level 3
October 17, 2018

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.

arunpatidar
Community Advisor
Community Advisor
October 17, 2018

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
raj_mandalapu
Level 7
October 17, 2018

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?