Skip to content

Commit

Permalink
Improve collection filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Jan 24, 2018
1 parent d2e3ea0 commit 103ac4b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 15 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# v1.4.0-rc.2
## mm/dd/2018

1. [](#improved)
* Better `Page.collection()` filtering support including ability to have non-published pages in collections

# v1.4.0-rc.1
## 01/22/2018

Expand Down
75 changes: 60 additions & 15 deletions system/src/Grav/Common/Page/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -2471,7 +2471,15 @@ public function collection($params = 'content', $pagination = true)
return new Collection();
}

$collection = $this->evaluate($params['items']);
// See if require published filter is set and use that, if assume published=true
$only_published = true;
if (isset($params['filter']['published']) && $params['filter']['published']) {
$only_published = false;
} elseif (isset($params['filter']['non-published']) && $params['filter']['non-published']) {
$only_published = false;
}

$collection = $this->evaluate($params['items'], $only_published);
if (!$collection instanceof Collection) {
$collection = new Collection();
}
Expand Down Expand Up @@ -2510,25 +2518,60 @@ public function collection($params = 'content', $pagination = true)

// If a filter or filters are set, filter the collection...
if (isset($params['filter'])) {

// remove any inclusive sets from filer:
$sets = ['published', 'visible', 'modular', 'routable'];
foreach ($sets as $type) {
if (isset($params['filter'][$type]) && isset($params['filter']['non-'.$type])) {
if ($params['filter'][$type] && $params['filter']['non-'.$type]) {
unset ($params['filter'][$type]);
unset ($params['filter']['non-'.$type]);
}

}
}

foreach ((array)$params['filter'] as $type => $filter) {
switch ($type) {
case 'published':
if ((bool) $filter) {
$collection->published();
}
break;
case 'non-published':
if ((bool) $filter) {
$collection->nonPublished();
}
break;
case 'visible':
$collection->visible($filter);
if ((bool) $filter) {
$collection->visible();
}
break;
case 'non-visible':
$collection->nonVisible($filter);
if ((bool) $filter) {
$collection->nonVisible();
}
break;
case 'modular':
$collection->modular($filter);
if ((bool) $filter) {
$collection->modular();
}
break;
case 'non-modular':
$collection->nonModular($filter);
if ((bool) $filter) {
$collection->nonModular();
}
break;
case 'routable':
$collection->routable($filter);
if ((bool) $filter) {
$collection->routable();
}
break;
case 'non-routable':
$collection->nonRoutable($filter);
if ((bool) $filter) {
$collection->nonRoutable();
}
break;
case 'type':
$collection->ofType($filter);
Expand Down Expand Up @@ -2589,11 +2632,11 @@ public function collection($params = 'content', $pagination = true)

/**
* @param string|array $value
*
* @param bool $only_published
* @return mixed
* @internal
*/
public function evaluate($value)
public function evaluate($value, $only_published = true)
{
// Parse command.
if (is_string($value)) {
Expand Down Expand Up @@ -2662,7 +2705,7 @@ public function evaluate($value)
}
}

$results = $results->published();

break;

case 'page@':
Expand Down Expand Up @@ -2706,16 +2749,14 @@ public function evaluate($value)
$results = $page->children()->nonModular();
}

$results = $results->published();

break;

case 'root@':
case '@root':
if (!empty($parts) && $parts[0] === 'descendants') {
$results = $pages->all($pages->root())->nonModular()->published();
$results = $pages->all($pages->root())->nonModular();
} else {
$results = $pages->root()->children()->nonModular()->published();
$results = $pages->root()->children()->nonModular();
}
break;

Expand All @@ -2732,10 +2773,14 @@ public function evaluate($value)
if (!empty($parts)) {
$params = [implode('.', $parts) => $params];
}
$results = $taxonomy_map->findTaxonomy($params)->published();
$results = $taxonomy_map->findTaxonomy($params);
break;
}

if ($only_published) {
$results = $results->published();
}

return $results;
}

Expand Down

0 comments on commit 103ac4b

Please sign in to comment.