public function Field::query

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 FieldPluginBase::query

File

drupal/core/modules/field/lib/Drupal/field/Plugin/views/field/Field.php, line 192
Definition of Drupal\field\Plugin\views\field\Field.

Class

Field
A field that displays fieldapi fields.

Namespace

Drupal\field\Plugin\views\field

Code

public function query($use_groupby = FALSE) {
  $this
    ->get_base_table();
  $entity_type = $this->definition['entity_tables'][$this->base_table];
  $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();
    $fields = array();
    $rkey = $this->definition['is revision'] ? 'FIELD_LOAD_REVISION' : 'FIELD_LOAD_CURRENT';

    // Go through the list and determine the actual column name from field api.
    foreach ($options as $column) {
      $name = $column;
      if (isset($this->field_info['storage_details']['sql'][$rkey][$this->table][$column])) {
        $name = $this->field_info['storage_details']['sql'][$rkey][$this->table][$column];
      }
      $fields[$column] = $name;
    }
    $this->group_fields = $fields;
  }

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

    // Filter by langcode, if field translation is enabled.
    $field = $this->field_info;
    if (field_is_translatable($entity_type, $field) && !empty($this->view->display_handler->options['field_langcode_add_to_query'])) {
      $column = $this->tableAlias . '.langcode';

      // By the same reason as field_language the field might be Language::LANGCODE_NOT_SPECIFIED in reality so allow it as well.
      // @see this::field_langcode()
      $default_langcode = language_default()->langcode;
      $langcode = str_replace(array(
        '***CURRENT_LANGUAGE***',
        '***DEFAULT_LANGUAGE***',
      ), array(
        drupal_container()
          ->get(Language::TYPE_CONTENT)->langcode,
        $default_langcode,
      ), $this->view->display_handler->options['field_langcode']);
      $placeholder = $this
        ->placeholder();
      $langcode_fallback_candidates = array(
        $langcode,
      );
      if (field_language_fallback_enabled()) {
        require_once DRUPAL_ROOT . '/includes/language.inc';
        $langcode_fallback_candidates = array_merge($langcode_fallback_candidates, language_fallback_get_candidates());
      }
      else {
        $langcode_fallback_candidates[] = Language::LANGCODE_NOT_SPECIFIED;
      }
      $this->query
        ->add_where_expression(0, "{$column} IN({$placeholder}) OR {$column} IS NULL", array(
        $placeholder => $langcode_fallback_candidates,
      ));
    }
  }
}