summaryrefslogtreecommitdiffstats
path: root/src/Plugin/views/sort/Natural.php
blob: 3cb89f43d6d2e52cbdd94a914012e88f2b051d15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?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;

/**
 * Sort plugin used to allow Natural Sorting.
 *
 * @ingroup views_sort_handlers
 *
 * @ViewsSort("natural")
 */
class Natural extends SortPluginBase {

  /**
   * Flag defining this particular sort as Natural or not.
   *
   * @var bool
   */
  protected $isNaturalSort;

  /**
   * {@inheritdoc}
   */
  public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
    parent::init($view, $display, $options);
    $this->setNaturalSort(substr($this->options['order'], 0, 1) == 'N');
  }

  /**
   * {@inheritdoc}
   */
  public function query() {
    // If this field isn't being used as a Natural Sort Field, move along
    // nothing to see here.
    if (!$this->isNaturalSort()) {
      parent::query();
      return;
    }

    // Add the Views Natural Sort table for this field.
    $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));
  }

  /**
   * Adds the views_natural_sort table to the query.
   *
   * @return Drupal\views\Plugin\views\join\Standard
   *   Join object containing views_natural_sort table.
   */
  public function naturalSortJoin() {
    $table_data = Views::viewsData()->get($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;
  }

  /**
   * {@inheritdoc}
   */
  protected function sortOptions() {
    $options = parent::sortOptions();
    $options['NASC'] = $this->t('Sort ascending naturally');
    $options['NDESC'] = $this->t('Sort descending naturally');
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  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;
    }
  }

  /**
   * Determines if this query is natural sort.
   *
   * @return bool
   *   True if natural sort, False otherwise.
   */
  public function isNaturalSort() {
    return $this->isNaturalSort;
  }

  /**
   * Sets the natural sort flag.
   *
   * @param bool $value
   *   The value.
   */
  protected function setNaturalSort($value) {
    $this->isNaturalSort = $value;
  }

}