You are here

protected function Tables::addNextBaseTable in Dynamic Entity Reference 8.2

Add the next entity base table.

For example, when building the SQL query for

condition('uid.entity.name', 'foo', 'CONTAINS');

this adds the users table.

Parameters

\Drupal\Core\Entity\EntityType $entity_type: The entity type being joined, in the above example, User.

string $table: This is the table being joined, in the above example, {users}.

string $sql_column: This is the SQL column in the existing table being joined to.

\Drupal\Core\Field\FieldStorageDefinitionInterface $field_storage: The field storage definition for the field referencing this column.

Return value

string The alias of the next entity table joined in.

Overrides Tables::addNextBaseTable

File

src/Query/Tables.php, line 18

Class

Tables
Adds tables and fields to the SQL entity query.

Namespace

Drupal\dynamic_entity_reference\Query

Code

protected function addNextBaseTable(EntityType $entity_type, $table, $sql_column, FieldStorageDefinitionInterface $field_storage = NULL) {

  // Parent method is overridden in order to choose the correct columns to
  // join on (string or int for the id and type for the entity type
  // specifier).
  $entity_type_id_key = $entity_type
    ->getKey('id');
  $entity_type_id = $entity_type
    ->id();
  $join_condition_type = '';
  if ($field_storage && $field_storage
    ->getType() === 'dynamic_entity_reference' && $entity_type_id_key !== FALSE) {

    // DER basefield SQL column are named entity_name__target_id and
    // entity_name__target_type where as config field columns are named
    // field_name_target_id and field_name_target_type.
    $sql_column_type = str_replace('target_id', 'target_type', $sql_column);
    if (DynamicEntityReferenceItem::entityHasIntegerId($entity_type_id)) {
      $sql_column .= '_int';
    }

    // Prepare to modify the join with an entity type specifier. This can
    // prevent obscure cases where entities of different types (e.g. node vs.
    // taxonomy_term) with the same id are returned.
    $join_condition_type = " AND [{$table}].[{$sql_column_type}] = '{$entity_type_id}'";
  }
  $join_condition = "[%alias].[{$entity_type_id_key}] = [{$table}].[{$sql_column}]" . $join_condition_type;
  return $this->sqlQuery
    ->leftJoin($entity_type
    ->getBaseTable(), NULL, $join_condition);
}