You are here

public function ReportDateField::query in Commerce Reporting 8

Called to add the field to a query.

By default, all needed data is taken from entities loaded by the query plugin. Columns are added only if they are used in groupings.

Overrides EntityField::query

File

src/Plugin/views/field/ReportDateField.php, line 121

Class

ReportDateField
Plugin annotation @ViewsField("commerce_reports_report_date_field");

Namespace

Drupal\commerce_reports\Plugin\views\field

Code

public function query($use_groupby = FALSE) {
  $fields = $this->additional_fields;

  // No need to add the entity type.
  $entity_type_key = array_search('entity_type', $fields);
  if ($entity_type_key !== FALSE) {
    unset($fields[$entity_type_key]);
  }
  if ($use_groupby) {

    // Add the fields that we're actually grouping on.
    $options = [];
    if ($this->options['group_column'] != 'entity_id') {
      $options = [
        $this->options['group_column'] => $this->options['group_column'],
      ];
    }
    $options += is_array($this->options['group_columns']) ? $this->options['group_columns'] : [];

    // Go through the list and determine the actual column name from field api.
    $fields = [];
    $table_mapping = $this
      ->getTableMapping();
    $field_definition = $this
      ->getFieldStorageDefinition();
    foreach ($options as $column) {
      $fields[$column] = $table_mapping
        ->getFieldColumnName($field_definition, $column);
    }
    $this->group_fields = $fields;
  }

  // Add additional fields (and the table join itself) if needed.
  $this
    ->add_field_table($use_groupby);
  $this
    ->ensureMyTable();

  // Add the field.
  $params = $this->options['group_type'] !== 'group' ? [
    'function' => $this->options['group_type'],
  ] : [];
  $expression = $this->query
    ->getDateFormat("FROM_UNIXTIME({$this->tableAlias}.{$this->realField})", $this->dateFormatString);
  $this->field_alias = $this->query
    ->addField(NULL, $expression, "{$this->tableAlias}_{$this->realField}", $params);
  $this->query
    ->addGroupBy($this->field_alias);
  $this->aliases[$this->definition['field_name']] = $this->field_alias;

  // Let the entity field renderer alter the query if needed.
  $this
    ->getEntityFieldRenderer()
    ->query($this->query, $this->relationship);
}