You are here

public function GoogleAnalyticsQuery::addField in Google Analytics Reports 8.3

Add a metric or dimension to the query.

Parameters

string $table: NULL in most cases, we could probably remove this altogether.

string $field: The name of the metric/dimension/field to add.

string $alias: Probably could get rid of this too.

array $params: Probably could get rid of this too.

Return value

string The name that this field can be referred to as.

File

src/Plugin/views/query/GoogleAnalyticsQuery.php, line 156

Class

GoogleAnalyticsQuery
Defines a Views query class for Google Analytics Reports API.

Namespace

Drupal\google_analytics_reports\Plugin\views\query

Code

public function addField($table, $field, $alias = '', array $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;

  // We limit the length of the original alias up to 60 characters
  // to get a unique alias later if its have duplicates.
  $alias = 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;
}