You are here

public function ViewsQueryAlter::alterQuery in Drupal 9

Same name and namespace in other branches
  1. 8 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 110

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->workspaceManager
      ->isEntityTypeSupported($entity_type_definitions[$entity_type_id])) {
      $this
        ->alterQueryForEntityType($query, $entity_type_definitions[$entity_type_id]);
    }
  }
}