Skip to content

Commit

Permalink
[List] Allow current page to be excluded
Browse files Browse the repository at this point in the history
Adds option to the V4 List component to exclude the current
page from the results.

----
fixes #2699
  • Loading branch information
ky940819 committed Oct 8, 2024
1 parent d2a7b02 commit d0a654b
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import javax.jcr.RepositoryException;

import com.adobe.cq.wcm.core.components.util.AbstractComponentImpl;
import com.day.cq.search.PredicateGroup;
import com.day.cq.search.result.SearchResult;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.ObjectUtils;
Expand Down Expand Up @@ -91,6 +92,11 @@ public class ListImpl extends AbstractComponentImpl implements List {
*/
private static final String PN_CHILD_DEPTH = "childDepth";

/**
* Property name for flag that indicates if current page should be excluded.
*/
private static final String PN_EXCLUDE_CURRENT_PAGE = "excludeCurrentPage";

/**
* Search query property name.
*/
Expand Down Expand Up @@ -175,6 +181,11 @@ public class ListImpl extends AbstractComponentImpl implements List {
*/
protected boolean linkItems;

/**
* Flag indicating if the current request page should be excluded.
*/
protected boolean excludeCurrentPage;

/**
* The list items.
*/
Expand All @@ -191,6 +202,7 @@ protected void initModel() {
PN_SHOW_MODIFICATION_DATE, currentStyle.get(PN_SHOW_MODIFICATION_DATE, SHOW_MODIFICATION_DATE_DEFAULT));
linkItems = properties.get(PN_LINK_ITEMS, currentStyle.get(PN_LINK_ITEMS, LINK_ITEMS_DEFAULT));
dateFormatString = properties.get(PN_DATE_FORMAT, currentStyle.get(PN_DATE_FORMAT, DATE_FORMAT_DEFAULT));
excludeCurrentPage = properties.get(PN_EXCLUDE_CURRENT_PAGE, Boolean.FALSE);
}

@Override
Expand Down Expand Up @@ -279,6 +291,11 @@ protected java.util.List<Page> getPages() {
break;
}

// filter out current page
if (this.excludeCurrentPage) {
itemStream = itemStream.filter(page -> !page.getPath().equals(this.currentPage.getPath()));
}

// order the results
OrderBy orderBy = OrderBy.fromString(properties.get(PN_ORDER_BY, StringUtils.EMPTY));
if (orderBy != null) {
Expand Down Expand Up @@ -376,6 +393,17 @@ private Stream<Page> getSearchListItems() {
search.setQuery(query);
search.setSearchIn(searchRoot.get().getPath());
search.addPredicate(new Predicate("type", "type").set("type", NameConstants.NT_PAGE));

if (this.excludeCurrentPage) {
PredicateGroup pg = new PredicateGroup();
pg.setNegated(true);
pg.add(new Predicate("path")
.set("path", this.currentPage.getPath())
.set("exact", Boolean.TRUE.toString())
.set("self", Boolean.TRUE.toString()));
search.addPredicate(pg);
}

int limit = properties.get(PN_SEARCH_LIMIT, SEARCH_LIMIT_DEFAULT);
search.setHitsPerPage(limit);
return safeGetSearchResult(search)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ public class ListImplTest {

protected static final String LIST_21 = TEST_PAGE_CONTENT_ROOT + "/staticOrderByTitleListTypeWithBlankTitle";

protected static final String LIST_22 = TEST_PAGE_CONTENT_ROOT + "/staticListTypeExcludeCurrentPage";


protected final AemContext context = CoreComponentTestContext.newAemContext();

protected String testBase;
Expand Down Expand Up @@ -103,6 +106,15 @@ protected void testStaticListType() {
Utils.testJSONExport(list, Utils.getTestExporterJSONPath(testBase, LIST_2));
}

@Test
protected void testStaticListExcludeCurrentPage() {
this.context.currentPage("/content/list/pages/page_2");
List list = getListUnderTest(LIST_22);
checkListConsistencyByPaths(list, new String[]{
"/content/list/pages/page_1"
});
}

@Test
protected void testChildrenListType() {
List list = getListUnderTest(LIST_3);
Expand Down
14 changes: 14 additions & 0 deletions bundles/core/src/test/resources/list/test-content.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@
"/content/list/pages/page_2"
]
},
"staticListTypeExcludeCurrentPage": {
"jcr:primaryType": "nt:unstructured",
"sling:resourceType": "core/wcm/components/list",
"listFrom": "static",
"showThumbnail": "true",
"linkItems": "true",
"showDescription": "true",
"showModificationDate": "true",
"excludeCurrentPage": "true",
"pages": [
"/content/list/pages/page_1",
"/content/list/pages/page_2"
]
},
"staticMaxItemsListType": {
"jcr:primaryType": "nt:unstructured",
"sling:resourceType": "core/wcm/components/list",
Expand Down
14 changes: 14 additions & 0 deletions bundles/core/src/test/resources/list/v2/test-content.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@
"/content/list/pages/page_2"
]
},
"staticListTypeExcludeCurrentPage": {
"jcr:primaryType": "nt:unstructured",
"sling:resourceType": "core/wcm/components/list/v2/list",
"listFrom": "static",
"showThumbnail": "true",
"linkItems": "true",
"showDescription": "true",
"showModificationDate": "true",
"excludeCurrentPage": "true",
"pages": [
"/content/list/pages/page_1",
"/content/list/pages/page_2"
]
},
"staticWithVanityPaths": {
"jcr:primaryType": "nt:unstructured",
"sling:resourceType": "core/wcm/components/list/v2/list",
Expand Down
14 changes: 14 additions & 0 deletions bundles/core/src/test/resources/list/v3/test-content.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@
"/content/list/pages/page_2"
]
},
"staticListTypeExcludeCurrentPage": {
"jcr:primaryType": "nt:unstructured",
"sling:resourceType": "core/wcm/components/list/v3/list",
"listFrom": "static",
"showThumbnail": "true",
"linkItems": "true",
"showDescription": "true",
"showModificationDate": "true",
"excludeCurrentPage": "true",
"pages": [
"/content/list/pages/page_1",
"/content/list/pages/page_2"
]
},
"staticMaxItemsListType": {
"jcr:primaryType": "nt:unstructured",
"sling:resourceType": "core/wcm/components/list/v3/list",
Expand Down
14 changes: 14 additions & 0 deletions bundles/core/src/test/resources/list/v4/test-content.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@
"/content/list/pages/page_2"
]
},
"staticListTypeExcludeCurrentPage": {
"jcr:primaryType": "nt:unstructured",
"sling:resourceType": "core/wcm/components/list/v4/list",
"listFrom": "static",
"showThumbnail": "true",
"linkItems": "true",
"showDescription": "true",
"showModificationDate": "true",
"excludeCurrentPage": "true",
"pages": [
"/content/list/pages/page_1",
"/content/list/pages/page_2"
]
},
"staticMaxItemsListType": {
"jcr:primaryType": "nt:unstructured",
"sling:resourceType": "core/wcm/components/list/v4/list",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ last modification date of the item; possible values: `title`, `modified`
14. `./showDescription` - if set to `true` each item's description will be rendered
15. `./showModificationDate` - if set to `true` each item's last modification date will be rendered
16. `./displayItemAsTeaser` - if set to `true` the rendering of each list item is delegated to the configured teaser component
17. `./id` - defines the component HTML ID attribute.
17. `./excludeCurrentPage` - if set to `true` the current page is excluded from results.
18. `./id` - defines the component HTML ID attribute.

### Deprecated Edit Dialog Properties
1. `./pages` - defines the pages to be rendered, when the `./listFrom` property is set to `static` up to component version `v3`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,15 @@
</well>
</items>
</setTags>
<excludeCurrentPage
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/checkbox"
checked="{Boolean}false"
fieldDescription="When checked, the current page is excluded from results."
name="./excludeCurrentPage"
text="Exclude current page"
uncheckedValue="{Boolean}false"
value="{Boolean}true"/>
<orderBy
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/select"
Expand Down

0 comments on commit d0a654b

Please sign in to comment.