You are here

public function EntityReferenceRevisions::query in Entity Reference Revisions 8

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

Overrides DisplayPluginBase::query

File

src/Plugin/views/display/EntityReferenceRevisions.php, line 113

Class

EntityReferenceRevisions
The plugin that handles an EntityReferenceRevisions display.

Namespace

Drupal\entity_reference_revisions\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_revisions_options');

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

    // Multiple search fields are OR'd together.
    $conditions = new Condition('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']);
}