Development
How do I sort on a Calendar field?
The format of a Calendar field value (distinct from a Date/Time field) is MM-dd-yyyy
which can make it tricky to sort on without some additional work to convert it to a numerical timestamp.
Here are some examples of using a lookup table to sort pages on a Calendar field:
With the XPathTool
#set ($pages = $_XPathTool.selectNodes($contentRoot, "/system-index-block/system-page[system-data-structure[calendar-field != '/']]"))
#if ($pages.size() > 0)
#set ($lookupTable = [])
#foreach ($p in $pages)
#set ($date = $_DateTool.toDate("MM-dd-yyyy",$p.getChild("system-data-structure").getChild("calendar-field").value))
#set ($dateTimestamp = $date.getTime())
#set ($_void = $lookupTable.add({
"timestamp": $_MathTool.mul($dateTimestamp, 1),
"page": $p
}))
#end
#foreach($page in $_SortTool.sort($lookupTable, ["timestamp:desc"]))
#set($timeStamp = $page.get("timestamp"))
#set($item = $page.get("page"))
#set($itemDate = $_EscapeTool.xml($item.getChild("system-data-structure").getChild("calendar-field").value))
#set($dateFormat = $_DateTool.getDateFormat("MM-dd-yyyy", $_DateTool.getLocale(), $_DateTool.getTimeZone()))
#set($itemDate = $dateFormat.parse($itemDate))
#set($itemTitle = $_EscapeTool.xml($item.getChild("title").value))
$_DateTool.format( 'MM/dd/yyyy', $itemDate ) - ${timeStamp} - $itemTitle
#end
#end
With our API
#set ($pages = $_.query().byContentType("path/to/Content Type").execute())
#set ($removed = $_ListTool.removeNull($pages, "structuredDataNode(calendar-field).textValue"))
#if ($pages.size() > 0)
#set ($lookupTable = [])
#foreach ($p in $pages)
#set ($date = $_DateTool.toDate("MM-dd-yyyy",$p.getStructuredDataNode("calendar-field").textValue))
#set ($dateTimestamp = $date.getTime())
#set ($_void = $lookupTable.add({
"timestamp": $_MathTool.mul($dateTimestamp, 1),
"page": $p
}))
#end
#foreach($page in $_SortTool.sort($lookupTable, ["timestamp:desc"]))
#set($timeStamp = $page.get("timestamp"))
#set($item = $page.get("page"))
$item.getStructuredDataNode("calendar-field").textValue - ${timeStamp} - $item.metadata.title
#end
#end