Skip to content

Instantly share code, notes, and snippets.

@ummdorian
Created February 27, 2018 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ummdorian/5a8007e16c37e8e73aa7232550c35bae to your computer and use it in GitHub Desktop.
Save ummdorian/5a8007e16c37e8e73aa7232550c35bae to your computer and use it in GitHub Desktop.
Views Nested Condition Group
<?php
function adw_calendar_query_views_events_list_alter(Drupal\Core\Database\Query\AlterableInterface $query){
if(
$query->getMetaData('view')->getDisplay()->display['id'] == 'page_1'
|| $query->getMetaData('view')->getDisplay()->display['id'] == 'page_2'
){
// Get the filters set for the date range
$dateFilter = $query->getMetaData('view')->exposed_data['date'];
$startFilterFormatted = date('Y-m-d',strtotime($dateFilter['min']));
$endFilterFormatted = date('Y-m-d',strtotime($dateFilter['max']));
// Get the second condition which is the one we're interested in.
$conditions =& $query->conditions()[0]['field']->conditions()[1]['field'];
// Create a new conditions group
$newConditionGroup = $query->andConditionGroup()
->condition('node__field_event_start_date.field_event_start_date_value', $startFilterFormatted, '<')
->condition('node__field_event_end_date.field_event_end_date_value', $endFilterFormatted, '>')
;
// Add our new condition group to the second condition
$conditions->condition($newConditionGroup);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment