public function ViewsRandomSeedRandom::query in Views random seed 8
Called to add the sort to a query.
Overrides SortPluginBase::query
File
- src/
Plugin/ views/ sort/ ViewsRandomSeedRandom.php, line 157
Class
- ViewsRandomSeedRandom
- Handle a random sort with seed.
Namespace
Drupal\views_random_seed\Plugin\views\sortCode
public function query() {
$db_type = \Drupal::database()
->driver();
$seed = $this->seedCalculator
->calculateSeed($this->options, $this->view
->id(), $this->view->current_display, $db_type);
switch ($db_type) {
case 'mysql':
case 'mysqli':
$formula = 'RAND(' . $seed . ')';
break;
case 'pgsql':
// For PgSQL we'll run an extra query with a integer between
// 0 and 1 which will be used by the RANDOM() function.
\Drupal::database()
->query('select setseed(' . $seed . ')');
\Drupal::database()
->query("select random()");
$formula = 'RANDOM()';
break;
}
if (!empty($formula)) {
// Use SearchAPI random sorting with seed if the query object is an
// instance of SearchApiViewsQuery (or a subclass of it). Pass along the
// seed and the built formula as options for the SearchApiQuery class.
// See: https://www.drupal.org/node/1197538#comment-10190520
if ($this->view->query instanceof SearchApiQuery) {
$this->query
->addOrderBy('rand', NULL, NULL, '', [
'seed' => $seed,
'formula' => $formula,
]);
}
else {
$this->query
->addOrderBy(NULL, $formula, $this->options['order'], '_' . $this->field);
}
}
}