/** * Return the ID of the current filter. * * @return int|null */ public function currentFilterID() { if ($filterTag = $this->constraint(static::FilterVar)) { if ($filter = GridListFilter::get()->filter(ModelTag::SingleFieldName, $filterTag)->first()) { return $filter->ID; } } }
/** * Make sure only the custom filters are provided. This needs to go after other filter providers. * * @param $filters */ public function constrainGridListFilters(&$filters, &$parameters = []) { $page = \Director::get_current_page(); if ($page instanceof \CMSMain) { $page = $page->currentPage(); } if ($customFilters = $page->config()->get('gridlist_custom_filters') ?: []) { $filters = new \ArrayList(); foreach ($customFilters as $tag => $title) { if (!($filter = GridListFilter::get()->filter(['ModelTag' => $tag])->first())) { $filter = new GridListFilter([Title::SingleFieldName => $title, GridListFilter::TagFieldName => $tag]); } $filters->push($filter); } } }
/** * @param \DataList|\ArrayList $items * @param $filters * @param array $parameters */ public function sequenceGridListItems(&$items, $filters, &$parameters = []) { $limit = isset($parameters['PageLength']) ? $parameters['PageLength'] : null; // filter items for each filter to current page length if ($limit) { $start = GridList::service()->Filters()->start() ?: 0; $out = new \ArrayList(); $currentFilter = GridList::service()->constraint(Constraints::FilterVar); if ($currentFilter && $currentFilter != 'all') { if ($filter = GridListFilter::get()->filter(['ModelTag' => $currentFilter])->first()) { $out->merge($items->limit($limit, $start)); } } else { foreach ($filters as $filter) { $filtered = new \ArrayList(); foreach ($items as $item) { if ($item instanceof Block) { // only push blocks first page if ($start == 0) { $filtered->push($item); } } else { if ($currentFilter == 'all') { $filtered->push($item); } else { if ($item->GridListFilters()->find('ID', $filter->ID)) { $filtered->push($item); } } } } // merge limited filtered items back in $out->merge($filtered->limit($limit, $start)); } } $items = $out; } }
public function provideGridListFilters() { return GridListFilter::get()->sort('Sort'); }