1 entityreference_plugin_display.inc entityreference_plugin_display::query()

Inject anything into the query that the display handler needs.

Overrides views_plugin_display::query

File

core/modules/entityreference/views/entityreference_plugin_display.inc, line 49
Handler for entityreference_plugin_display.

Class

entityreference_plugin_display
@file Handler for entityreference_plugin_display.

Code

function query() {
  $options = $this->get_option('entityreference_options');

  // Play nice with Views UI 'preview' : if the view is not executed through
  // EntityReferenceSelectionHandlerViews::getReferencableEntities(),
  // don't alter the query.
  if (empty($options)) {
    return;
  }

  // Make sure the id field is included in the results, and save its alias
  // so that references_plugin_style can retrieve it.
  $this->id_field_alias = $id_field = $this->view->query->add_field($this->view->base_table, $this->view->base_field);
  if (strpos($id_field, '.') === FALSE) {
    $id_field = $this->view->base_table . '.' . $this->id_field_alias;
  }

  // Restrict the autocomplete options based on what's been typed already.
  if (isset($options['match'])) {
    $style_options = $this->get_option('style_options');
    $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['search_fields'] as $field_alias) {
      if (!empty($field_alias)) {

        // Get the table and field names for the checked field.
        if (empty($this->view->field[$field_alias]->field_info)) {
          $field = $this->view->query->fields[$this->view->field[$field_alias]->field_alias];
        }
        else {
          $field_table = $this->view->query->ensure_table($this->view->field[$field_alias]->table, $this->view->field[$field_alias]->relationship);
          $this->view->query->add_field($field_table, $this->view->field[$field_alias]->real_field, $this->view->field[$field_alias]->field, array());
          $field = $this->view->query->fields[$this->view->field[$field_alias]->field];
        }
        // Add an OR condition for the field
        $conditions->condition($field['table'] . '.' . $field['field'], $value, 'LIKE');
      }
    }

    $this->view->query->add_where(NULL, $conditions);
  }

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

  $this->view->set_items_per_page($options['limit']);
}