Skip to content

Instantly share code, notes, and snippets.

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

Embed
What would you like to do?
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
You can’t perform that action at this time.