1 EntityReferenceSelectionHandlerGeneric.inc public EntityReferenceSelectionHandlerGeneric::ensureBaseTable(SelectQueryInterface $query)

Ensure a base table exists for the query.

If we have a field-only query, we want to assure we have a base-table so we can later alter the query in entityFieldQueryAlter().

Parameters

$query: The Select query.

Return value

The alias of the base-table.:

File

core/modules/entityreference/plugins/selection/EntityReferenceSelectionHandlerGeneric.inc, line 342
Generic Entity handler.

Class

EntityReferenceSelectionHandlerGeneric
@file Generic Entity handler.

Code

public function ensureBaseTable(SelectQueryInterface $query) {
  $tables = $query->getTables();

  // Check the current base table.
  foreach ($tables as $table) {
    if (empty($table['join'])) {
      $alias = $table['alias'];
      break;
    }
  }

  if (strpos($alias, 'field_data_') !== 0) {
    // The existing base-table is the correct one.
    return $alias;
  }

  // Join the known base-table.
  $target_type = $this->field['settings']['target_type'];
  $entity_info = entity_get_info($target_type);
  $target_type_base_table = $entity_info['base table'];
  $id = $entity_info['entity keys']['id'];

  // Return the alias of the table.
  return $query->innerJoin($target_type_base_table, NULL, "%alias.$id = $alias.entity_id");
}