71aa2defcc5e63ed9f2dfc1454266b03b8c92603
[yaffs-website] / web / core / modules / views / tests / modules / views_test_data / src / Plugin / views / query / QueryTest.php
1 <?php
2
3 namespace Drupal\views_test_data\Plugin\views\query;
4
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;
10
11 /**
12  * Defines a query test plugin.
13  *
14  * @ViewsQuery(
15  *   id = "query_test",
16  *   title = @Translation("Query test"),
17  *   help = @Translation("Defines a query test plugin.")
18  * )
19  */
20 class QueryTest extends QueryPluginBase {
21   protected $conditions = [];
22   protected $fields = [];
23   protected $allItems = [];
24   protected $orderBy = [];
25
26   /**
27    * {@inheritdoc}
28    */
29   protected function defineOptions() {
30     $options = parent::defineOptions();
31     $options['test_setting'] = ['default' => ''];
32
33     return $options;
34   }
35
36   /**
37    * {@inheritdoc}
38    */
39   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
40     parent::buildOptionsForm($form, $form_state);
41
42     $form['test_setting'] = [
43       '#title' => $this->t('Test setting'),
44       '#type' => 'textfield',
45       '#default_value' => $this->options['test_setting'],
46     ];
47   }
48
49   /**
50    * Sets the allItems property.
51    *
52    * @param array $allItems
53    *   An array of stdClasses.
54    */
55   public function setAllItems($allItems) {
56     $this->allItems = $allItems;
57   }
58
59   public function addWhere($group, $field, $value = NULL, $operator = NULL) {
60     $this->conditions[] = [
61       'field' => $field,
62       'value' => $value,
63       'operator' => $operator,
64     ];
65
66   }
67
68   public function addField($table, $field, $alias = '', $params = []) {
69     $this->fields[$field] = $field;
70     return $field;
71   }
72
73   public function addOrderBy($table, $field = NULL, $order = 'ASC', $alias = '', $params = []) {
74     $this->orderBy = ['field' => $field, 'order' => $order];
75   }
76
77   public function ensureTable($table, $relationship = NULL, JoinPluginBase $join = NULL) {
78     // There is no concept of joins.
79   }
80
81   /**
82    * Implements Drupal\views\Plugin\views\query\QueryPluginBase::build().
83    *
84    * @param \Drupal\views\ViewExecutable $view
85    */
86   public function build(ViewExecutable $view) {
87     $this->view = $view;
88     // @todo Support pagers for know, a php based one would probably match.
89     // @todo You could add a string representation of the query.
90     $this->view->build_info['query'] = "";
91     $this->view->build_info['count_query'] = "";
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   public function execute(ViewExecutable $view) {
98     $result = [];
99     foreach ($this->allItems as $element) {
100       // Run all conditions on the element, and add it to the result if they
101       // match.
102       $match = TRUE;
103       foreach ($this->conditions as $condition) {
104         $match &= $this->match($element, $condition);
105       }
106       if ($match) {
107         // If the query explicit defines fields to use, filter all others out.
108         // Filter out fields
109         if ($this->fields) {
110           $element = array_intersect_key($element, $this->fields);
111         }
112         $result[] = new ResultRow($element);
113       }
114     }
115     $this->view->result = $result;
116   }
117
118   /**
119    * Check a single condition for a single element.
120    *
121    * @param array $element
122    *   The element which should be checked.
123    * @param array $condition
124    *   An associative array containing:
125    *   - field: The field to by, for example id.
126    *   - value: The expected value of the element.
127    *   - operator: The operator to compare the element value with the expected
128    *     value.
129    *
130    * @return bool
131    *   Returns whether the condition matches with the element.
132    */
133   public function match($element, $condition) {
134     $value = $element[$condition['field']];
135     switch ($condition['operator']) {
136       case '=':
137         return $value == $condition['value'];
138       case 'IN':
139         return in_array($value, $condition['value']);
140     }
141     return FALSE;
142   }
143
144   /**
145    * {@inheritdoc}
146    */
147   public function calculateDependencies() {
148     return parent::calculateDependencies() + [
149       'content' => ['QueryTest'],
150     ];
151   }
152
153   /**
154    * {@inheritdoc}
155    */
156   public function setFieldTimezoneOffset(&$field, $offset) {}
157
158 }