Natural.php in Views Natural Sort 8.2
File
src/Plugin/views/sort/Natural.php
View source
<?php
namespace Drupal\views_natural_sort\Plugin\views\sort;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\ViewExecutable;
use Drupal\views\Views;
use Drupal\views\Plugin\views\sort\SortPluginBase;
class Natural extends SortPluginBase {
protected $isNaturalSort;
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
$this
->setNaturalSort(substr($this->options['order'], 0, 1) == 'N');
}
public function query() {
if (!$this
->isNaturalSort()) {
parent::query();
return;
}
$vns_alias = 'vns_' . $this->tableAlias;
if (empty($this->query->relationships[$vns_alias])) {
$this
->ensureMyTable();
$vns_alias = $this->query
->addRelationship('vns_' . $this->tableAlias, $this
->naturalSortJoin(), $this->table, $this->relationship);
}
$this->query
->addOrderBy($vns_alias, 'content', substr($this->options['order'], 1));
}
public function naturalSortJoin() {
$storage = Views::viewsData()
->getAll();
$table_data = $storage[$this->table];
$configuration = [
'table' => 'views_natural_sort',
'field' => 'eid',
'left_field' => $table_data['table']['base']['field'],
'left_table' => $this->table,
'extra' => [
[
'field' => 'entity_type',
'value' => $table_data['table']['entity type'],
],
[
'field' => 'field',
'value' => $this->realField,
],
],
];
$join = Views::pluginManager('join')
->createInstance('standard', $configuration);
return $join;
}
protected function sortOptions() {
$options = parent::sortOptions();
$options['NASC'] = $this
->t('Sort ascending naturally');
$options['NDESC'] = $this
->t('Sort descending naturally');
return $options;
}
public function adminSummary() {
if (!empty($this->options['exposed'])) {
return $this
->t('Exposed');
}
$label = parent::adminSummary();
switch ($this->options['order']) {
case 'NASC':
return $this
->t('natural asc');
case 'NDESC':
return $this
->t('natural asc');
default:
return $label;
}
}
public function isNaturalSort() {
return $this->isNaturalSort;
}
protected function setNaturalSort($value) {
$this->isNaturalSort = $value;
}
}
Classes
Name |
Description |
Natural |
Sort plugin used to allow Natural Sorting. |