You are here

class EventContentBlock in Open Social 10.1.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
  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
  3. 10.3.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
  4. 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
  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

Provides a content block for events.

Plugin annotation


@ContentBlock(
  id = "event_content_block",
  entityTypeId = "node",
  bundle = "event",
  fields = {
    "field_event_type",
    "field_event_content_tag",
    "field_event_group",
    "field_event_date",
  },
)

Hierarchy

Expanded class hierarchy of EventContentBlock

File

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

Namespace

Drupal\social_event_content_block\Plugin\ContentBlock
View source
class EventContentBlock extends ContentBlockBase implements ContainerFactoryPluginInterface {

  /**
   * Module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * BookAddBlock constructor.
   *
   * @param array $configuration
   *   The given configuration.
   * @param string $plugin_id
   *   The given plugin id.
   * @param mixed $plugin_definition
   *   The given plugin definition.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   Module handler.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandlerInterface $module_handler) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->moduleHandler = $module_handler;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('module_handler'));
  }

  /**
   * {@inheritdoc}
   */
  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;
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function supportedSortOptions() : array {
    $defaults = parent::supportedSortOptions();
    return [
      'event_date' => 'Event date',
    ] + $defaults;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EventContentBlock::$moduleHandler protected property Module handler.
EventContentBlock::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
EventContentBlock::query public function Create filtering query. Overrides ContentBlockPluginInterface::query
EventContentBlock::supportedSortOptions public function The sort options that are supported for this content block type. Overrides ContentBlockBase::supportedSortOptions
EventContentBlock::__construct public function BookAddBlock constructor. Overrides PluginBase::__construct
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.