function ViewsQueryAlter::alterQuery

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

Implements a hook bridge for hook_views_query_alter().

See also

hook_views_query_alter()

File

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

Class

ViewsQueryAlter
Defines a class for altering views queries.

Namespace

Drupal\workspaces

Code

public function alterQuery(ViewExecutable $view, QueryPluginBase $query) {
    // Don't alter any views queries if we're not in a workspace context.
    if (!$this->workspaceManager
        ->hasActiveWorkspace()) {
        return;
    }
    // Don't alter any non-sql views queries.
    if (!$query instanceof Sql) {
        return;
    }
    // Find out what entity types are represented in this query.
    $entity_type_ids = [];
    foreach ($query->relationships as $info) {
        $table_data = $this->viewsData
            ->get($info['base']);
        if (empty($table_data['table']['entity type'])) {
            continue;
        }
        $entity_type_id = $table_data['table']['entity type'];
        // This construct ensures each entity type exists only once.
        $entity_type_ids[$entity_type_id] = $entity_type_id;
    }
    $entity_type_definitions = $this->entityTypeManager
        ->getDefinitions();
    foreach ($entity_type_ids as $entity_type_id) {
        if ($this->workspaceInfo
            ->isEntityTypeSupported($entity_type_definitions[$entity_type_id])) {
            $this->alterQueryForEntityType($query, $entity_type_definitions[$entity_type_id]);
        }
    }
}

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