Sling Model Exporters - Override node name | Community
Skip to main content
Level 4
Jun 19, 2018 2:01 AM UTC
Solved

Sling Model Exporters - Override node name

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.
joerghoh
joerghohAccepted solution
Employee
Jun 19, 2018 7:11 PM UTC

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
Employee
Jun 21, 2018 10:14 AM UTC

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

Translate
Level 4
Jun 20, 2018 7:18 PM UTC

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

Translate
joerghoh
joerghohAccepted solution
Employee
Jun 19, 2018 7:11 PM UTC

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

Translate