You are here

function social_event_node_update in Open Social 10.0.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_event/social_event.module \social_event_node_update()
  2. 10.3.x modules/social_features/social_event/social_event.module \social_event_node_update()
  3. 10.1.x modules/social_features/social_event/social_event.module \social_event_node_update()
  4. 10.2.x modules/social_features/social_event/social_event.module \social_event_node_update()

Implements hook_ENTITY_TYPE_update().

Delete the event enrollments request if type is changed to directly.

File

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

Code

function social_event_node_update(EntityInterface $entity) {
  if ($entity instanceof NodeInterface && $entity
    ->getType() === 'event') {
    $enroll_state_original = $entity->original
      ->get('field_enroll_method')
      ->getString();
    $enroll_new_state = $entity
      ->get('field_enroll_method')
      ->getString();

    // If the request to enroll was turned off then delete the request.
    if ((int) $enroll_state_original === EventEnrollmentInterface::ENROLL_METHOD_REQUEST && (int) $enroll_new_state !== EventEnrollmentInterface::ENROLL_METHOD_REQUEST) {

      /** @var \Drupal\social_event\EventEnrollmentStatusHelper $enrollments */
      $enrollments = \Drupal::service('social_event.status_helper');

      /** @var \Drupal\social_event\Entity\EventEnrollment $enrollment */
      foreach ($enrollments
        ->getAllEventEnrollments($entity
        ->id(), TRUE) as $enrollment) {

        // Check if the field is not empty before getting the value,
        // Since (int) will otherwise return 0 and give false positives.
        if (!$enrollment
          ->get('field_request_or_invite_status')
          ->isEmpty() && (int) $enrollment
          ->get('field_request_or_invite_status')
          ->getString() === EventEnrollmentInterface::REQUEST_PENDING) {
          $enrollment
            ->delete();
        }
      }
    }
  }
}