You are here

protected function Tables::addJoin in Workspace 8.2

Adds a join to a given table.

Parameters

string $type: The join type.

string $table: The table to join to.

string $join_condition: The condition on which to join to.

string $langcode: The langcode we use on the join.

string|null $delta: (optional) A delta which should be used as additional condition.

Return value

string Returns the alias of the joined table.

Overrides Tables::addJoin

File

src/EntityQuery/Tables.php, line 84

Class

Tables
Alters entity queries to use a workspace revision instead of the default one.

Namespace

Drupal\workspace\EntityQuery

Code

protected function addJoin($type, $table, $join_condition, $langcode, $delta = NULL) {
  if ($this->sqlQuery
    ->getMetaData('active_workspace_id')) {

    // The join condition for a shared or dedicated field table is in the form
    // of "%alias.$id_field = $base_table.$id_field". Whenever we join a field
    // table we have to check:
    // 1) if $base_table is of an entity type that can belong to a workspace;
    // 2) if $id_field is the revision key of that entity type or the special
    // 'revision_id' string used when joining dedicated field tables.
    // If those two conditions are met, we have to update the join condition
    // to also look for a possible workspace-specific revision using COALESCE.
    $condition_parts = explode(' = ', $join_condition);
    list($base_table, $id_field) = explode('.', $condition_parts[1]);
    if (isset($this->baseTablesEntityType[$base_table])) {
      $entity_type_id = $this->baseTablesEntityType[$base_table];
      $revision_key = $this->entityManager
        ->getDefinition($entity_type_id)
        ->getKey('revision');
      if ($id_field === $revision_key || $id_field === 'revision_id') {
        $workspace_association_table = $this->contentWorkspaceTables[$base_table];
        $join_condition = "{$condition_parts[0]} = COALESCE({$workspace_association_table}.target_entity_revision_id, {$condition_parts[1]})";
      }
    }
  }
  return parent::addJoin($type, $table, $join_condition, $langcode, $delta);
}