function ViewsQueryAlter::ensureRevisionTable

Same name in other branches
  1. 8.9.x core/modules/workspaces/src/ViewsQueryAlter.php \Drupal\workspaces\ViewsQueryAlter::ensureRevisionTable()
  2. 10 core/modules/workspaces/src/ViewsQueryAlter.php \Drupal\workspaces\ViewsQueryAlter::ensureRevisionTable()
  3. 11.x core/modules/workspaces/src/ViewsQueryAlter.php \Drupal\workspaces\ViewsQueryAlter::ensureRevisionTable()

Adds the revision table of an entity type to a query object.

Parameters

\Drupal\Core\Entity\EntityTypeInterface $entity_type: The entity type definition.

\Drupal\views\Plugin\views\query\Sql $query: The query plugin object for the query.

string $relationship: The name of the relationship.

Return value

string The alias of the relationship.

1 call to ViewsQueryAlter::ensureRevisionTable()
ViewsQueryAlter::alterQueryForEntityType in core/modules/workspaces/src/ViewsQueryAlter.php
Alters the entity type tables for a Views query.

File

core/modules/workspaces/src/ViewsQueryAlter.php, line 330

Class

ViewsQueryAlter
Defines a class for altering views queries.

Namespace

Drupal\workspaces

Code

protected function ensureRevisionTable(EntityTypeInterface $entity_type, Sql $query, $relationship) {
    // Get the alias for the 'workspace_association' table we chain off of in
    // the COALESCE.
    $workspace_association_table = $this->ensureWorkspaceAssociationTable($entity_type->id(), $query, $relationship);
    // Get the name of the revision table and revision key.
    $base_revision_table = $entity_type->isTranslatable() ? $entity_type->getRevisionDataTable() : $entity_type->getRevisionTable();
    $revision_field = $entity_type->getKey('revision');
    // If the table was already added and has a join against the same field on
    // the revision table, reuse that rather than adding a new join.
    if (isset($query->tables[$relationship][$base_revision_table])) {
        $table_queue =& $query->getTableQueue();
        $alias = $query->tables[$relationship][$base_revision_table]['alias'];
        if (isset($table_queue[$alias]['join']->field) && $table_queue[$alias]['join']->field == $revision_field) {
            // If this table previously existed, but was not added by us, we need
            // to modify the join and make sure that 'workspace_association' comes
            // first.
            if (empty($table_queue[$alias]['join']->workspace_adjusted)) {
                $table_queue[$alias]['join'] = $this->getRevisionTableJoin($relationship, $base_revision_table, $revision_field, $workspace_association_table, $entity_type);
                // We also have to ensure that our 'workspace_association' comes before
                // this.
                $this->moveEntityTable($query, $workspace_association_table, $alias);
            }
            return $alias;
        }
    }
    // Construct a new join.
    $join = $this->getRevisionTableJoin($relationship, $base_revision_table, $revision_field, $workspace_association_table, $entity_type);
    return $query->queueTable($base_revision_table, $relationship, $join);
}

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