You are here

function social_event_node_view_alter in Open Social 10.1.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_event/social_event.module \social_event_node_view_alter()
  2. 8 modules/social_features/social_event/social_event.module \social_event_node_view_alter()
  3. 8.2 modules/social_features/social_event/social_event.module \social_event_node_view_alter()
  4. 8.3 modules/social_features/social_event/social_event.module \social_event_node_view_alter()
  5. 8.4 modules/social_features/social_event/social_event.module \social_event_node_view_alter()
  6. 8.5 modules/social_features/social_event/social_event.module \social_event_node_view_alter()
  7. 8.6 modules/social_features/social_event/social_event.module \social_event_node_view_alter()
  8. 8.7 modules/social_features/social_event/social_event.module \social_event_node_view_alter()
  9. 8.8 modules/social_features/social_event/social_event.module \social_event_node_view_alter()
  10. 10.3.x modules/social_features/social_event/social_event.module \social_event_node_view_alter()
  11. 10.0.x modules/social_features/social_event/social_event.module \social_event_node_view_alter()
  12. 10.2.x modules/social_features/social_event/social_event.module \social_event_node_view_alter()

Implements hook_ENTITY_TYPE_view_alter().

File

modules/social_features/social_event/social_event.module, line 246
The Social event module.

Code

function social_event_node_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
  $current_user = \Drupal::currentUser();
  if (!$current_user
    ->isAnonymous() && $entity
    ->getType() === 'event') {
    $uid = $current_user
      ->id();
    $nid = $entity
      ->id();

    // Create our custom enrollment tag so we can also invalidate f.e. teasers
    // cache when people enrol. See EnrollActionForm->submitForm().
    $enrollment_tag = 'enrollment:' . $nid . '-' . $uid;
    $build['#cache']['tags'][] = $enrollment_tag;
    $build['#cache']['contexts'][] = 'user';
    if (empty($nid)) {
      return;
    }
    $storage = \Drupal::entityTypeManager()
      ->getStorage('event_enrollment');

    // Prepare 'Enrolled' label for teasers.
    $enrolled = $storage
      ->loadByProperties([
      'field_account' => $uid,
      'field_event' => $nid,
      'field_enrollment_status' => 1,
    ]);
    if ($enrolled) {
      $build['enrolled'] = [
        '#type' => '#text_field',
        '#markup' => t('You have enrolled'),
      ];
    }

    // Prepare enrollments counter for teasers.
    $enrollments_count = $storage
      ->getQuery()
      ->condition('field_event', $nid)
      ->condition('field_enrollment_status', 1)
      ->count()
      ->execute();
    $build['enrollments_count'] = [
      '#type' => '#text_field',
      '#markup' => $enrollments_count,
    ];

    // Check if ongoing.
    // Get user current date and time.
    $user_time = new DateTime(date(DateTimeItemInterface::DATETIME_STORAGE_FORMAT), new DateTimeZone(date_default_timezone_get()));

    // Convert to UTC.
    $user_time
      ->setTimezone(new DateTimeZone(DateTimeItemInterface::STORAGE_TIMEZONE));
    $date = $user_time
      ->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
    $node_storage = \Drupal::entityTypeManager()
      ->getStorage('node');
    $ongoing = $node_storage
      ->getQuery()
      ->condition('field_event_date', $date, '<=')
      ->condition('field_event_date_end', $date, '>=')
      ->condition('nid', $nid)
      ->count()
      ->execute();
    if ($ongoing) {
      $build['ongoing'] = [
        '#type' => '#text_field',
        '#markup' => t('Ongoing'),
      ];
    }
  }
}