You are here

function social_event_node_view_alter in Open Social 8.8

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. 10.3.x modules/social_features/social_event/social_event.module \social_event_node_view_alter()
  10. 10.0.x modules/social_features/social_event/social_event.module \social_event_node_view_alter()
  11. 10.1.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 207
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,
    ];
  }
}