You are here

public function EnrollActionForm::submitForm in Open Social 8.6

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.7 modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm::submitForm()
  8. 8.8 modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm::submitForm()
  9. 10.3.x modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm::submitForm()
  10. 10.0.x modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm::submitForm()
  11. 10.1.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

2 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.

File

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

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');

  // 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();
    $form_state
      ->setRedirect('user.login', [], [
      'query' => [
        'destination' => $node_url,
      ],
    ]);

    // Check if user can register accounts.
    if ($this->configFactory
      ->get('user.settings')
      ->get('register') != USER_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,
      ]);
    }
    drupal_set_message($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_tag = 'enrollment:' . $nid . '-' . $uid;
  Cache::invalidateTags([
    $cache_tag,
  ]);
  if ($enrollment = array_pop($enrollments)) {
    $current_enrollment_status = $enrollment->field_enrollment_status->value;
    if ($to_enroll_status === '0' && $current_enrollment_status === '1') {
      $enrollment->field_enrollment_status->value = '0';
      $enrollment
        ->save();
    }
    elseif ($to_enroll_status === '1' && $current_enrollment_status === '0') {
      $enrollment->field_enrollment_status->value = '1';
      $enrollment
        ->save();
    }
  }
  else {

    // Create a new enrollment for the event.
    $enrollment = EventEnrollment::create([
      'user_id' => $uid,
      'field_event' => $nid,
      'field_enrollment_status' => '1',
      'field_account' => $uid,
    ]);
    $enrollment
      ->save();
  }
}