In the example below we check if the object is a collection.
if(is_a($collection, 'Collection')) {
echo 'This is a collection';
}
Forum - Check if the object is a page or collection
There is nothing called "Global collection filter", but it's possible to get close to creating one.
page::$methods['realChildren'] = function($page) {
return $page->children()->filterBy('intendedTemplate', 'not in', ['modules', 'revision']);
};
foreach($page->realChildren() as $child) {
echo $child->title();
}
- It will not work on collections.
- You can't name it
children
because then it will still use the nativechildren
method.
Forum - Global filters run it before template loads
What we want here is an interval of pages and if it reaches the end of the collection, start over until the limit is reached.
Look at this list of pages:
project-a
project-b
project-c
project-d //The current page
project-e
Result should be a collection like this (limit set to 4):
project-e
// End of collection. Starts over to fill the limit.
project-a
project-b
project-c
Page method
page::$methods['collectionRotator'] = function($page, $limit) {
$pages = $page->siblings();
$offset = $pages->indexOf($page)+1;
$items = $pages->slice($offset, $limit);
$remainder = $pages->limit($limit-$items->count());
$collection = new Pages();
$collection->add($items);
$collection->add($remainder);
return $collection;
};
Usage
foreach($page->collectionRotator(4) as $child) {
echo $child->title();
}