Expand my Community achievements bar.

SOLVED

Sling Model Exporters - Override node name

Avatar

Level 4

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

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

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

View solution in original post

3 Replies

Avatar

Correct answer by
Employee Advisor

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

Avatar

Level 4

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

Avatar

Employee Advisor

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-slin... ). Maybe you can dig deeper there.

Jörg