I have content type "event". In this content type I have a date field, provided by the Date Recur module. At the result I have range of the dates: Event start and end date. Also Date Recur provides an option to repeat event. I need to select all event nodes where date field is greater then current date and repeat event option is empty or select all nodes, where repeat event option is not empty.

My code:

$current_time = \Drupal::service('date.formatter')->format(time(), 'custom', 'Y-m-d\TH:i:s');

$query = $this->db_connection->select('node__field_type', 'n_f_type');
$query->join('node_field_data', 'n_f_data',
  'n_f_data.nid = n_f_type.entity_id AND n_f_data.type = n_f_type.bundle');  

$query->join('node__field_event_dates', 'n_event_dates',
  'n_event_dates.bundle = n_f_type.bundle AND n_event_dates.entity_id = n_f_type.entity_id');
$query->fields('n_f_data', ['nid']);
$query->condition('n_f_type.bundle', 'event');
$query->condition('n_f_data.status', 1);
$group_and = $query->andConditionGroup();
$group_and->condition('n_event_dates.field_event_dates_value', $current_time, '>=');
$group_and->condition('n_event_dates.field_event_dates_rrule', '', '<>');
$group_or = $query->orConditionGroup();
$group_or->condition('n_event_dates.field_event_dates_rrule', '%RRULE%', 'LIKE');
$query->range(0, 500);
$events = $query->execute()->fetchAll();

Please, can anybody help me to build the correct query, because current query returns empty array.

SELECT * FROM table_name.node__field_event_dates where 
((field_event_dates_value >= '2019-07-17T10:00:00') and 
(field_event_dates_rrule is null or field_event_dates_rrule not like 
'%RRULE%')) or (field_event_dates_rrule like '%RRULE%');

Last query works fine;

I need some help to transform the last query via using Drupal and/or conditions.

1 Answer 1


Right now your rrule condition is a singleton of its "OR" group. You need to add all the other conditions to an "AND" group and place that group as a sibling to the rrule condition in the "OR" group.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.