clarkwinkelmann I have modified this position and changed the default sorting of the homepage. I would like to change the default sorting of the search results to the latest publication. How do I proceed? I installed a plugin from the official community, but it got an error. My falrum version is 1.8.1. I consulted AI, and their response was to modify the AbstractSearcher. php file
Specifically, as follows:
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Search;
use Flarum\Query\ApplyQueryParametersTrait;
use Flarum\Query\QueryCriteria;
use Flarum\Query\QueryResults;
use Flarum\User\User;
use Illuminate\Database\Eloquent\Builder;
abstract class AbstractSearcher
{
use ApplyQueryParametersTrait;
/**
* @var GambitManager
*/
protected $gambits;
/**
* @var array
*/
protected $searchMutators;
public function __construct(GambitManager $gambits, array $searchMutators)
{
parent::__construct($gambits, $searchMutators);
}
abstract protected function getQuery(User $actor): Builder;
/**
* @param QueryCriteria $criteria
* @param int|null $limit
* @param int $offset
*
* @return QueryResults
*/
public function search(QueryCriteria $criteria, $limit = null, $offset = 0): QueryResults
{
$actor = $criteria->actor;
//This is the code snippet you need to add: make sure to modify the sorting in $writer before applying Sort---
//Get the current sorting in $writer
$sortFromCriteria = $criteria->getSort();
//If the sorting is not explicitly specified in $writer, or if Flarum's default 'relevance' is specified
if (empty($sortFromCriteria) || (isset($sortFromCriteria['relevance']) && $sortFromCriteria['relevance'] === 'desc')) {
//Force the sorting of the $writer object to be set to 'creatdAt' descending order
$criteria->setSort(['createdAt' => 'desc']);
}
//--- End adding paragraph---
$query = $this->getQuery($actor);
$search = new SearchState($query->getQuery(), $actor);
$this->gambits->apply($search, $criteria->query['q']);
//ApplySort uses the object of SearchState ($search) and the original sorting and default tags of $writer
//Because we are modifying $writer ia ->sort, $this ->applySort() will use the modified value here
$this->applySort($search, $criteria->sort, $criteria->sortIsDefault);
$this->applyOffset($search, $offset);
$this->applyLimit($search, $limit + 1);
foreach ($this->searchMutators as $mutator) {
$mutator($search, $criteria);
}
// Execute the search query and retrieve the results. We get one more
// results than the user asked for, so that we can say if there are more
// results. If there are, we will get rid of that extra result.
$results = $query->get();
if ($areMoreResults = $limit > 0 && $results->count() > $limit) {
$results->pop();
}
return new QueryResults($results, $areMoreResults);
}
}
But after using the above code, the homepage cannot be opened. I'm really not sure how to modify it to achieve it. If you could let me know, thank you very much!