function Sql::addField

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

Adds a field to the query table, possibly with an alias.

This will automatically call ensureTable to make sure the required table exists, *unless* $table is unset.

Parameters

$table: The table this field is attached to. If NULL, it is assumed this will be a formula; otherwise, ensureTable is used to make sure the table exists.

$field: The name of the field to add. This may be a real field or a formula.

$alias: The alias to create. If not specified, the alias will be $table_$field unless $table is NULL. When adding formulae, it is recommended that an alias be used.

$params: An array of parameters additional to the field that will control items such as aggregation functions and DISTINCT. Some values that are recognized:

  • function: An aggregation function to apply, such as SUM.
  • aggregate: Set to TRUE to indicate that this value should be aggregated in a GROUP BY.

Return value

string The name that this field can be referred to as. Usually this is the alias.

2 calls to Sql::addField()
Sql::addOrderBy in core/modules/views/src/Plugin/views/query/Sql.php
Add an ORDER BY clause to the query.
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 840

Class

Sql
Views query plugin for an SQL query.

Namespace

Drupal\views\Plugin\views\query

Code

public function addField($table, $field, $alias = '', $params = []) {
    // We check for this specifically because it gets a special alias.
    if ($table == $this->view->storage
        ->get('base_table') && $field == $this->view->storage
        ->get('base_field') && empty($alias)) {
        $alias = $this->view->storage
            ->get('base_field');
    }
    if ($table && empty($this->tableQueue[$table])) {
        $this->ensureTable($table);
    }
    if (!$alias && $table) {
        $alias = $table . '_' . $field;
    }
    // Make sure an alias is assigned
    $alias = $alias ? $alias : $field;
    // PostgreSQL truncates aliases to 63 characters:
    // https://www.drupal.org/node/571548.
    // We limit the length of the original alias up to 60 characters
    // to get a unique alias later if its have duplicates
    $alias = strtolower(substr($alias, 0, 60));
    // Create a field info array.
    $field_info = [
        'field' => $field,
        'table' => $table,
        'alias' => $alias,
    ] + $params;
    // Test to see if the field is actually the same or not. Due to
    // differing parameters changing the aggregation function, we need
    // to do some automatic alias collision detection:
    $base = $alias;
    $counter = 0;
    while (!empty($this->fields[$alias]) && $this->fields[$alias] != $field_info) {
        $field_info['alias'] = $alias = $base . '_' . ++$counter;
    }
    if (empty($this->fields[$alias])) {
        $this->fields[$alias] = $field_info;
    }
    // Keep track of all aliases used.
    $this->fieldAliases[$table][$field] = $alias;
    return $alias;
}

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