You are here

public static function SocialEventInviteBulkHelper::bulkInviteEmails in Open Social 10.0.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_event/modules/social_event_invite/src/SocialEventInviteBulkHelper.php \Drupal\social_event_invite\SocialEventInviteBulkHelper::bulkInviteEmails()
  2. 10.3.x modules/social_features/social_event/modules/social_event_invite/src/SocialEventInviteBulkHelper.php \Drupal\social_event_invite\SocialEventInviteBulkHelper::bulkInviteEmails()
  3. 10.1.x modules/social_features/social_event/modules/social_event_invite/src/SocialEventInviteBulkHelper.php \Drupal\social_event_invite\SocialEventInviteBulkHelper::bulkInviteEmails()
  4. 10.2.x modules/social_features/social_event/modules/social_event_invite/src/SocialEventInviteBulkHelper.php \Drupal\social_event_invite\SocialEventInviteBulkHelper::bulkInviteEmails()

Send the invites to emails in a batch.

Parameters

array $emails: Array containing emails.

string $nid: The node id.

array $context: The context.

Throws

\Drupal\Core\Entity\EntityStorageException

File

modules/social_features/social_event/modules/social_event_invite/src/SocialEventInviteBulkHelper.php, line 104

Class

SocialEventInviteBulkHelper
Class SocialEventBulkInvite.

Namespace

Drupal\social_event_invite

Code

public static function bulkInviteEmails(array $emails, $nid, array &$context) {
  $results = [];
  foreach ($emails as $email) {
    $user = user_load_by_mail($email);

    // Default values.
    $fields = [
      'field_event' => $nid,
      'field_enrollment_status' => '0',
      'field_request_or_invite_status' => EventEnrollmentInterface::INVITE_PENDING_REPLY,
    ];
    if ($user instanceof UserInterface) {

      // Add user information.
      $fields['user_id'] = $user
        ->id();
      $fields['field_account'] = $user
        ->id();

      // Clear the cache.
      $tags = [];
      $tags[] = 'enrollment:' . $nid . '-' . $user
        ->id();
      $tags[] = 'event_content_list:entity:' . $user
        ->id();
      Cache::invalidateTags($tags);
    }
    else {

      // Add email address.
      $fields['field_email'] = $email;
    }

    // Create a new enrollment for the event.
    $enrollment = EventEnrollment::create($fields);

    // In order for the notifications to be sent correctly we're updating the
    // owner here. The account is still linked to the actual enrollee.
    // The owner is always used as the actor.
    // @see activity_creator_message_insert().
    $enrollment
      ->setOwnerId(\Drupal::currentUser()
      ->id());

    // Add the node id to the results so we have the nid available in the
    // finished callback so we can redirect to the correct node.
    $results[$nid] = $enrollment
      ->save();
  }
  $context['results'] = $results;
}