You are here

public function ViewsDateFormatSqlField::query in Views Date Format SQL 8.3

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/ViewsDateFormatSqlField.php, line 57

Class

ViewsDateFormatSqlField
A field that displays entity timestamp field data. Supports grouping.

Namespace

Drupal\views_date_format_sql\Plugin\views\field

Code

public function query($use_groupby = FALSE) {
  if (empty($this->options['format_date_sql'])) {
    return parent::query($use_groupby);
  }
  $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 = array();
    if ($this->options['group_column'] != 'entity_id') {
      $options = array(
        $this->options['group_column'] => $this->options['group_column'],
      );
    }
    $options += is_array($this->options['group_columns']) ? $this->options['group_columns'] : array();

    // Go through the list and determine the actual column name from field api.
    $fields = array();
    $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();
  $this
    ->setDateFormat();

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

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