QueryTest.php

Same filename in this branch
  1. 9 core/modules/views/tests/src/Kernel/Plugin/QueryTest.php
  2. 9 core/modules/views_ui/tests/src/Functional/QueryTest.php
  3. 9 core/tests/Drupal/KernelTests/Core/Database/QueryTest.php
  4. 9 core/tests/Drupal/Tests/Core/Entity/Query/Sql/QueryTest.php
Same filename in other branches
  1. 8.9.x core/modules/views/tests/src/Kernel/Plugin/QueryTest.php
  2. 8.9.x core/modules/views/tests/modules/views_test_data/src/Plugin/views/query/QueryTest.php
  3. 8.9.x core/modules/views_ui/tests/src/Functional/QueryTest.php
  4. 8.9.x core/tests/Drupal/KernelTests/Core/Database/QueryTest.php
  5. 8.9.x core/tests/Drupal/Tests/Core/Entity/Query/Sql/QueryTest.php
  6. 10 core/modules/views/tests/src/Kernel/Plugin/QueryTest.php
  7. 10 core/modules/views/tests/modules/views_test_data/src/Plugin/views/query/QueryTest.php
  8. 10 core/modules/views_ui/tests/src/Functional/QueryTest.php
  9. 10 core/tests/Drupal/KernelTests/Core/Database/QueryTest.php
  10. 10 core/tests/Drupal/Tests/Core/Entity/Query/Sql/QueryTest.php
  11. 11.x core/modules/views/tests/src/Kernel/Plugin/QueryTest.php
  12. 11.x core/modules/views/tests/modules/views_test_data/src/Plugin/views/query/QueryTest.php
  13. 11.x core/modules/views_ui/tests/src/Functional/QueryTest.php
  14. 11.x core/tests/Drupal/KernelTests/Core/Database/QueryTest.php
  15. 11.x core/tests/Drupal/Tests/Core/Entity/Query/Sql/QueryTest.php

Namespace

Drupal\views_test_data\Plugin\views\query

File

core/modules/views/tests/modules/views_test_data/src/Plugin/views/query/QueryTest.php

View source
<?php

namespace Drupal\views_test_data\Plugin\views\query;

use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\query\QueryPluginBase;
use Drupal\views\Plugin\views\join\JoinPluginBase;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;

/**
 * Defines a query test plugin.
 *
 * @ViewsQuery(
 *   id = "query_test",
 *   title = @Translation("Query test"),
 *   help = @Translation("Defines a query test plugin.")
 * )
 */
class QueryTest extends QueryPluginBase {
    protected $conditions = [];
    protected $fields = [];
    protected $allItems = [];
    protected $orderBy = [];
    
    /**
     * {@inheritdoc}
     */
    protected function defineOptions() {
        $options = parent::defineOptions();
        $options['test_setting'] = [
            'default' => '',
        ];
        return $options;
    }
    
    /**
     * {@inheritdoc}
     */
    public function buildOptionsForm(&$form, FormStateInterface $form_state) {
        parent::buildOptionsForm($form, $form_state);
        $form['test_setting'] = [
            '#title' => $this->t('Test setting'),
            '#type' => 'textfield',
            '#default_value' => $this->options['test_setting'],
        ];
    }
    
    /**
     * Sets the allItems property.
     *
     * @param array $allItems
     *   An array of stdClasses.
     */
    public function setAllItems($allItems) {
        $this->allItems = $allItems;
    }
    public function addWhere($group, $field, $value = NULL, $operator = NULL) {
        $this->conditions[] = [
            'field' => $field,
            'value' => $value,
            'operator' => $operator,
        ];
    }
    public function addField($table, $field, $alias = '', $params = []) {
        $this->fields[$field] = $field;
        return $field;
    }
    public function addOrderBy($table, $field = NULL, $order = 'ASC', $alias = '', $params = []) {
        $this->orderBy = [
            'field' => $field,
            'order' => $order,
        ];
    }
    public function ensureTable($table, $relationship = NULL, JoinPluginBase $join = NULL) {
        // There is no concept of joins.
    }
    
    /**
     * Implements Drupal\views\Plugin\views\query\QueryPluginBase::build().
     *
     * @param \Drupal\views\ViewExecutable $view
     *   The view executable.
     */
    public function build(ViewExecutable $view) {
        $this->view = $view;
        // @todo Support pagers for know, a php based one would probably match.
        // @todo You could add a string representation of the query.
        $this->view->build_info['query'] = "";
        $this->view->build_info['count_query'] = "";
    }
    
    /**
     * {@inheritdoc}
     */
    public function execute(ViewExecutable $view) {
        $result = [];
        foreach ($this->allItems as $element) {
            // Run all conditions on the element, and add it to the result if they
            // match.
            $match = TRUE;
            foreach ($this->conditions as $condition) {
                $match &= $this->match($element, $condition);
            }
            if ($match) {
                // If the query explicit defines fields to use, filter all others out.
                // Filter out fields
                if ($this->fields) {
                    $element = array_intersect_key($element, $this->fields);
                }
                $result[] = new ResultRow($element);
            }
        }
        $this->view->result = $result;
    }
    
    /**
     * Check a single condition for a single element.
     *
     * @param array $element
     *   The element which should be checked.
     * @param array $condition
     *   An associative array containing:
     *   - field: The field to by, for example id.
     *   - value: The expected value of the element.
     *   - operator: The operator to compare the element value with the expected
     *     value.
     *
     * @return bool
     *   Returns whether the condition matches with the element.
     */
    public function match($element, $condition) {
        $value = $element[$condition['field']];
        switch ($condition['operator']) {
            case '=':
                return $value == $condition['value'];
            case 'IN':
                return in_array($value, $condition['value']);
        }
        return FALSE;
    }
    
    /**
     * {@inheritdoc}
     */
    public function calculateDependencies() {
        return parent::calculateDependencies() + [
            'content' => [
                'QueryTest',
            ],
        ];
    }
    
    /**
     * {@inheritdoc}
     */
    public function setFieldTimezoneOffset(&$field, $offset) {
    }

}

Classes

Title Deprecated Summary
QueryTest Defines a query test plugin.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.