function EntityReference::query

Same name in other branches
  1. 8.9.x core/modules/views/src/Plugin/views/display/EntityReference.php \Drupal\views\Plugin\views\display\EntityReference::query()
  2. 10 core/modules/views/src/Plugin/views/display/EntityReference.php \Drupal\views\Plugin\views\display\EntityReference::query()
  3. 11.x core/modules/views/src/Plugin/views/display/EntityReference.php \Drupal\views\Plugin\views\display\EntityReference::query()

Overrides DisplayPluginBase::query

File

core/modules/views/src/Plugin/views/display/EntityReference.php, line 148

Class

EntityReference
The plugin that handles an EntityReference display.

Namespace

Drupal\views\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');
    $id_table = $this->view->storage
        ->get('base_table');
    $this->id_field_alias = $this->view->query
        ->addField($id_table, $id_field);
    $options = $this->getOption('entity_reference_options') ?? [];
    // The entity_reference_options are typically set during a call to
    // Drupal\views\Plugin\EntityReferenceSelection\ViewsSelection::initializeView().
    // If any entity_reference_options are not yet set, we apply the same
    // default values that would typically be added by that method.
    $default_options = [
        'match' => NULL,
        'match_operator' => 'CONTAINS',
        'limit' => 0,
        'ids' => NULL,
    ];
    $options += $default_options;
    // Restrict the autocomplete options based on what's been typed already.
    if (isset($options['match'])) {
        $style_options = $this->getOption('style');
        $value = $this->connection
            ->escapeLike($options['match']);
        if ($options['match_operator'] !== '=') {
            $value = $value . '%';
            if ($options['match_operator'] != 'STARTS_WITH') {
                $value = '%' . $value;
            }
        }
        // Multiple search fields are ORed together.
        $conditions = $this->view->query
            ->getConnection()
            ->condition('OR');
        // Build the condition using the selected search fields.
        foreach ($style_options['options']['search_fields'] as $field_id) {
            if (!empty($field_id)) {
                // Get the table and field names for the checked field.
                $field_handler = $this->view->field[$field_id];
                $table_alias = $this->view->query
                    ->ensureTable($field_handler->table, $field_handler->relationship);
                $field_alias = $this->view->query
                    ->addField($table_alias, $field_handler->realField);
                $field = $this->view->query->fields[$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_table . '.' . $id_field, $options['ids'], 'IN');
    }
    $this->view
        ->setItemsPerPage($options['limit']);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.