Allow the use of any custom page date field for dateRange collection filtering.

This commit is contained in:
Andrew Koebbe
2015-08-20 23:37:29 -05:00
parent 0675ce718c
commit 8e41b51cb7
2 changed files with 10 additions and 4 deletions

View File

@@ -211,17 +211,19 @@ class Collection extends Iterator
}
/**
* Returns the items between a set of date ranges where second value is optional
* Returns the items between a set of date ranges of either the page date field (default) or
* an arbitrary datetime page field where end date is optional
* Dates can be passed in as text that strtotime() can process
* http://php.net/manual/en/function.strtotime.php
*
* @param $field
* @param $startDate
* @param bool $endDate
*
* @return $this
* @throws \Exception
*/
public function dateRange($startDate, $endDate = false)
public function dateRange($field = false, $startDate, $endDate = false)
{
$start = strtotime($startDate);
$end = $endDate ? strtotime($endDate) : strtotime("now +1000 years");
@@ -230,7 +232,10 @@ class Collection extends Iterator
foreach ($this->items as $path => $slug) {
$page = $this->pages->get($path);
if ($page->date() > $start && $page->date() < $end) {
$date = $field ? strtotime($page->value($field)) : $page->date();
if ($date > $start && $date < $end) {
$date_range[$path] = $slug;
}
}

View File

@@ -1841,9 +1841,10 @@ class Page
// TODO: END OF MOVE
if (isset($params['dateRange'])) {
$field = isset($params['dateRange']['field']) ? $params['dateRange']['field'] : false;
$start = isset($params['dateRange']['start']) ? $params['dateRange']['start'] : 0;
$end = isset($params['dateRange']['end']) ? $params['dateRange']['end'] : false;
$collection->dateRange($start, $end);
$collection->dateRange($field, $start, $end);
}
if (isset($params['order'])) {