on 2018 Dec 13 5:17 PM
Request clarification before answering.
So I solved this in a following way:
1) I've created a custom CMS Restriction which extends AbstractRestriction or you can go with CMSUserGroupRestriction, if you want also to apply it for some specific UserGroup
2)I am applying this restriction on the CMSLinkComponent (it can be done also from the smart edit in the Navigation Management)
In my hierarchy each category has a separate CMSLinkComponent
3) Added RestrictionEvaluator for my custom restriction , In the restriction evaluator I am restricting CMSLinkComponent's of the categories which have no products inside
(this can be also combined with SearchRestriction on products, if you want to exclude some products and therefore the categories)
4) So after the changes above the response structure will be the following:
{
"uid" : "SomeRootCategoryNavNode",
"uuid" : "uuid",
"entries" : [ {
"itemId" : "SomeRootCategoryLink",
"itemSuperType" : "AbstractCMSComponent",
"itemType" : "CMSLinkComponent"
} ],
"children" : [ {
"uid" : "uid1",
"uuid" : "uuid1",
"entries" : [ {
"itemId" : "id1",
"itemSuperType" : "AbstractCMSComponent",
"itemType" : "CMSLinkComponent"
} ],
"children" : [ ],
"title" : "Child Category 1 Nav Node"
}, {
"uid" : "uid2",
"uuid" : "uuid2",
"entries" : [ ],
"children" : [ ],
"title" : "Child Category 2 Nav Node"
} ]
} ]
}So what we did with our custom restriction is that we excluded CMSLinkComponent for ChildCategory2NavNode, thats why it has an empty list of entries [ ]
But the node will be still visible just without the link.
In order to hide the node:
6) There is a OOTB CMSNavigationNodeRenderingVisibilityRule , we gonna extend it and override (using spring alias)
also we gonna need to override isVisible(final CMSNavigationNodeModel itemModel) method
@Override
public boolean isVisible(final CMSNavigationNodeModel itemModel)
{
return itemModel.isVisible() && (CollectionUtils.isNotEmpty(itemModel.getChildren()) ||
(CollectionUtils.isNotEmpty(itemModel.getEntries()) && checkAllEntriesVisible(itemModel.getEntries())));
}
protected boolean checkAllEntriesVisible(final List<CMSNavigationEntryModel> entries)
{
return entries.stream().allMatch(entry -> getRenderingVisibilityService().isVisible(entry.getItem()));
}I implemented it like this, but you can refactor it as you wish, the idea is that we are hiding all the nodes , which doesnt have a children or entries(or all the entries that they are not visible) , or simply are not visible
After this being done the Child Category 2 Nav Node will be excluded from our response
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Attach category to CMSLinkComponent for this CMSNavigationNode and create cronjob which periodically set visibility to false to node, it category has no products.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.