Important Notice

This blog has been moved! Visit the new location at https://medium.com/@arunpatidar26/list/aem-dbc5a3e4df7c.

Restrict Allowed Component Group in Template Policy

 In Dynamic/Editable Template, The Component level policy allows to configure various properties. One of the properties for responsivegrid or container or any other container component is to allowe child components inside parsys/responsivegrid, which template author can define.

To Allow Components , Template author has to create policy and select components from the Allowed Components list.



But Template Author can see all the components group to select and sometimes you just want to enable few components to be allowed, for example a card container can only have card components. But you cannot restrict template Author to choose from.


How to Restrict Groups in Allowed Components list

Unfortunately there is no OOTB configuration/solution to achieve this. If you want to restrict allowed components then you have to write a custom solution. I am going to explain one of the custom solution here. 
The Allowed Component list is generated from /libs/cq/gui/components/authoring/allowedcomponents , so I will create custom allowedcomponents (/apps/commons/components/authoring/allowedcomponents) based on /libs/cq/gui/components/authoring/allowedcomponents

I am going to create custom allowed component which will be used wherever I have to restrict allowedcomponents.

Steps :

1. Copy allowedcomponent node with all the subnode from /libs/cq/gui/components/authoring/allowedcomponents to apps folder e.g. /apps/commons/components/authoring/allowedcomponents



2. Modify /apps/commons/components/authoring/allowedcomponents/allowedcomponents.html to  update JAVA reference , the package name should be same as the location of  allowedcomponents folder which was copied in Step 1 i.e. apps.commons.components.authoring.allowedcomponents.AllowedComponents


3. Modify /apps/commons/components/authoring/allowedcomponents/AllowedComponents.java to add logic to filter group list. 
I am using logic to read allowed groups from the node property where this custom component is used via resourceType . e.g.




JAVA Logic - AllowedComponents.java
3.1 update package 
package apps.commons.components.authoring.allowedcomponents;

3.2 Read allowedGroups property inside activate method

    @Override
    public void activate() throws Exception {
        // get all available components
        Map<String, Component> components = getAllComponents();
        // get selected groups and components from properties
        Set<String> selected = getSelectedComponents();
        // keeps track of encountered resources types
        Set<String> resourceTypes = new HashSet<String>();
        // stores groups indexed by their name
        Map<String, GroupInfo> groupsMap = new HashMap<String, GroupInfo>();

        // Get Allowed Groups
        ValueMap vmAG = getResource().getValueMap();
        String resGroup[] = vmAG.get("allowedGroups", new String[] {});


        // iterate over all components and build component and group info
        for (Component component : components.values()) {

3.3 Update condition to skip groups similar to groups start with .

// filter components that are part of a hidden group and non allowed group
            if (group.startsWith(".") || (resGroup.length > 0 && notAllowedGroup(resGroup, group))) {
                continue;
            }

3.4 Create notAllowedGroup method at the bottom(you can create anywhere I just created at the bottom of the file)

     /**
     * Returns true if component groups is not allowed
     */

    public boolean notAllowedGroup(String[] resGroup, String group) {
        for (int i = 0; i < resGroup.length; i++) {
if (group.matches(resGroup[i])) {
return false;
}
}
return true;
    }

4. Refresh the Template. And we are done.








No comments:

Post a Comment