Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to treat null values as heavier than the allowed values. #1

Open
wants to merge 1 commit into
base: 8.x-1.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions src/Plugin/views/sort/SortAllowedValues.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<?php

/**
* @file
* Definition of Drupal\views_list_sort\Plugin\views\sort\SortAllowedValues.
*/

namespace Drupal\views_list_sort\Plugin\views\sort;

use Drupal\Core\Database\Database;
Expand All @@ -13,9 +8,7 @@
use Drupal\views\Plugin\views\sort\SortPluginBase;

/**
* Basic sort handler for dates.
*
* This handler enables sorting by time instead of complete date.
* Sort handler for fields with allowed_values.
*
* @ingroup views_sort_handlers
*
Expand All @@ -31,6 +24,7 @@ class SortAllowedValues extends SortPluginBase {
protected function defineOptions() {
$options = parent::defineOptions();
$options['allowed_values'] = array('default' => 0);
$options['null_heavy'] = array('default' => 0);
return $options;
}

Expand All @@ -45,10 +39,17 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) {
'#options' => array(t('No'), t('Yes')),
'#default_value' => $this->options['allowed_values'],
);
$form['null_heavy'] = array(
'#type' => 'radios',
'#title' => t('Treat null values as heavier than the allowed values'),
'#options' => array(t('No'), t('Yes')),
'#default_value' => $this->options['null_heavy'],
);
}

/**
* Called to add the sort to a query.
*
* Sort by index of allowed values using sql FIELD function.
*
* @see http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_field
Expand All @@ -64,7 +65,16 @@ public function query() {
$allowed_values = array_keys(options_allowed_values($field_storage));
$connection = Database::getConnection();

$formula = 'FIELD(' . $this->tableAlias .'.'. $this->field .', '. implode(', ', array_map(array($connection, 'quote'), $allowed_values)) .')';
$formula = '';
// Reverse the values returned by the FIELD function and the allowed values
// so '0' is heavier than the rest.
if ($this->options['null_heavy']) {
$allowed_values = array_reverse($allowed_values);
$formula .= '-1 * ';
}

$formula .= 'FIELD(' . $this->tableAlias . '.' . $this->field . ', ' . implode(', ', array_map(array($connection, 'quote'), $allowed_values)) . ')';
$this->query->addOrderBy(NULL, $formula, $this->options['order'], $this->tableAlias . '_' . $this->field . '_allowed_values');
}

}