filter Jahia 7.3 Jahia 8

Filter condition applyOnTemplates only works if the template is in the URL

Question

How to create a custom filter condition?

Answer

Add an implementation of the ExecutionCondition to your filter class:

public MyFilterForTemplate() {
    addCondition(new TemplateCondition());
}

private class TemplateCondition implements ExecutionCondition {

    @Override
    public boolean matches(RenderContext renderContext, Resource resource) {
        boolean templateProperty = false;
        boolean templateURL = false;
        Resource main = renderContext.getMainResource();
        for (String template : templateList) {
            if (main.getNode().getPropertyAsString("j:templateName") != null)
                templateProperty = templateProperty | main.getNode().getPropertyAsString("j:templateName").equals(template);
            if (main.getResolvedTemplate() != null)
                templateURL = templateURL | main.getResolvedTemplate().equals(template);
        }
        return templateProperty | templateURL;
    }
}