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

  Written by The Jahia Team
 
Developers
  filter
   Estimated reading time:
7.1.1.0
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;
    }
}