You are here

public function EventContentBlock::query in Open Social 10.3.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_content_block/modules/social_event_content_block/src/Plugin/ContentBlock/EventContentBlock.php \Drupal\social_event_content_block\Plugin\ContentBlock\EventContentBlock::query()
  2. 8.8 modules/social_features/social_content_block/modules/social_event_content_block/src/Plugin/ContentBlock/EventContentBlock.php \Drupal\social_event_content_block\Plugin\ContentBlock\EventContentBlock::query()
  3. 10.0.x modules/social_features/social_content_block/modules/social_event_content_block/src/Plugin/ContentBlock/EventContentBlock.php \Drupal\social_event_content_block\Plugin\ContentBlock\EventContentBlock::query()
  4. 10.1.x modules/social_features/social_content_block/modules/social_event_content_block/src/Plugin/ContentBlock/EventContentBlock.php \Drupal\social_event_content_block\Plugin\ContentBlock\EventContentBlock::query()
  5. 10.2.x modules/social_features/social_content_block/modules/social_event_content_block/src/Plugin/ContentBlock/EventContentBlock.php \Drupal\social_event_content_block\Plugin\ContentBlock\EventContentBlock::query()

Create filtering query.

Parameters

\Drupal\Core\Database\Query\SelectInterface $query: The query.

array $fields: The fields.

Overrides ContentBlockPluginInterface::query

File

modules/social_features/social_content_block/modules/social_event_content_block/src/Plugin/ContentBlock/EventContentBlock.php, line 68

Class

EventContentBlock
Provides a content block for events.

Namespace

Drupal\social_event_content_block\Plugin\ContentBlock

Code

public function query(SelectInterface $query, array $fields) {
  foreach ($fields as $field_name => $field_value) {
    switch ($field_name) {
      case 'field_event_type':
        $query
          ->innerJoin('node__field_event_type', 'et', 'et.entity_id = base_table.nid');
        $query
          ->condition('et.field_event_type_target_id', $field_value, 'IN');
        break;
      case 'field_event_content_tag':
        $query
          ->innerJoin('node__social_tagging', 'st', 'st.entity_id = base_table.nid');
        $query
          ->condition('st.social_tagging_target_id', $field_value, 'IN');
        break;
      case 'field_event_group':
        $query
          ->innerJoin('group_content_field_data', 'gc', 'gc.entity_id = base_table.nid');
        $query
          ->condition('gc.type', '%' . $query
          ->escapeLike('-group_node-event'), 'LIKE');
        $query
          ->condition('gc.gid', $field_value, 'IN');
        break;

      // Filter for events in a certain date range.
      case 'field_event_date':
        $query
          ->innerJoin('node__field_event_date', 'nfed', "nfed.entity_id = base_table.nid AND nfed.bundle = 'event'");
        $range = [
          'start' => NULL,
          'end' => NULL,
        ];
        $start_operator = '>=';
        $end_operator = '<';

        // Apply a range based on a value.
        switch ($field_value[0]['value']) {
          case 'future':
            $range['start'] = new \DateTime();
            break;
          case 'past':
            $range['end'] = new \DateTime();
            break;
          case 'last_month':
            $range['start'] = new \DateTime('first day of last month 00:00');
            $range['end'] = new \DateTime('last day of last month 23:59');
            break;
          case 'current_month':
            $range['start'] = new \DateTime('first day of this month 00:00');
            $range['end'] = new \DateTime('last day of this month 23:59');
            break;
          case 'next_month':
            $range['start'] = new \DateTime('first day of next month 00:00');
            $range['end'] = new \DateTime('last day of next month 23:59');
            break;
          case 'ongoing':
            $range['start'] = new \DateTime('-30 days');
            $range['end'] = new \DateTime();
            $end_operator = '>';
            $query
              ->condition('nfed.field_event_date_value', (new \DateTime())
              ->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT), '<');
            break;
          case 'last_30':
            $range['start'] = new \DateTime('-30 days');
            $range['end'] = new \DateTime();
            break;
          case 'next_30':
            $range['start'] = new \DateTime();
            $range['end'] = new \DateTime('+30 days');
            break;
          case 'last_14':
            $range['start'] = new \DateTime('-14 days');
            $range['end'] = new \DateTime();
            break;
          case 'next_14':
            $range['start'] = new \DateTime();
            $range['end'] = new \DateTime('+14 days');
            break;
          case 'last_7':
            $range['start'] = new \DateTime('-7 days');
            $range['end'] = new \DateTime();
            break;
          case 'next_7':
            $range['start'] = new \DateTime();
            $range['end'] = new \DateTime('+7 days');
            break;
          default:

            // If we can't handle it allow other modules a chance.
            $this->moduleHandler
              ->alter('social_event_content_block_date_range', $range, $field_value);
        }

        // Only apply range constraints if any were actually set.
        if (isset($range['start'])) {
          $query
            ->condition('nfed.field_event_date_value', $range['start'] instanceof \DateTime ? $range['start']
            ->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT) : $range['start'], $start_operator);
        }
        if (isset($range['end'])) {
          $query
            ->innerJoin('node__field_event_date_end', 'nfede', "nfede.entity_id = base_table.nid AND nfede.bundle = 'event'");
          $query
            ->condition('nfede.field_event_date_end_value', $range['end'] instanceof \DateTime ? $range['end']
            ->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT) : $range['end'], $end_operator);
        }
        break;
    }
  }
}