$host_entity_type =
$this->entityTypeManager->
getDefinition($host_entity_type_id);
$host_entity_field_data_table =
$host_entity_type->
getDataTable();
// Not all entities have a data table, so check first.
if ($host_entity_field_data_table) { $id_key =
$host_entity_type->
getKey('id'
);
// The Comment module doesn't implement per-comment access, so it
// checks instead that the user has access to the host entity.
$entity_alias =
$query->
innerJoin($host_entity_field_data_table, 'n', "[%alias].[
$id_key] = [
$data_table].[entity_id] AND [
$data_table].[entity_type] = '
$host_entity_type_id'"
);
// Pass the query to the entity access control.
$this->
reAlterQuery($query,
$host_entity_type_id . '_access',
$entity_alias);
// Additional checks for "node" entities.
if ($host_entity_type_id === 'node'
) { // Passing the query to node_query_node_access_alter() is sadly
// insufficient for nodes.
// @see \Drupal\node\Plugin\EntityReferenceSelection\NodeSelection::buildEntityQuery()
if (!
$this->currentUser->
hasPermission('bypass node access'
) && !
$this->moduleHandler->
hasImplementations('node_grants'
)) { $query->
condition($entity_alias . '.status', 1
);
} } } }