Sling Model Exporters - Override node name | Community
Skip to main content
Level 4
June 19, 2018
Solved

Sling Model Exporters - Override node name

  • June 19, 2018
  • 3 replies
  • 2769 views

Hi All,

We are trying to determine if there is a way to override the node name while exposing the model JSON. The properties for JSON can be controlled by exporter but not the node name itself. For example following is page structure

page

     jcr:content

          parsys

               component (node has its properties)

               component_xxx (node has its properties)

               component_yyy (node has its properties)

Component has its exporter implemented. Now when we try to get data as  page.model.json we get the node names in response as component, component_xxx, component_yyy.

Is there way to change this in Sling Model Exporter JSON output, as by default AEM generates random node names for same component dragged in page, we wanted to expose a meaningful name as part of JSON output (We are trying to customize content fragment JSON output). Let me know if there is way or which direction which should look into.

Thanks,
Sandeep

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

When testing localhost:4502/content/we-retail/us/en/user.model.json, I get a JSON model. But I don't think that this page is backed by an explicit Sling model, but rather by a default implementation.

You can always implement a custom model and then choose the names yourself.

Jörg

3 replies

joerghoh
Adobe Employee
joerghohAdobe EmployeeAccepted solution
Adobe Employee
June 19, 2018

When testing localhost:4502/content/we-retail/us/en/user.model.json, I get a JSON model. But I don't think that this page is backed by an explicit Sling model, but rather by a default implementation.

You can always implement a custom model and then choose the names yourself.

Jörg

Level 4
June 20, 2018

Thanks Jörg

It seems the AEM Core Components has Page Model implementation. This has a method

private <T> Map<String, T> getChildModels(@Nonnull SlingHttpServletRequest slingRequest,

                                              @Nonnull Class<T> modelClass) {

        Map<String, T> itemWrappers = new LinkedHashMap<>();

        for (final Resource child : slingModelFilter.filterChildResources(request.getResource().getChildren())) {

            itemWrappers.put(child.getName(), modelFactory.getModelFromWrappedRequest(slingRequest, child, modelClass));

        }

        return  itemWrappers;

}

We can tweak this to change child.getName(), One observation I had is this depends on JCR structure, but if our nodes are more nested control goes to modelFactory.getModelFromWrappedRequest(slingRequest, child, modelClass)call, this is part of AEM out of the box bundles. And we need to look into to tweak this.

Regards,
Sandeep

joerghoh
Adobe Employee
Adobe Employee
June 21, 2018

the modelFactory interface belongs to sling, and the only implementation I have on my 6.4 instance is org.apache.sling.models.impl.ModelAdapterFactory (sling-org-apache-sling-models-impl/ModelAdapterFactory.java at master · apache/sling-org-apache-sling-models-impl · GitH… ). Maybe you can dig deeper there.

Jörg