Skip to content

Commit 51826da

Browse files
committed
[List] Allow current page to be excluded
Adds option to the V4 List component to exclude the current page from the results. ---- fixes #2699
1 parent d6699e5 commit 51826da

File tree

8 files changed

+107
-1
lines changed

8 files changed

+107
-1
lines changed

bundles/core/src/main/java/com/adobe/cq/wcm/core/components/internal/models/v1/ListImpl.java

+28
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import javax.jcr.RepositoryException;
3333

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

95+
/**
96+
* Property name for flag that indicates if current page should be excluded.
97+
*/
98+
private static final String PN_EXCLUDE_CURRENT_PAGE = "excludeCurrentPage";
99+
94100
/**
95101
* Search query property name.
96102
*/
@@ -175,6 +181,11 @@ public class ListImpl extends AbstractComponentImpl implements List {
175181
*/
176182
protected boolean linkItems;
177183

184+
/**
185+
* Flag indicating if the current request page should be excluded.
186+
*/
187+
protected boolean excludeCurrentPage;
188+
178189
/**
179190
* The list items.
180191
*/
@@ -191,6 +202,7 @@ protected void initModel() {
191202
PN_SHOW_MODIFICATION_DATE, currentStyle.get(PN_SHOW_MODIFICATION_DATE, SHOW_MODIFICATION_DATE_DEFAULT));
192203
linkItems = properties.get(PN_LINK_ITEMS, currentStyle.get(PN_LINK_ITEMS, LINK_ITEMS_DEFAULT));
193204
dateFormatString = properties.get(PN_DATE_FORMAT, currentStyle.get(PN_DATE_FORMAT, DATE_FORMAT_DEFAULT));
205+
excludeCurrentPage = properties.get(PN_EXCLUDE_CURRENT_PAGE, Boolean.FALSE);
194206
}
195207

196208
@Override
@@ -279,6 +291,11 @@ protected java.util.List<Page> getPages() {
279291
break;
280292
}
281293

294+
// filter out current page
295+
if (this.excludeCurrentPage) {
296+
itemStream = itemStream.filter(page -> !page.getPath().equals(this.currentPage.getPath()));
297+
}
298+
282299
// order the results
283300
OrderBy orderBy = OrderBy.fromString(properties.get(PN_ORDER_BY, StringUtils.EMPTY));
284301
if (orderBy != null) {
@@ -376,6 +393,17 @@ private Stream<Page> getSearchListItems() {
376393
search.setQuery(query);
377394
search.setSearchIn(searchRoot.get().getPath());
378395
search.addPredicate(new Predicate("type", "type").set("type", NameConstants.NT_PAGE));
396+
397+
if (this.excludeCurrentPage) {
398+
PredicateGroup pg = new PredicateGroup();
399+
pg.setNegated(true);
400+
pg.add(new Predicate("path")
401+
.set("path", this.currentPage.getPath())
402+
.set("exact", Boolean.TRUE.toString())
403+
.set("self", Boolean.TRUE.toString()));
404+
search.addPredicate(pg);
405+
}
406+
379407
int limit = properties.get(PN_SEARCH_LIMIT, SEARCH_LIMIT_DEFAULT);
380408
search.setHitsPerPage(limit);
381409
return safeGetSearchResult(search)

bundles/core/src/test/java/com/adobe/cq/wcm/core/components/internal/models/v1/ListImplTest.java

+12
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ public class ListImplTest {
6969

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

72+
protected static final String LIST_22 = TEST_PAGE_CONTENT_ROOT + "/staticListTypeExcludeCurrentPage";
73+
74+
7275
protected final AemContext context = CoreComponentTestContext.newAemContext();
7376

7477
protected String testBase;
@@ -103,6 +106,15 @@ protected void testStaticListType() {
103106
Utils.testJSONExport(list, Utils.getTestExporterJSONPath(testBase, LIST_2));
104107
}
105108

109+
@Test
110+
protected void testStaticListExcludeCurrentPage() {
111+
this.context.currentPage("/content/list/pages/page_2");
112+
List list = getListUnderTest(LIST_22);
113+
checkListConsistencyByPaths(list, new String[]{
114+
"/content/list/pages/page_1"
115+
});
116+
}
117+
106118
@Test
107119
protected void testChildrenListType() {
108120
List list = getListUnderTest(LIST_3);

bundles/core/src/test/resources/list/test-content.json

+14
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@
2525
"/content/list/pages/page_2"
2626
]
2727
},
28+
"staticListTypeExcludeCurrentPage": {
29+
"jcr:primaryType": "nt:unstructured",
30+
"sling:resourceType": "core/wcm/components/list",
31+
"listFrom": "static",
32+
"showThumbnail": "true",
33+
"linkItems": "true",
34+
"showDescription": "true",
35+
"showModificationDate": "true",
36+
"excludeCurrentPage": "true",
37+
"pages": [
38+
"/content/list/pages/page_1",
39+
"/content/list/pages/page_2"
40+
]
41+
},
2842
"staticMaxItemsListType": {
2943
"jcr:primaryType": "nt:unstructured",
3044
"sling:resourceType": "core/wcm/components/list",

bundles/core/src/test/resources/list/v2/test-content.json

+14
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@
2525
"/content/list/pages/page_2"
2626
]
2727
},
28+
"staticListTypeExcludeCurrentPage": {
29+
"jcr:primaryType": "nt:unstructured",
30+
"sling:resourceType": "core/wcm/components/list/v2/list",
31+
"listFrom": "static",
32+
"showThumbnail": "true",
33+
"linkItems": "true",
34+
"showDescription": "true",
35+
"showModificationDate": "true",
36+
"excludeCurrentPage": "true",
37+
"pages": [
38+
"/content/list/pages/page_1",
39+
"/content/list/pages/page_2"
40+
]
41+
},
2842
"staticWithVanityPaths": {
2943
"jcr:primaryType": "nt:unstructured",
3044
"sling:resourceType": "core/wcm/components/list/v2/list",

bundles/core/src/test/resources/list/v3/test-content.json

+14
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@
2525
"/content/list/pages/page_2"
2626
]
2727
},
28+
"staticListTypeExcludeCurrentPage": {
29+
"jcr:primaryType": "nt:unstructured",
30+
"sling:resourceType": "core/wcm/components/list/v3/list",
31+
"listFrom": "static",
32+
"showThumbnail": "true",
33+
"linkItems": "true",
34+
"showDescription": "true",
35+
"showModificationDate": "true",
36+
"excludeCurrentPage": "true",
37+
"pages": [
38+
"/content/list/pages/page_1",
39+
"/content/list/pages/page_2"
40+
]
41+
},
2842
"staticMaxItemsListType": {
2943
"jcr:primaryType": "nt:unstructured",
3044
"sling:resourceType": "core/wcm/components/list/v3/list",

bundles/core/src/test/resources/list/v4/test-content.json

+14
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@
2525
"/content/list/pages/page_2"
2626
]
2727
},
28+
"staticListTypeExcludeCurrentPage": {
29+
"jcr:primaryType": "nt:unstructured",
30+
"sling:resourceType": "core/wcm/components/list/v4/list",
31+
"listFrom": "static",
32+
"showThumbnail": "true",
33+
"linkItems": "true",
34+
"showDescription": "true",
35+
"showModificationDate": "true",
36+
"excludeCurrentPage": "true",
37+
"pages": [
38+
"/content/list/pages/page_1",
39+
"/content/list/pages/page_2"
40+
]
41+
},
2842
"staticMaxItemsListType": {
2943
"jcr:primaryType": "nt:unstructured",
3044
"sling:resourceType": "core/wcm/components/list/v4/list",

content/src/content/jcr_root/apps/core/wcm/components/list/v4/list/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ last modification date of the item; possible values: `title`, `modified`
6868
14. `./showDescription` - if set to `true` each item's description will be rendered
6969
15. `./showModificationDate` - if set to `true` each item's last modification date will be rendered
7070
16. `./displayItemAsTeaser` - if set to `true` the rendering of each list item is delegated to the configured teaser component
71-
17. `./id` - defines the component HTML ID attribute.
71+
17. `./excludeCurrentPage` - if set to `true` the current page is excluded from results.
72+
18. `./id` - defines the component HTML ID attribute.
7273

7374
### Deprecated Edit Dialog Properties
7475
1. `./pages` - defines the pages to be rendered, when the `./listFrom` property is set to `static` up to component version `v3`.

content/src/content/jcr_root/apps/core/wcm/components/list/v4/list/_cq_dialog/.content.xml

+9
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,15 @@
282282
</well>
283283
</items>
284284
</setTags>
285+
<excludeCurrentPage
286+
jcr:primaryType="nt:unstructured"
287+
sling:resourceType="granite/ui/components/coral/foundation/form/checkbox"
288+
checked="{Boolean}false"
289+
fieldDescription="When checked, the current page is excluded from results."
290+
name="./excludeCurrentPage"
291+
text="Exclude current page"
292+
uncheckedValue="{Boolean}false"
293+
value="{Boolean}true"/>
285294
<orderBy
286295
jcr:primaryType="nt:unstructured"
287296
sling:resourceType="granite/ui/components/coral/foundation/form/select"

0 commit comments

Comments
 (0)