You are here

public function CountryItem::query in Country 8

Same name in this branch
  1. 8 src/Plugin/views/filter/CountryItem.php \Drupal\country\Plugin\views\filter\CountryItem::query()
  2. 8 src/Plugin/views/sort/CountryItem.php \Drupal\country\Plugin\views\sort\CountryItem::query()

Called to add the sort to a query.

Sort by index of country names using sql FIELD function.

Overrides SortPluginBase::query

See also

http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_field

File

src/Plugin/views/sort/CountryItem.php, line 47

Class

CountryItem
Sort handler for country fields.

Namespace

Drupal\country\Plugin\views\sort

Code

public function query() {

  // Fall back to default sort for sorting by country code.
  if ($this->options['default_sort']) {
    return parent::query();
  }
  $this
    ->ensureMyTable();
  $country_codes = array_keys(\Drupal::service('country_manager')
    ->getList());
  $connection = Database::getConnection();
  $formula = 'FIELD(' . $this
    ->getField() . ', ' . implode(', ', array_map([
    $connection,
    'quote',
  ], $country_codes)) . ')';
  $this->query
    ->addOrderBy(NULL, $formula, $this->options['order'], $this->tableAlias . '_' . $this->field . '_country_name_sort');
}