Example #1
0
 /**
  * Get a collection of pages in the current context.
  *
  * @param string|array $params
  * @param boolean $pagination
  * @return Collection
  * @throws \InvalidArgumentException
  */
 public function collection($params = 'content', $pagination = true)
 {
     if (is_string($params)) {
         $params = (array) $this->value('header.' . $params);
     } elseif (!is_array($params)) {
         throw new \InvalidArgumentException('Argument should be either header variable name or array of parameters');
     }
     if (!isset($params['items'])) {
         return array();
     }
     $collection = $this->evaluate($params['items']);
     if (!$collection instanceof Collection) {
         $collection = new Collection();
     }
     $collection->setParams($params);
     /** @var Uri $uri */
     $uri = self::getGrav()['uri'];
     /** @var Config $config */
     $config = self::getGrav()['config'];
     $process_taxonomy = isset($params['url_taxonomy_filters']) ? $params['url_taxonomy_filters'] : $config->get('system.pages.url_taxonomy_filters');
     if ($process_taxonomy) {
         foreach ((array) $config->get('site.taxonomies') as $taxonomy) {
             if ($uri->param($taxonomy)) {
                 $items = explode(',', $uri->param($taxonomy));
                 $collection->setParams(['taxonomies' => [$taxonomy => $items]]);
                 foreach ($collection as $page) {
                     // Don't filter modular pages
                     if ($page->modular()) {
                         continue;
                     }
                     foreach ($items as $item) {
                         if (empty($page->taxonomy[$taxonomy]) || !in_array(htmlspecialchars_decode($item, ENT_QUOTES), $page->taxonomy[$taxonomy])) {
                             $collection->remove();
                         }
                     }
                 }
             }
         }
     }
     if (isset($params['dateRange'])) {
         $start = isset($params['dateRange']['start']) ? $params['dateRange']['start'] : 0;
         $end = isset($params['dateRange']['end']) ? $params['dateRange']['end'] : false;
         $field = isset($params['dateRange']['field']) ? $params['dateRange']['field'] : false;
         $collection->dateRange($start, $end, $field);
     }
     if (isset($params['order'])) {
         $by = isset($params['order']['by']) ? $params['order']['by'] : 'default';
         $dir = isset($params['order']['dir']) ? $params['order']['dir'] : 'asc';
         $custom = isset($params['order']['custom']) ? $params['order']['custom'] : null;
         $collection->order($by, $dir, $custom);
     }
     /** @var Grav $grav */
     $grav = self::getGrav()['grav'];
     // New Custom event to handle things like pagination.
     $grav->fireEvent('onCollectionProcessed', new Event(['collection' => $collection]));
     // Slice and dice the collection if pagination is required
     if ($pagination) {
         $params = $collection->params();
         $limit = isset($params['limit']) ? $params['limit'] : 0;
         $start = !empty($params['pagination']) ? ($uri->currentPage() - 1) * $limit : 0;
         if ($limit && $collection->count() > $limit) {
             $collection->slice($start, $limit);
         }
     }
     return $collection;
 }