public function sort(&$sort, &$order, $params) { $section = $params['current-section']; // If `?unsort` is appended to the URL, then sorting information are reverted // to their defaults if ($params['unsort']) { $section->setSortingField($section->getDefaultSortingField(), false); $section->setSortingOrder('asc'); redirect(Administration::instance()->getCurrentPageURL()); } // By default, sorting information are retrieved from // the filesystem and stored inside the `Configuration` object if (is_null($sort) && is_null($order)) { $sort = $section->getSortingField(); $order = $section->getSortingOrder(); // Sorting by ID requires saving sort data to the `EntryManager` // object for subsequent use if ($sort == 'id') { EntryManager::setFetchSortingField('id'); EntryManager::setFetchSortingDirection($order); } } else { // Ensure that this field is infact sortable, otherwise // fallback to IDs if (($field = FieldManager::fetch($sort)) instanceof Field && !$field->isSortable()) { $sort = $section->getDefaultSortingField(); } // If the sort order or direction differs from what is saved, // update the config file and reload the page if ($sort != $section->getSortingField() || $order != $section->getSortingOrder()) { $section->setSortingField($sort, false); $section->setSortingOrder($order); redirect(Administration::instance()->getCurrentPageURL() . $params['filters']); } } }