Skip to content

Instantly share code, notes, and snippets.

@ummdorian ummdorian/index.php
Created Mar 28, 2018

What would you like to do?
Drupal 8 Query Alter Group By (views)
function module_query_views_events_alter(Drupal\Core\Database\Query\AlterableInterface $query){
// If this the right query
$query->getMetaData('view') != ''
&& $query->getMetaData('view')->getDisplay()->display['id'] == 'block_3'
// loop over fields in SELECT
$fields =& $query->getFields();
foreach($fields as $fieldIndex => $field){
// Add an expression to the query, necessary as drupal sets
$query->addExpression('MIN('.$field['table'].'.'.$field['field'].')', $field['alias']);
// Remove field from select
// Set group by
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.