function Tables::addJoin

Same name in this branch
  1. 11.x 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. 9 core/lib/Drupal/Core/Entity/Query/Sql/Tables.php \Drupal\Core\Entity\Query\Sql\Tables::addJoin()
  3. 8.9.x core/modules/workspaces/src/EntityQuery/Tables.php \Drupal\workspaces\EntityQuery\Tables::addJoin()
  4. 8.9.x core/lib/Drupal/Core/Entity/Query/Sql/Tables.php \Drupal\Core\Entity\Query\Sql\Tables::addJoin()
  5. 10 core/modules/workspaces/src/EntityQuery/Tables.php \Drupal\workspaces\EntityQuery\Tables::addJoin()
  6. 10 core/lib/Drupal/Core/Entity/Query/Sql/Tables.php \Drupal\Core\Entity\Query\Sql\Tables::addJoin()

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);
        $condition_parts_1 = str_replace([
            '[',
            ']',
        ], '', $condition_parts[1]);
        [
            $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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.