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];
77 public function ensureTable($table, $relationship = NULL, JoinPluginBase $join = NULL) {
78 // There is no concept of joins.
82 * Implements Drupal\views\Plugin\views\query\QueryPluginBase::build().
84 * @param \Drupal\views\ViewExecutable $view
86 public function build(ViewExecutable $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'] = "";
97 public function execute(ViewExecutable $view) {
99 foreach ($this->allItems as $element) {
100 // Run all conditions on the element, and add it to the result if they
103 foreach ($this->conditions as $condition) {
104 $match &= $this->match($element, $condition);
107 // If the query explicit defines fields to use, filter all others out.
110 $element = array_intersect_key($element, $this->fields);
112 $result[] = new ResultRow($element);
115 $this->view->result = $result;
119 * Check a single condition for a single element.
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
131 * Returns whether the condition matches with the element.
133 public function match($element, $condition) {
134 $value = $element[$condition['field']];
135 switch ($condition['operator']) {
137 return $value == $condition['value'];
139 return in_array($value, $condition['value']);
147 public function calculateDependencies() {
148 return parent::calculateDependencies() + [
149 'content' => ['QueryTest'],
156 public function setFieldTimezoneOffset(&$field, $offset) {}