mirror of
https://github.com/getgrav/grav.git
synced 2026-05-07 06:35:30 +02:00
Allow the use of any custom page date field for dateRange collection filtering.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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'])) {
|
||||
|
||||
Reference in New Issue
Block a user