Show pages of specific resourceType in pathfield selection dialog
Hello everyone,
I have recently run into an issue where I want to filter the pages that appear inside of a pathfield's selection dialog, to pages that have a specific resourceType (e.g. "products"). I have tried using a custom predicate to achieve this, but it does not seem to work (predicate gets registered as an OSGi service, but never fires).
Could anybody shine some light on this? Is a predicate even the correct way of approaching this? I will attach the code bellow nonetheless. We are using AEM as a Cloud Service.
Thanks in advance!
Predicate:
// Imports omitted
@Component(
service = Predicate.class,
property = { "predicate.name=allowed-article-predicate" }
)
public class AllowedArticlePredicate extends AbstractNodePredicate {
private static final Logger log = LoggerFactory.getLogger(AllowedArticlePredicate.class);
private static final String[] ALLOWED_RESOURCE_TYPES = {
CommonConstants.NEWS_RESOURCE_TYPE,
CommonConstants.EVENT_RESOURCE_TYPE,
CommonConstants.PRODUCT_RESOURCE_TYPE
};
@9944223
public final boolean evaluate(final Node node) {
if (Objects.nonNull(node)) {
try {
if (node.isNodeType("cq:Page")) {
Node contentNode = node.getNode("jcr:content");
if (contentNode != null && contentNode.hasProperty("sling:resourceType")) {
String resourceType = contentNode.getProperty("sling:resourceType").getString();
for (String allowedType : ALLOWED_RESOURCE_TYPES) {
if (allowedType.equals(resourceType)) {
return true;
}
}
}
}
} catch (RepositoryException re) {
log.error("Unable to read node or property: {}", re.getMessage());
}
}
return false;
}
}
CQ Dialog:
<article
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/pathfield"
fieldLabel="Article Selection"
name="./article"
required="true"
showTitleInTree="true"
predicate="allowed-article-predicate"
multiple="false"
forceSelection="{Boolean}true"
/>