You are here

public function EventInviteEnrollActionForm::buildForm in Open Social 8.9

Same name and namespace in other branches
  1. 10.3.x modules/social_features/social_event/modules/social_event_invite/src/Form/EventInviteEnrollActionForm.php \Drupal\social_event_invite\Form\EventInviteEnrollActionForm::buildForm()
  2. 10.0.x modules/social_features/social_event/modules/social_event_invite/src/Form/EventInviteEnrollActionForm.php \Drupal\social_event_invite\Form\EventInviteEnrollActionForm::buildForm()
  3. 10.1.x modules/social_features/social_event/modules/social_event_invite/src/Form/EventInviteEnrollActionForm.php \Drupal\social_event_invite\Form\EventInviteEnrollActionForm::buildForm()
  4. 10.2.x modules/social_features/social_event/modules/social_event_invite/src/Form/EventInviteEnrollActionForm.php \Drupal\social_event_invite\Form\EventInviteEnrollActionForm::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 EnrollActionForm::buildForm

File

modules/social_features/social_event/modules/social_event_invite/src/Form/EventInviteEnrollActionForm.php, line 28

Class

EventInviteEnrollActionForm
Class EventInviteEnrollActionForm.

Namespace

Drupal\social_event_invite\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, Node $node = NULL) {
  $form = parent::buildForm($form, $form_state);
  $nid = $this->routeMatch
    ->getRawParameter('node');
  $current_user = $this->currentUser;
  $uid = $current_user
    ->id();
  if (!$current_user
    ->isAnonymous()) {
    $conditions = [
      'field_account' => $uid,
      'field_event' => $nid,
    ];
    $enrollments = $this->entityStorage
      ->loadByProperties($conditions);

    // If the event is invite only and you have not been invited, return.
    // Unless you are the node owner or organizer.
    if (empty($enrollments)) {
      if ((int) $node->field_enroll_method->value === EventEnrollmentInterface::ENROLL_METHOD_INVITE && social_event_manager_or_organizer() === FALSE) {
        return [];
      }
    }
    elseif ($enrollment = array_pop($enrollments)) {
      $enroll_request_status = $enrollment->field_request_or_invite_status->value;

      // If user got invited perform actions.
      if ($enroll_request_status == '4') {
        $submit_text = $this
          ->t('Accept');
        $form['enroll_for_this_event'] = [
          '#type' => 'submit',
          '#value' => $submit_text,
          '#name' => 'accept_invite',
        ];

        // Extra attributes needed for when a user is logged in.
        // This will make sure the button acts like a dropdown.
        $form['enroll_for_this_event']['#attributes'] = [
          'class' => [
            'btn',
            'btn-accent brand-bg-accent',
            'btn-lg btn-raised',
            'dropdown-toggle',
            'waves-effect',
          ],
        ];

        // We need a hidden element for later usage.
        $form['event_id'] = [
          '#type' => 'hidden',
          '#value' => $this->routeMatch
            ->getRawParameter('node'),
        ];
        $form['decline_invite'] = [
          '#type' => 'submit',
          '#value' => '',
          '#name' => 'decline_invite',
        ];

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

        // Add markup for the button so it will be a dropdown.
        $form['decline_invite_dropdown'] = [
          '#markup' => '<ul class="dropdown-menu dropdown-menu-right"><li><a href="#" class="enroll-form-submit"> ' . $decline_text . ' </a></li></ul>',
        ];

        // Add a hidden operation we can fill with jquery when declining.
        $form['operation'] = [
          '#type' => 'hidden',
          '#default_value' => '',
        ];
        $form['#attached']['library'][] = 'social_event/form_submit';
      }
    }
  }

  // For AN users it can be rendered on a Public event with
  // invite only as option. Let's make it similar to a Group experience
  // where there is no button rendered.
  // We unset it here because in the parent form and this form
  // a lot of times this button get's overridden.
  if ($current_user
    ->isAnonymous()) {
    unset($form['enroll_for_this_event']);
  }
  return $form;
}