/** * This method populates the iterator property * */ private function initialize() { if ($this->initialized) { return; } if (!isset($this->datasource)) { throw new \Exception('A datalist must have a datasource before it can be iterated or counted'); } // Handle pagination if ($this->hasOption('limit_per_page')) { $this->datasource->paginate($this->getOption('limit_per_page'), $this->getOption('range_limit'))->setPage($this->page); } // Handle search if (null !== $this->getOption('search') && !empty($this->searchQuery)) { $expressionBuilder = new DatalistFilterExpressionBuilder(); $this->searchFilter->getType()->buildExpression($expressionBuilder, $this->searchFilter, $this->searchQuery, $this->searchFilter->getOptions()); $this->datasource->setSearchExpression($expressionBuilder->getExpression()); } // Handle filters $expressionBuilder = new DatalistFilterExpressionBuilder(); if (!empty($this->filterData)) { foreach ($this->filterData as $filterName => $filterValue) { $filter = $this->filters[$filterName]; $filter->getType()->buildExpression($expressionBuilder, $filter, $filterValue, $filter->getOptions()); } $this->datasource->setFilterExpression($expressionBuilder->getExpression()); } // Handle sort if (isset($this->routeParams['sort-field']) && isset($this->routeParams['sort-direction'])) { $field = $this->getField($this->routeParams['sort-field']); if (null !== $field && true === $field->getOption('sortable')) { $propertyPath = $field->getOption('sort_property_path'); if (empty($propertyPath)) { throw new \Exception('The "sort_property_path" option must be set on datalist field when option "sortable" is true.'); } $this->datasource->setSort($propertyPath, $this->routeParams['sort-direction']); } } $this->iterator = $this->datasource->getIterator(); }