Skip to content

Instantly share code, notes, and snippets.

@ummdorian
Created March 28, 2018 14:57
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ummdorian/02d53271a302b01ee297eaa3d1612288 to your computer and use it in GitHub Desktop.
Save ummdorian/02d53271a302b01ee297eaa3d1612288 to your computer and use it in GitHub Desktop.
Drupal 8 Query Alter Group By (views)
<?php
function module_query_views_events_alter(Drupal\Core\Database\Query\AlterableInterface $query){
// If this the right query
if(
$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
unset($fields[$fieldIndex]);
}
// Set group by
$query->groupBy('node__field_date.entity_id');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment