View source  
  <?php
namespace Drupal\config_views\Plugin\views\query;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\views\Plugin\views\join\JoinPluginBase;
use Drupal\views\Plugin\views\query\Sql;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;
class ConfigEntityQuery extends Sql {
  protected $commands = [];
  protected $entityConditionGroups;
  protected $sorting = [];
  
  public function ensureTable($table, $relationship = NULL, JoinPluginBase $join = NULL) {
  }
  
  public function addField($table, $field, $alias = '', $params = []) {
    return $alias ? $alias : $field;
  }
  
  public function condition($group, $field, $value = NULL, $operator = NULL, $langcode = NULL) {
    $this->commands[$group][] = [
      'method' => 'condition',
      'args' => [
        $field,
        $value,
        $operator,
        $langcode,
      ],
    ];
  }
  
  public function exists($group, $field, $langcode = NULL) {
    $this->commands[$group][] = [
      'method' => 'exists',
      'args' => [
        $field,
        $langcode,
      ],
    ];
  }
  
  public function notExists($group, $field, $langcode = NULL) {
    $this->commands[$group][] = [
      'method' => 'notExists',
      'args' => [
        $field,
        $langcode,
      ],
    ];
  }
  
  public function build(ViewExecutable $view) {
    
    $this->view = $view;
    $view
      ->initPager();
  }
  
  public function addOrderBy($table, $field = NULL, $order = 'ASC', $alias = '', $params = []) {
    if ($alias) {
      $this->sorting[$alias] = $order;
    }
    elseif ($field) {
      $this->sorting[$field] = $order;
    }
  }
  
  public function execute(ViewExecutable $view) {
    $this->group_operator = isset($this->group_operator) ? $this->group_operator : 'AND';
    $base_table = $this->view->storage
      ->get('base_table');
    $data = \Drupal::service('views.views_data')
      ->get($base_table);
    $entity_type = $data['table']['entity type'];
    $query = \Drupal::entityQuery($entity_type, $this->group_operator);
    $this->entityConditionGroups = [
      $query,
    ];
    $this
      ->buildConditions();
    $this
      ->buildSorting($query);
    $ids = $query
      ->execute();
    $results = \Drupal::entityTypeManager()
      ->getStorage($entity_type)
      ->loadMultiple($ids);
    $index = 0;
    
    foreach ($results as $result) {
      
      $entity = $result
        ->toArray();
      $entity['type'] = $entity_type;
      $entity['entity'] = $result;
      
      $entity['index'] = $index++;
      $view->result[] = new ResultRow($entity);
    }
    $view->total_rows = count($view->result);
    $view->execute_time = 0;
  }
  
  protected function buildConditions() {
    foreach ($this->commands as $group => $grouped_commands) {
      $conditionGroup = $this
        ->getConditionGroup($group);
      foreach ($grouped_commands as $command) {
        call_user_func_array([
          $conditionGroup,
          $command['method'],
        ], $command['args']);
      }
    }
  }
  
  protected function getConditionGroup($group) {
    if (!isset($this->entityConditionGroups[$group])) {
      $query = $this->entityConditionGroups[0];
      $condition = isset($this->where[$group]) && $this->where[$group]['type'] == 'OR' ? $query
        ->orConditionGroup() : $query
        ->andConditionGroup();
      $query
        ->condition($condition);
      $this->entityConditionGroups[$group] = $condition;
    }
    return $this->entityConditionGroups[$group];
  }
  
  protected function buildSorting(QueryInterface $query) {
    foreach ($this->sorting as $field => $direction) {
      $query
        ->sort($field, $direction);
    }
  }
}