You are here

protected function Tables::addJoin in Drupal 8

Same name in this branch
  1. 8 core/modules/workspaces/src/EntityQuery/Tables.php \Drupal\workspaces\EntityQuery\Tables::addJoin()
  2. 8 core/lib/Drupal/Core/Entity/Query/Sql/Tables.php \Drupal\Core\Entity\Query\Sql\Tables::addJoin()
Same name and namespace in other branches
  1. 9 core/modules/workspaces/src/EntityQuery/Tables.php \Drupal\workspaces\EntityQuery\Tables::addJoin()
  2. 10 core/modules/workspaces/src/EntityQuery/Tables.php \Drupal\workspaces\EntityQuery\Tables::addJoin()

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

core/modules/workspaces/src/EntityQuery/Tables.php, line 85

Class

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

Namespace

Drupal\workspaces\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->entityTypeManager
        ->getActiveDefinition($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);
}