17. Create a dynamic front page listing#
In this part you will:
Use a listing block to show content marked as "featured"
Configure additional criterion for a listing block
Tools and techniques covered:
listing criterion
Check out mastering-plone-project at tag behaviors_1:
git checkout behaviors_1
The code at the end of the chapter:
git checkout frontpage
More info in The code for the training
In the previous chapter, we prepared a behavior for content types to store the choice of whether the content should be featured. And we enhanced the catalog search by adding index and metadata "featured". Now we turn this into a criterion for a listing block that shows featured content.
17.1. Query criteria#
To understand what a query criterion is, we have to look at the listing block of Volto.
In the sidebar, we see the Criteria select menu, and if we click there, it'll show some of the selectable criteria ordered in categories like the following:
Metadatacontains indexes that are counting as metadata like Type (meaning content type) and Review StateTextcontains indexes that are counting as text data like Description and Searchable TextDatescontains indexes which are working with date data like Effective Date and Creation Date
These criteria control how the listing is filtered to include the content items we want to show.
17.2. Add an index to the query criteria#
To get all talks we marked as featured, we have to get the listing block to recognize our newly created index.
This means we have to add our index to the collection criteria, to be selectable by the editor.
To add our new index as a criterion to be applicable in a listing block or a collection, we have to create a plone.app.registry record for our index.
This can be achieved by adding a new file backend/src/ploneconf/site/profiles/default/registry/querystring.xml:
1<?xml version="1.0" encoding="utf-8"?>
2<registry xmlns:i18n="http://xml.zope.org/namespaces/i18n"
3 i18n:domain="plone"
4>
5
6 <records interface="plone.app.querystring.interfaces.IQueryField"
7 prefix="plone.app.querystring.field.featured"
8 >
9 <value key="title"
10 i18n:translate=""
11 >Featured</value>
12 <value key="enabled">True</value>
13 <value key="sortable">False</value>
14 <value key="operations">
15 <element>plone.app.querystring.operation.boolean.isTrue</element>
16 <element>plone.app.querystring.operation.boolean.isFalse</element>
17 </value>
18 <value key="group"
19 i18n:translate=""
20 >Metadata</value>
21 </records>
22
23</registry>
To understand this code snippet, we have to know the information and tags we are using:
The prefix
plone.app.querystring.field.featuredrefers to the featured index we just created.The operations elements define which operations can be used when filtering by this index.
The group value defines the group under which the entry shows up in the selection widget, in our case
Metadata.
Tip
For a list of Plone's default querystring criteria and declarations and operations, see plone/plone.app.querystring
We can now restart the instance and re-install the add-on.
17.3. Add a listing block to show the featured content#
Now we go back to our frontend.
To create a new listing block on the front page we have to click on edit and then create one new block.
Choose the block Listing from the menu:
You can select the 'featured' criterion:
Now the listing shows only the items in the site that have the featured checkbox checked.