function Sql::compileFields

Same name in other branches
  1. 9 core/modules/views/src/Plugin/views/query/Sql.php \Drupal\views\Plugin\views\query\Sql::compileFields()
  2. 8.9.x core/modules/views/src/Plugin/views/query/Sql.php \Drupal\views\Plugin\views\query\Sql::compileFields()
  3. 11.x core/modules/views/src/Plugin/views/query/Sql.php \Drupal\views\Plugin\views\query\Sql::compileFields()

Adds fields to the query.

Parameters

\Drupal\Core\Database\Query\SelectInterface $query: The drupal query object.

1 call to Sql::compileFields()
Sql::query in core/modules/views/src/Plugin/views/query/Sql.php
Generates a query and count query from all of the information supplied.

File

core/modules/views/src/Plugin/views/query/Sql.php, line 1236

Class

Sql
Views query plugin for an SQL query.

Namespace

Drupal\views\Plugin\views\query

Code

protected function compileFields($query) {
    foreach ($this->fields as $field) {
        $string = '';
        if (!empty($field['table'])) {
            $string .= $field['table'] . '.';
        }
        $string .= $field['field'];
        $fieldname = !empty($field['alias']) ? $field['alias'] : $string;
        if (!empty($field['count'])) {
            // Retained for compatibility.
            $field['function'] = 'count';
        }
        if (!empty($field['function'])) {
            $info = $this->getAggregationInfo();
            if (!empty($info[$field['function']]['method']) && is_callable([
                $this,
                $info[$field['function']]['method'],
            ])) {
                $string = $this::{$info[$field['function']]['method']}($field['function'], $string);
                $placeholders = !empty($field['placeholders']) ? $field['placeholders'] : [];
                $query->addExpression($string, $fieldname, $placeholders);
            }
            $this->hasAggregate = TRUE;
        }
        elseif (empty($field['table'])) {
            $placeholders = !empty($field['placeholders']) ? $field['placeholders'] : [];
            $query->addExpression($string, $fieldname, $placeholders);
        }
        elseif ($this->distinct && !in_array($fieldname, $this->groupby)) {
            $query->addField(!empty($field['table']) ? $field['table'] : $this->view->storage
                ->get('base_table'), $field['field'], $fieldname);
        }
        elseif (empty($field['aggregate'])) {
            $query->addField(!empty($field['table']) ? $field['table'] : $this->view->storage
                ->get('base_table'), $field['field'], $fieldname);
        }
        if ($this->getCountOptimized) {
            // We only want the first field in this case.
            break;
        }
    }
}

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