You are here

function social_event_node_view_alter in Open Social 8.4

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.5 modules/social_features/social_event/social_event.module \social_event_node_view_alter()
  6. 8.6 modules/social_features/social_event/social_event.module \social_event_node_view_alter()
  7. 8.7 modules/social_features/social_event/social_event.module \social_event_node_view_alter()
  8. 8.8 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 128
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' && ($display
    ->getMode() === 'teaser' || $display
    ->getMode() === 'small_teaser')) {
    $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().
    $enrollmenttag = 'enrollment:' . $nid . '-' . $uid;
    $build['#cache']['tags'][] = $enrollmenttag;
    $build['#cache']['contexts'][] = 'user';
    if (empty($nid)) {
      return;
    }
    $conditions = [
      'field_account' => $uid,
      'field_event' => $nid,
    ];
    $enrollments = \Drupal::service('entity.manager')
      ->getStorage('event_enrollment')
      ->loadByProperties($conditions);

    // Redirect anonymous use to login page before enrolling to an event.
    if ($enrollment = array_pop($enrollments)) {
      $current_enrollment_status = $enrollment->field_enrollment_status->value;
      if ($current_enrollment_status === '1') {
        $build['enrolled'] = [
          '#type' => '#text_field',
          '#markup' => t('Enrolled'),
        ];
      }
    }
  }
}