mirror of
https://github.com/processwire/processwire.git
synced 2025-08-09 00:06:55 +02:00
Update PageFinder and Fieldtype interface so that Fieldtype modules can optionally take over sorting of their own fields/subfields in PageFinder searches.
This commit is contained in:
@@ -726,6 +726,28 @@ abstract class Fieldtype extends WireData implements Module {
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get or update query to sort by given $field or $subfield
|
||||
*
|
||||
* Return false if this Fieldtype does not have built-in sort logic and PageFinder should handle it.
|
||||
* Return string of query to add to ORDER BY statement, or boolean true if method added it already.
|
||||
*
|
||||
* #pw-internal
|
||||
*
|
||||
* @param Field $field
|
||||
* @param DatabaseQuerySelect $query
|
||||
* @param string $table
|
||||
* @param string $subfield
|
||||
* @param bool $desc True for descending, false for ascending
|
||||
* @return bool|string
|
||||
* @since 3.0.167
|
||||
*
|
||||
*/
|
||||
public function getMatchQuerySort(Field $field, $query, $table, $subfield, $desc) {
|
||||
if($query && $table && $field && $subfield && $desc) {}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new field table in the database.
|
||||
*
|
||||
|
@@ -2105,10 +2105,13 @@ class PageFinder extends Wire {
|
||||
|
||||
$fc = substr($value, 0, 1);
|
||||
$lc = substr($value, -1);
|
||||
$descending = $fc == '-' || $lc == '-';
|
||||
$value = trim($value, "-+");
|
||||
$subValue = '';
|
||||
// $terValue = ''; // not currently used, here for future use
|
||||
|
||||
if($this->lastOptions['reverseSort']) $descending = !$descending;
|
||||
|
||||
if(strpos($value, ".")) {
|
||||
list($value, $subValue) = explode(".", $value, 2); // i.e. some_field.title
|
||||
if(strpos($subValue, ".")) {
|
||||
@@ -2187,7 +2190,14 @@ class PageFinder extends Wire {
|
||||
|
||||
$query->leftjoin("$table AS $tableAlias ON $tableAlias.pages_id=pages.$idColumn");
|
||||
|
||||
if($subValue === 'count') {
|
||||
$customValue = $field->type->getMatchQuerySort($field, $query, $tableAlias, $subValue, $descending);
|
||||
|
||||
if(!empty($customValue)) {
|
||||
// Fieldtype handled it: boolean true (handled by Fieldtype) or string to add to orderby
|
||||
if(is_string($customValue)) $query->orderby($customValue, true);
|
||||
$value = false;
|
||||
|
||||
} else if($subValue === 'count') {
|
||||
if($this->isRepeaterFieldtype($field->type)) {
|
||||
// repeaters have a native count column that can be used for sorting
|
||||
$value = "$tableAlias.count";
|
||||
@@ -2238,8 +2248,7 @@ class PageFinder extends Wire {
|
||||
}
|
||||
}
|
||||
|
||||
$descending = $fc == '-' || $lc == '-';
|
||||
if($this->lastOptions['reverseSort']) $descending = !$descending;
|
||||
if(is_string($value) && strlen($value)) {
|
||||
if($descending) {
|
||||
$query->orderby("$value DESC", true);
|
||||
} else {
|
||||
@@ -2247,6 +2256,7 @@ class PageFinder extends Wire {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function getQueryStartLimit(DatabaseQuerySelect $query) {
|
||||
|
||||
|
Reference in New Issue
Block a user