Created
March 28, 2018 14:57
-
-
Save ummdorian/02d53271a302b01ee297eaa3d1612288 to your computer and use it in GitHub Desktop.
Drupal 8 Query Alter Group By (views)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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