8868f75de667a3d3941129c8819c814a10da798e
[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
78   public function ensureTable($table, $relationship = NULL, JoinPluginBase $join = NULL) {
79     // There is no concept of joins.
80   }
81
82   /**
83    * Implements Drupal\views\Plugin\views\query\QueryPluginBase::build().
84    *
85    * @param \Drupal\views\ViewExecutable $view
86    */
87   public function build(ViewExecutable $view) {
88     $this->view = $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'] = "";
93   }
94
95   /**
96    * {@inheritdoc}
97    */
98   public function execute(ViewExecutable $view) {
99     $result = [];
100     foreach ($this->allItems as $element) {
101       // Run all conditions on the element, and add it to the result if they
102       // match.
103       $match = TRUE;
104       foreach ($this->conditions as $condition) {
105         $match &= $this->match($element, $condition);
106       }
107       if ($match) {
108         // If the query explicit defines fields to use, filter all others out.
109         // Filter out fields
110         if ($this->fields) {
111           $element = array_intersect_key($element, $this->fields);
112         }
113         $result[] = new ResultRow($element);
114       }
115     }
116     $this->view->result = $result;
117   }
118
119   /**
120    * Check a single condition for a single element.
121    *
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
129    *     value.
130    *
131    * @return bool
132    *   Returns whether the condition matches with the element.
133    */
134   public function match($element, $condition) {
135     $value = $element[$condition['field']];
136     switch ($condition['operator']) {
137       case '=':
138         return $value == $condition['value'];
139       case 'IN':
140         return in_array($value, $condition['value']);
141     }
142     return FALSE;
143   }
144
145   /**
146    * {@inheritdoc}
147    */
148   public function calculateDependencies() {
149     return parent::calculateDependencies() + [
150       'content' => ['QueryTest'],
151     ];
152   }
153
154   /**
155    * {@inheritdoc}
156    */
157   public function setFieldTimezoneOffset(&$field, $offset) {}
158
159 }