You are here

public function EnrollActionForm::buildForm in Open Social 10.3.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::buildForm()
  2. 8 modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm::buildForm()
  3. 8.2 modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm::buildForm()
  4. 8.3 modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm::buildForm()
  5. 8.4 modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm::buildForm()
  6. 8.5 modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm::buildForm()
  7. 8.6 modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm::buildForm()
  8. 8.7 modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm::buildForm()
  9. 8.8 modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm::buildForm()
  10. 10.0.x modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm::buildForm()
  11. 10.1.x modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm::buildForm()
  12. 10.2.x modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm::buildForm()

Form constructor.

Parameters

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

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

Return value

array The form structure.

Overrides FormInterface::buildForm

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

File

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

Class

EnrollActionForm
Class EnrollActionForm.

Namespace

Drupal\social_event\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $nid = $this->routeMatch
    ->getRawParameter('node');
  $current_user = $this->currentUser;
  $uid = $current_user
    ->id();

  // We check if the node is placed in a Group I am a member of. If not,
  // we are not going to build anything.
  if (!is_null($nid)) {
    if (!is_object($nid)) {
      $node = $this->entityTypeManager
        ->getStorage('node')
        ->load($nid);
    }
    $groups = $this
      ->getGroups($node);

    // If the user is invited to an event
    // it shouldn't care about group permissions.
    $conditions = [
      'field_account' => $current_user
        ->id(),
      'field_event' => $node
        ->id(),
    ];
    $enrollments = $this->entityStorage
      ->loadByProperties($conditions);

    // Check if groups are not empty, or that the outsiders are able to join.
    if (!empty($groups) && $node->field_event_enroll_outside_group->value !== '1' && empty($enrollments) && social_event_manager_or_organizer() === FALSE) {
      $group_type_ids = $this->configFactory
        ->getEditable('social_event.settings')
        ->get('enroll');
      foreach ($groups as $group) {
        $group_type_id = $group
          ->bundle();

        // The join group permission has never really been used since
        // this commit. This now means that events in a closed group cannot
        // be joined by outsiders, which makes sense, since they also
        // couldn't see these events in the first place.
        if (in_array($group_type_id, $group_type_ids) && $group
          ->hasPermission('join group', $current_user)) {
          break;
        }
        if ($group
          ->hasPermission('enroll to events in groups', $current_user) == FALSE) {
          return [];
        }
      }
    }
  }
  $form['event'] = [
    '#type' => 'hidden',
    '#value' => $nid,
  ];
  $submit_text = $this
    ->t('Enroll');
  $to_enroll_status = '1';
  $enrollment_open = TRUE;
  $request_to_join = FALSE;
  $isNodeOwner = $node
    ->getOwnerId() === $uid;

  // Initialise the default attributes for the "Enroll" button
  // if the event enroll method is request to enroll, this will
  // be overwritten because of the modal.
  $attributes = [
    'class' => [
      'btn',
      'btn-accent brand-bg-accent',
      'btn-lg btn-raised',
      'dropdown-toggle',
      'waves-effect',
    ],
  ];

  // Add request to join event.
  if ((int) $node->field_enroll_method->value === EventEnrollmentInterface::ENROLL_METHOD_REQUEST && !$isNodeOwner) {
    $submit_text = $this
      ->t('Request to enroll');
    $to_enroll_status = '2';
    if ($current_user
      ->isAnonymous()) {
      $attributes = [
        'class' => [
          'use-ajax',
          'js-form-submit',
          'form-submit',
          'btn',
          'btn-accent',
          'btn-lg',
        ],
        'data-dialog-type' => 'modal',
        'data-dialog-options' => json_encode([
          'title' => t('Request to enroll'),
          'width' => 'auto',
        ]),
      ];
      $request_to_join = TRUE;
    }
  }

  // Add the enrollment closed label.
  if ($this
    ->eventHasBeenFinished($node)) {
    $submit_text = $this
      ->t('Event has passed');
    $enrollment_open = FALSE;
  }
  if (!$current_user
    ->isAnonymous()) {
    $conditions = [
      'field_account' => $uid,
      'field_event' => $nid,
    ];
    $enrollments = $this->entityStorage
      ->loadByProperties($conditions);
    if ($enrollment = array_pop($enrollments)) {
      $current_enrollment_status = $enrollment->field_enrollment_status->value;
      if ($current_enrollment_status === '1') {
        $submit_text = $this
          ->t('Enrolled');
        $to_enroll_status = '0';
      }
      elseif ($node->field_enroll_method->value && (int) $node->field_enroll_method->value === EventEnrollmentInterface::ENROLL_METHOD_REQUEST && !$isNodeOwner) {
        $event_request_ajax = TRUE;
        if ((int) $enrollment->field_request_or_invite_status->value === EventEnrollmentInterface::REQUEST_PENDING) {
          $submit_text = $this
            ->t('Pending');
          $event_request_ajax = FALSE;
        }
      }
    }

    // Use the ajax submit if the enrollments are empty, or if the
    // user cancelled his enrollment and tries again.
    if ($enrollment_open === TRUE) {
      if (!$isNodeOwner && (empty($enrollment) && $node->field_enroll_method->value && (int) $node->field_enroll_method->value === EventEnrollmentInterface::ENROLL_METHOD_REQUEST) || isset($event_request_ajax) && $event_request_ajax === TRUE) {
        $attributes = [
          'class' => [
            'use-ajax',
            'js-form-submit',
            'form-submit',
            'btn',
            'btn-accent',
            'btn-lg',
          ],
          'data-dialog-type' => 'modal',
          'data-dialog-options' => json_encode([
            'title' => t('Request to enroll'),
            'width' => 'auto',
          ]),
        ];
        $request_to_join = TRUE;
      }
    }
  }
  $form['to_enroll_status'] = [
    '#type' => 'hidden',
    '#value' => $to_enroll_status,
  ];
  $form['enroll_for_this_event'] = [
    '#type' => 'submit',
    '#value' => $submit_text,
    '#disabled' => !$enrollment_open,
    '#attributes' => $attributes,
  ];
  if ($request_to_join === TRUE) {
    $form['enroll_for_this_event'] = [
      '#type' => 'link',
      '#title' => $submit_text,
      '#url' => Url::fromRoute('social_event.request_enroll_dialog', [
        'node' => $nid,
      ]),
      '#attributes' => $attributes,
    ];
  }
  $form['#attributes']['name'] = 'enroll_action_form';
  if (isset($enrollment->field_enrollment_status->value) && $enrollment->field_enrollment_status->value === '1' || isset($enrollment->field_request_or_invite_status->value) && (int) $enrollment->field_request_or_invite_status->value === EventEnrollmentInterface::REQUEST_PENDING) {

    // Extra attributes needed for when a user is logged in. This will make
    // sure the button acts like a dropwdown.
    $form['enroll_for_this_event']['#attributes'] = [
      'class' => [
        'btn',
        'btn-accent brand-bg-accent',
        'btn-lg btn-raised',
        'dropdown-toggle',
        'waves-effect',
      ],
      'autocomplete' => 'off',
      'data-toggle' => 'dropdown',
      'aria-haspopup' => 'true',
      'aria-expanded' => 'false',
      'data-caret' => 'true',
    ];
    $cancel_text = $this
      ->t('Cancel enrollment');

    // Add markup for the button so it will be a dropdown.
    $form['feedback_user_has_enrolled'] = [
      '#markup' => '<ul class="dropdown-menu dropdown-menu-right"><li><a href="#" class="enroll-form-submit"> ' . $cancel_text . ' </a></li></ul>',
    ];
    $form['#attached']['library'][] = 'social_event/form_submit';
  }
  return $form;
}