Skip to content

Latest commit

 

History

History
93 lines (67 loc) · 2.14 KB

collection.md

File metadata and controls

93 lines (67 loc) · 2.14 KB

Collection

Is collection

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

Global collection filter

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']);
};

Template / snippet

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 native children method.

Forum - Global filters run it before template loads

Collection rotator

Get next collection of pages

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();
}

Forum - Filter interval of pages

Sources