3 namespace Drupal\views_test_data\Plugin\views\query;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\Plugin\views\query\QueryPluginBase;
7 use Drupal\views\Plugin\views\join\JoinPluginBase;
8 use Drupal\views\ResultRow;
9 use Drupal\views\ViewExecutable;
12 * Defines a query test plugin.
16 * title = @Translation("Query test"),
17 * help = @Translation("Defines a query test plugin.")
20 class QueryTest extends QueryPluginBase {
21 protected $conditions = [];
22 protected $fields = [];
23 protected $allItems = [];
24 protected $orderBy = [];
29 protected function defineOptions() {
30 $options = parent::defineOptions();
31 $options['test_setting'] = ['default' => ''];
39 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
40 parent::buildOptionsForm($form, $form_state);
42 $form['test_setting'] = [
43 '#title' => $this->t('Test setting'),
44 '#type' => 'textfield',
45 '#default_value' => $this->options['test_setting'],
50 * Sets the allItems property.
52 * @param array $allItems
53 * An array of stdClasses.
55 public function setAllItems($allItems) {
56 $this->allItems = $allItems;
59 public function addWhere($group, $field, $value = NULL, $operator = NULL) {
60 $this->conditions[] = [
63 'operator' => $operator
68 public function addField($table, $field, $alias = '', $params = []) {
69 $this->fields[$field] = $field;
73 public function addOrderBy($table, $field = NULL, $order = 'ASC', $alias = '', $params = []) {
74 $this->orderBy = ['field' => $field, 'order' => $order];
78 public function ensureTable($table, $relationship = NULL, JoinPluginBase $join = NULL) {
79 // There is no concept of joins.
83 * Implements Drupal\views\Plugin\views\query\QueryPluginBase::build().
85 * @param \Drupal\views\ViewExecutable $view
87 public function build(ViewExecutable $view) {
89 // @todo Support pagers for know, a php based one would probably match.
90 // @todo You could add a string representation of the query.
91 $this->view->build_info['query'] = "";
92 $this->view->build_info['count_query'] = "";
98 public function execute(ViewExecutable $view) {
100 foreach ($this->allItems as $element) {
101 // Run all conditions on the element, and add it to the result if they
104 foreach ($this->conditions as $condition) {
105 $match &= $this->match($element, $condition);
108 // If the query explicit defines fields to use, filter all others out.
111 $element = array_intersect_key($element, $this->fields);
113 $result[] = new ResultRow($element);
116 $this->view->result = $result;
120 * Check a single condition for a single element.
122 * @param array $element
123 * The element which should be checked.
124 * @param array $condition
125 * An associative array containing:
126 * - field: The field to by, for example id.
127 * - value: The expected value of the element.
128 * - operator: The operator to compare the element value with the expected
132 * Returns whether the condition matches with the element.
134 public function match($element, $condition) {
135 $value = $element[$condition['field']];
136 switch ($condition['operator']) {
138 return $value == $condition['value'];
140 return in_array($value, $condition['value']);
148 public function calculateDependencies() {
149 return parent::calculateDependencies() + [
150 'content' => ['QueryTest'],
157 public function setFieldTimezoneOffset(&$field, $offset) {}