You are here

function social_follow_content_event_enrollment_follow in Open Social 10.2.x

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

Automatically follow content when user enrolls the event.

2 calls to social_follow_content_event_enrollment_follow()
social_follow_content_event_enrollment_insert in modules/social_features/social_follow_content/social_follow_content.module
Implements hook_ENTITY_TYPE_insert().
social_follow_content_event_enrollment_update in modules/social_features/social_follow_content/social_follow_content.module
Implements hook_ENTITY_TYPE_update().

File

modules/social_features/social_follow_content/social_follow_content.module, line 59
The Social Follow Content module.

Code

function social_follow_content_event_enrollment_follow(EntityInterface $entity) {
  if ($entity->field_enrollment_status->value) {

    // Check if user already follow this content.
    // Only logged in users can follow content.
    $account = \Drupal::currentUser();
    if (!$account
      ->isAuthenticated()) {
      return;
    }
    $user_id = $account
      ->id();

    // If the user got enrolled by someone else, then use the target_id as user.
    if ($account
      ->id() !== $entity->field_account->target_id) {
      $user_id = $entity->field_account->target_id;
    }
    $properties = [
      'uid' => $user_id,
      'entity_type' => 'node',
      'entity_id' => $entity->field_event->target_id,
      'flag_id' => 'follow_content',
    ];
    $flaggings = \Drupal::entityTypeManager()
      ->getStorage('flagging')
      ->loadByProperties($properties);
    $flagging = reset($flaggings);
    if (empty($flagging)) {
      $flagging = Flagging::create($properties);
      if ($flagging) {
        $flagging
          ->save();

        // No need to set a message if the user got enrolled by someone else.
        if ($account
          ->id() === $entity->field_account->target_id) {
          $message = t('You have successfully enrolled to this event. Also you are now following the event, which means you will receive notifications when a new comment is placed.');
          \Drupal::messenger()
            ->addStatus($message);
        }
      }
    }
  }
}