You are here

public function Field::query in Views (for Drupal 7) 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 FieldPluginBase::query

File

lib/Views/field/Plugin/views/field/Field.php, line 142
Definition of Views\field\Plugin\views\field\Field.

Class

Field
A field that displays fieldapi fields.

Namespace

Views\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
      ->add_additional_fields($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_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 (variable_get('locale_field_language_fallback', TRUE)) {
        require_once DRUPAL_ROOT . '/includes/language.inc';
        $langcode_fallback_candidates = array_merge($langcode_fallback_candidates, language_fallback_get_candidates());
      }
      else {
        $langcode_fallback_candidates[] = LANGUAGE_NOT_SPECIFIED;
      }
      $this->query
        ->add_where_expression(0, "{$column} IN({$placeholder}) OR {$column} IS NULL", array(
        $placeholder => $langcode_fallback_candidates,
      ));
    }
  }
}