1 views_plugin_query_default.inc views_plugin_query_default::compile_fields($fields_array, $query)

Build fields array.

File

core/modules/views/plugins/views_plugin_query_default.inc, line 1177
Defines the default query object.

Class

views_plugin_query_default
Object used to create a SELECT query.

Code

function compile_fields($fields_array, $query) {
  $non_aggregates = array();
  foreach ($fields_array as $field) {
    $string = '';
    if (!empty($field['table'])) {
      $string .= $field['table'] . '.';
    }
    $string .= $field['field'];
    $fieldname = (!empty($field['alias']) ? $field['alias'] : $string);

    if (!empty($field['distinct'])) {
      throw new Exception("Column-level distinct is not supported anymore.");
    }

    if (!empty($field['count'])) {
      // Retained for compatibility
      $field['function'] = 'count';
      // It seems there's no way to abstract the table+column reference
      // without adding a field, aliasing, and then using the alias.
    }

    if (!empty($field['function'])) {
      $info = $this->get_aggregation_info();
      if (!empty($info[$field['function']]['method']) && function_exists($info[$field['function']]['method'])) {
        $string = $info[$field['function']]['method']($field['function'], $string);
        $placeholders = !empty($field['placeholders']) ? $field['placeholders'] : array();
        $query->addExpression($string, $fieldname, $placeholders);
      }

      $this->has_aggregate = TRUE;
    }
    // This is a formula, using no tables.
    elseif (empty($field['table'])) {
      $non_aggregates[] = $fieldname;
      $placeholders = !empty($field['placeholders']) ? $field['placeholders'] : array();
      $query->addExpression($string, $fieldname, $placeholders);
    }

    elseif ($this->distinct && !in_array($fieldname, $this->groupby)) {
      $query->addField(!empty($field['table']) ? $field['table'] : $this->base_table, $field['field'], $fieldname);
    }
    elseif (empty($field['aggregate'])) {
      $non_aggregates[] = $fieldname;
      $query->addField(!empty($field['table']) ? $field['table'] : $this->base_table, $field['field'], $fieldname);
    }

    // @TODO Remove this old code.
    if (!empty($field['distinct']) && empty($field['function'])) {
      $distinct[] = $string;
    }
    else {
      $fields[] = $string;
    }

    if ($this->get_count_optimized) {
      // We only want the first field in this case.
      break;
    }
  }
  return array(
    $non_aggregates,
  );
}