public function EntityReference::query

Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::query().

Overrides DisplayPluginBase::query

File

drupal/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/views/display/EntityReference.php, line 118
Contains \Drupal\entity_reference\Plugin\views\display\EntityReference.

Class

EntityReference
The plugin that handles an EntityReference display.

Namespace

Drupal\entity_reference\Plugin\views\display

Code

public function query() {
  if (!empty($this->view->live_preview)) {
    return;
  }

  // Make sure the id field is included in the results.
  $id_field = $this->view->storage
    ->get('base_field');
  $this->id_field_alias = $this->view->query
    ->addField($this->view->storage
    ->get('base_table'), $id_field);
  $options = $this
    ->getOption('entity_reference_options');

  // Restrict the autocomplete options based on what's been typed already.
  if (isset($options['match'])) {
    $style_options = $this
      ->getOption('style');
    $value = db_like($options['match']) . '%';
    if ($options['match_operator'] != 'STARTS_WITH') {
      $value = '%' . $value;
    }

    // Multiple search fields are OR'd together.
    $conditions = db_or();

    // Build the condition using the selected search fields.
    foreach ($style_options['options']['search_fields'] as $field_alias) {
      if (!empty($field_alias)) {

        // Get the table and field names for the checked field.
        $field = $this->view->query->fields[$this->view->field[$field_alias]->field_alias];

        // Add an OR condition for the field.
        $conditions
          ->condition($field['table'] . '.' . $field['field'], $value, 'LIKE');
      }
    }
    $this->view->query
      ->addWhere(0, $conditions);
  }

  // Add an IN condition for validation.
  if (!empty($options['ids'])) {
    $this->view->query
      ->addWhere(0, $id_field, $options['ids']);
  }
  $this->view
    ->setItemsPerPage($options['limit']);
}