SortAllowedValues.php in Views List Sort 8
File
src/Plugin/views/sort/SortAllowedValues.php
View source
<?php
namespace Drupal\views_list_sort\Plugin\views\sort;
use Drupal\Core\Database\Database;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\FieldAPIHandlerTrait;
use Drupal\views\Plugin\views\sort\SortPluginBase;
class SortAllowedValues extends SortPluginBase {
use FieldAPIHandlerTrait;
protected function defineOptions() {
$options = parent::defineOptions();
$options['allowed_values'] = [
'default' => 0,
];
$options['null_heavy'] = [
'default' => 0,
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['allowed_values'] = [
'#type' => 'radios',
'#title' => t('Sort by allowed values'),
'#options' => [
t('No'),
t('Yes'),
],
'#default_value' => $this->options['allowed_values'],
];
$form['null_heavy'] = [
'#type' => 'radios',
'#title' => t('Treat null values as heavier than the allowed values'),
'#options' => [
t('No'),
t('Yes'),
],
'#default_value' => $this->options['null_heavy'],
];
}
public function query() {
$this
->ensureMyTable();
if (!$this->options['allowed_values']) {
return;
}
$field_storage = $this
->getFieldStorageDefinition();
$allowed_values = array_keys(options_allowed_values($field_storage));
$connection = Database::getConnection();
$formula = '';
if ($this->options['null_heavy']) {
$allowed_values = array_reverse($allowed_values);
$formula .= '-1 * ';
}
$formula .= 'FIELD(' . $this->tableAlias . '.' . $this->field . ', ' . implode(', ', array_map([
$connection,
'quote',
], $allowed_values)) . ')';
$this->query
->addOrderBy(NULL, $formula, $this->options['order'], $this->tableAlias . '_' . $this->field . '_allowed_values');
}
}