Expand my Community achievements bar.

AEM Cloud Service - Sort PathField Picker pages Alphabetically | AEM Community Blog Seeding

Avatar

Administrator

BlogImage.jpg

AEM Cloud Service - Sort PathField Picker pages Alphabetically by Sreekanth Choudry Nalabotu

Abstract

AEM Cloud Version 2022.4.7138.20220427T075748Z-220401
PathField picker shows pages in the order they exist in CRX. This extension is to show them in Alphabetical order...

Solution
1) Create the project...

mvn -B org.apache.maven.plugins:maven-archetype-plugin:3.2.1:generate -D archetypeGroupId=com.adobe.aem
-D archetypeArtifactId=aem-project-archetype -D archetypeVersion=36 -D aemVersion=cloud
-D appTitle="Experience AEM Path Field Sort" -D appId="eaem-pathfield-sort" -D groupId="apps.experienceaem.sites"
-D frontendModule=none -D includeExamples=n -D includeDispatcherConfig=n


2) Add filter apps.experienceaem.sites.core.filters.PathFieldSortFilter to sort the nodes for PathField picker ...

package apps.experienceaem.sites.core.filters;

import com.adobe.granite.ui.components.ds.AbstractDataSource;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.wrappers.SlingHttpServletRequestWrapper;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;

import com.adobe.granite.ui.components.ds.DataSource;

import javax.servlet.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

@Component(
service = Filter.class,
immediate = true,
name = "Experience AEM PathField Sort Filter",
property = {
Constants.SERVICE_RANKING + ":Integer=-99",
"sling.filter.scope=REQUEST",
"sling.filter.pattern=/mnt/overlay/cq/gui/content/coral/common/form/pagefield/picker.*"
}
)
public class PathFieldSortFilter implements Filter {
public static String DATA_SOURCE_NAME = DataSource.class.getName();

@Override
public void init(FilterConfig filterConfig) throws ServletException {
}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
chain.doFilter(new PathFieldSlingServletRequestWrapper((SlingHttpServletRequest) request), response);
}

@Override
public void destroy() {
}

private class PathFieldSlingServletRequestWrapper extends SlingHttpServletRequestWrapper {
public PathFieldSlingServletRequestWrapper(final SlingHttpServletRequest request) {
super(request);
}

@Override
public Object getAttribute(String attrName) {
if (!PathFieldSortFilter.DATA_SOURCE_NAME.equals(attrName)) {
return super.getAttribute(attrName);
}

DataSource ds = (DataSource) super.getAttribute(attrName);

if (ds == null) {
return ds;
}

final List sortedList = new ArrayList();
Iterator items = ds.iterator();

while (items.hasNext()) {
sortedList.add(items.next());
}

sortedList.sort(Comparator.comparing(Resource::getName));

ds = new AbstractDataSource() {
public Iterator iterator() {
return sortedList.iterator();
}
};

return ds;
}
}
}

Read Full Blog

AEM Cloud Service - Sort PathField Picker pages Alphabetically

Q&A

Please use this thread to ask the related questions.



Kautuk Sahni
0 Replies