Why is Sling ignoring the "adaptable" parameter of HTL's data-sly-use?

275 Views Asked by At

In 2017 functionality was added to Apache Sling and HTL to allow use of the Use API with a different adaptable (such as a resource resolver, or a child resource) rather than the default backing resource or request.

Feike Visser provided an example of how to use such flexible adapters with a simple list:

<div data-sly-list.child="${resource.listChildren}">
   <sly data-sly-use.c="${'com.adobe.examples.htl.core.models.HelloWorldModel' @ adaptable=child}" />
   ${c.resourceType}
</div>

However, the example does not appear to work (in this case, using non-AEM Sling 11). While the HelloWorldModel (a Sling Model) is instantiated, the backing resource is always the original page, not the adaptable specified. It's as it the adaptable=child part is ignored.

What might be preventing this useful feature from working?

EDIT: The HelloWorldModel is based on Visser's example:

@Model(adaptables=Resource.class)
public class HelloWorldModel {

    private static Logger log = LoggerFactory.getLogger(HelloWorldModel.class);

    @OSGiService
    private SlingSettingsService settings;

    @SlingObject
    private ResourceResolver resourceResolver;

    @ValueMapValue(name = "sling:resourceType", injectionStrategy=InjectionStrategy.OPTIONAL) @Default(values="No resourceType")
    private String resourceType;

    private String message;


    @PostConstruct
    public void init() {
        log.info("Reached init of HelloWorldModel");
        message = "\tHello World!\n";
        message += "\tResource type is: " + resourceType + "\n";
        message += "\tUser id is " + resourceResolver.getUserID() + "\n";
    }

    public String getResourceType() {
           return resourceType;
    }

    public String getMessage() {
        log.info("Inside getMessage() method");
        return message;
    }  
}

The output is always the resource type of the page resource, never the resource type of the list children.

EDIT: Could this be because the SlingModelsUseProvider is used before the JavaUseProvider, meaning that the JavaUseProvider - which provides the flexible adaptation - is never reached?

0

There are 0 best solutions below