You are here

public function EnrollActionForm::submitForm in Open Social 10.1.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm::submitForm()
  2. 8 modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm::submitForm()
  3. 8.2 modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm::submitForm()
  4. 8.3 modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm::submitForm()
  5. 8.4 modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm::submitForm()
  6. 8.5 modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm::submitForm()
  7. 8.6 modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm::submitForm()
  8. 8.7 modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm::submitForm()
  9. 8.8 modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm::submitForm()
  10. 10.3.x modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm::submitForm()
  11. 10.0.x modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm::submitForm()
  12. 10.2.x modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm::submitForm()

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

1 call to EnrollActionForm::submitForm()
EventInviteEnrollActionForm::submitForm in modules/social_features/social_event/modules/social_event_invite/src/Form/EventInviteEnrollActionForm.php
Form submission handler.
3 methods override EnrollActionForm::submitForm()
EventAnEnrollActionForm::submitForm in modules/social_features/social_event/modules/social_event_an_enroll/src/Form/EventAnEnrollActionForm.php
Form submission handler.
EventAnEnrollForm::submitForm in modules/social_features/social_event/modules/social_event_an_enroll/src/Form/EventAnEnrollForm.php
Form submission handler.
EventInviteEnrollActionForm::submitForm in modules/social_features/social_event/modules/social_event_invite/src/Form/EventInviteEnrollActionForm.php
Form submission handler.

File

modules/social_features/social_event/src/Form/EnrollActionForm.php, line 377

Class

EnrollActionForm
Class EnrollActionForm.

Namespace

Drupal\social_event\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $current_user = $this->currentUser;
  $uid = $current_user
    ->id();
  $nid = $form_state
    ->getValue('event') ?? $this->routeMatch
    ->getRawParameter('node');
  $node = $this->entityTypeManager
    ->getStorage('node')
    ->load($nid);

  // Redirect anonymous use to login page before enrolling to an event.
  if ($current_user
    ->isAnonymous()) {
    $node_url = Url::fromRoute('entity.node.canonical', [
      'node' => $nid,
    ])
      ->toString();
    $destination = $node_url;

    // If the request enroll method is set, alter the destination for AN.
    if ((int) $node
      ->get('field_enroll_method')->value === EventEnrollmentInterface::ENROLL_METHOD_REQUEST) {
      $destination = $node_url . '?requested-enrollment=TRUE';
    }
    $form_state
      ->setRedirect('user.login', [], [
      'query' => [
        'destination' => $destination,
      ],
    ]);

    // Check if user can register accounts.
    if ($this->configFactory
      ->get('user.settings')
      ->get('register') !== UserInterface::REGISTER_ADMINISTRATORS_ONLY) {
      $log_in_url = Url::fromUserInput('/user/login');
      $log_in_link = Link::fromTextAndUrl($this
        ->t('log in'), $log_in_url)
        ->toString();
      $create_account_url = Url::fromUserInput('/user/register');
      $create_account_link = Link::fromTextAndUrl($this
        ->t('create a new account'), $create_account_url)
        ->toString();
      $message = $this
        ->t('Please @log_in or @create_account_link so that you can enroll to the event.', [
        '@log_in' => $log_in_link,
        '@create_account_link' => $create_account_link,
      ]);
    }
    else {
      $log_in_url = Url::fromUserInput('/user/login');
      $log_in_link = Link::fromTextAndUrl($this
        ->t('log in'), $log_in_url)
        ->toString();
      $message = $this
        ->t('Please @log_in so that you can enroll to the event.', [
        '@log_in' => $log_in_link,
      ]);
    }
    $this
      ->messenger()
      ->addStatus($message);
    return;
  }
  $to_enroll_status = $form_state
    ->getValue('to_enroll_status');
  $conditions = [
    'field_account' => $uid,
    'field_event' => $nid,
  ];
  $enrollments = $this->entityStorage
    ->loadByProperties($conditions);

  // Invalidate cache for our enrollment cache tag in
  // social_event_node_view_alter().
  $cache_tags[] = 'enrollment:' . $nid . '-' . $uid;
  $cache_tags[] = 'node:' . $nid;
  Cache::invalidateTags($cache_tags);
  if ($enrollment = array_pop($enrollments)) {
    $current_enrollment_status = $enrollment->field_enrollment_status->value;

    // The user is enrolled, but cancels his enrollment.
    if ($to_enroll_status === '0' && $current_enrollment_status === '1') {

      // The user is enrolled by invited or request, but either the user or
      // event manager is declining or invalidating the enrollment.
      if ($enrollment->field_request_or_invite_status && (int) $enrollment->field_request_or_invite_status->value === EventEnrollmentInterface::INVITE_ACCEPTED_AND_JOINED) {

        // Mark this user his enrollment as declined.
        $enrollment->field_request_or_invite_status->value = EventEnrollmentInterface::REQUEST_OR_INVITE_DECLINED;

        // If the user is cancelling, un-enroll.
        $current_enrollment_status = $enrollment->field_enrollment_status->value;
        if ($current_enrollment_status === '1') {
          $enrollment->field_enrollment_status->value = '0';
        }
        $enrollment
          ->save();
      }
      else {
        $enrollment
          ->delete();
      }
    }
    elseif ($to_enroll_status === '1' && $current_enrollment_status === '0') {
      $enrollment->field_enrollment_status->value = '1';
      $enrollment
        ->save();
    }
    elseif ($to_enroll_status === '2' && $current_enrollment_status === '0') {
      if ((int) $enrollment->field_request_or_invite_status->value === EventEnrollmentInterface::REQUEST_PENDING) {
        $enrollment
          ->delete();
      }
    }
  }
  else {

    // Default event enrollment field set.
    $fields = [
      'user_id' => $uid,
      'field_event' => $nid,
      'field_enrollment_status' => '1',
      'field_account' => $uid,
    ];

    // If request to join is on, alter fields.
    if ($to_enroll_status === '2') {
      $fields['field_enrollment_status'] = '0';
      $fields['field_request_or_invite_status'] = EventEnrollmentInterface::REQUEST_PENDING;
    }

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