You are here

protected function Tables::addJoin in Drupal 10

Same name in this branch
  1. 10 core/modules/workspaces/src/EntityQuery/Tables.php \Drupal\workspaces\EntityQuery\Tables::addJoin()
  2. 10 core/lib/Drupal/Core/Entity/Query/Sql/Tables.php \Drupal\Core\Entity\Query\Sql\Tables::addJoin()
Same name and namespace in other branches
  1. 8 core/modules/workspaces/src/EntityQuery/Tables.php \Drupal\workspaces\EntityQuery\Tables::addJoin()
  2. 9 core/modules/workspaces/src/EntityQuery/Tables.php \Drupal\workspaces\EntityQuery\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);
}