function ViewsQueryAlter::moveEntityTable

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

Moves a 'workspace_association' table to appear before the given alias.

Because Workspace chains possibly pre-existing tables onto the 'workspace_association' table, we have to ensure that the 'workspace_association' table appears in the query before the alias it's chained on or the SQL is invalid.

Parameters

\Drupal\views\Plugin\views\query\Sql $query: The SQL query object.

string $workspace_association_table: The alias of the 'workspace_association' table.

string $alias: The alias of the table it needs to appear before.

2 calls to ViewsQueryAlter::moveEntityTable()
ViewsQueryAlter::alterQueryForEntityType in core/modules/workspaces/src/ViewsQueryAlter.php
Alters the entity type tables for a Views query.
ViewsQueryAlter::ensureRevisionTable in core/modules/workspaces/src/ViewsQueryAlter.php
Adds the revision table of an entity type to a query object.

File

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

Class

ViewsQueryAlter
Defines a class for altering views queries.

Namespace

Drupal\workspaces

Code

protected function moveEntityTable(Sql $query, $workspace_association_table, $alias) {
    $table_queue =& $query->getTableQueue();
    $keys = array_keys($table_queue);
    $current_index = array_search($workspace_association_table, $keys);
    $index = array_search($alias, $keys);
    // If it's already before our table, we don't need to move it, as we could
    // accidentally move it forward.
    if ($current_index < $index) {
        return;
    }
    $splice = [
        $workspace_association_table => $table_queue[$workspace_association_table],
    ];
    unset($table_queue[$workspace_association_table]);
    // Now move the item to the proper location in the array. Don't use
    // array_splice() because that breaks indices.
    $table_queue = array_slice($table_queue, 0, $index, TRUE) + $splice + array_slice($table_queue, $index, NULL, TRUE);
}

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