You are here

public function ViewsQueryAlter::alterQuery in Workspace 8.2

Implements a hook bridge for hook_views_query_alter().

See also

hook_views_query_alter()

File

src/ViewsQueryAlter.php, line 98

Class

ViewsQueryAlter
Defines a class for altering views queries.

Namespace

Drupal\workspace

Code

public function alterQuery(ViewExecutable $view, QueryPluginBase $query) {

  // Don't alter any views queries if we're in the default workspace.
  if ($this->workspaceManager
    ->getActiveWorkspace()
    ->isDefaultWorkspace()) {
    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]);
    }
  }
}