You are here

class EventInviteEnrollActionForm 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
  2. 10.0.x modules/social_features/social_event/modules/social_event_invite/src/Form/EventInviteEnrollActionForm.php \Drupal\social_event_invite\Form\EventInviteEnrollActionForm
  3. 10.1.x modules/social_features/social_event/modules/social_event_invite/src/Form/EventInviteEnrollActionForm.php \Drupal\social_event_invite\Form\EventInviteEnrollActionForm
  4. 10.2.x modules/social_features/social_event/modules/social_event_invite/src/Form/EventInviteEnrollActionForm.php \Drupal\social_event_invite\Form\EventInviteEnrollActionForm

Class EventInviteEnrollActionForm.

@package Drupal\social_event_invite\Form

Hierarchy

Expanded class hierarchy of EventInviteEnrollActionForm

File

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

Namespace

Drupal\social_event_invite\Form
View source
class EventInviteEnrollActionForm extends EnrollActionForm {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'event_invite_enroll_action_form';
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
    $operation = $form_state
      ->getValue('operation');
    $current_user = $this->currentUser;
    $uid = $current_user
      ->id();
    $nid = $form_state
      ->getValue('event') ?? $this->routeMatch
      ->getRawParameter('node');
    $conditions = [
      'field_account' => $uid,
      'field_event' => $nid,
    ];
    $enrollments = $this->entityStorage
      ->loadByProperties($conditions);

    // @todo: also clear the breadcrumb cachetags.
    // Invalidate cache for our enrollment cache tag in
    // social_event_node_view_alter().
    $tags = [];
    $tags[] = 'enrollment:' . $nid . '-' . $uid;
    $tags[] = 'event_content_list:entity:' . $uid;
    Cache::invalidateTags($tags);
    if ($enrollment = array_pop($enrollments)) {

      // Only trigger when the user is invited.
      if ($enrollment->field_request_or_invite_status && (int) $enrollment->field_request_or_invite_status->value === EventEnrollmentInterface::INVITE_PENDING_REPLY) {

        // Delete any messages since it would show a 'successful enrollment'.
        $this
          ->messenger()
          ->deleteAll();

        // Accept the invite.
        $enrollment->field_enrollment_status->value = '1';
        $enrollment->field_request_or_invite_status->value = EventEnrollmentInterface::INVITE_ACCEPTED_AND_JOINED;

        // If decline is chosen, set invite to declined.
        if ($operation === 'decline') {

          // Delete any messages since it would show a 'successful enrollment'.
          $this
            ->messenger()
            ->deleteAll();
          $enrollment->field_enrollment_status->value = '0';
          $enrollment->field_request_or_invite_status->value = EventEnrollmentInterface::REQUEST_OR_INVITE_DECLINED;
        }
        $enrollment
          ->save();
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EnrollActionForm::$configFactory protected property The config factory. Overrides FormBase::$configFactory
EnrollActionForm::$currentUser protected property The current user.
EnrollActionForm::$entityStorage protected property The node storage for event enrollments.
EnrollActionForm::$entityTypeManager protected property The entity type manager.
EnrollActionForm::$moduleHandler protected property The module handler.
EnrollActionForm::$routeMatch protected property The routing matcher to get the nid. Overrides FormBase::$routeMatch
EnrollActionForm::$userStorage protected property The user storage.
EnrollActionForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
EnrollActionForm::eventHasBeenFinished protected function Function to determine if an event has been finished.
EnrollActionForm::getGroups public function Get group object where event enrollment is posted in.
EnrollActionForm::__construct public function Constructs an Enroll Action Form.
EventInviteEnrollActionForm::buildForm public function Form constructor. Overrides EnrollActionForm::buildForm
EventInviteEnrollActionForm::getFormId public function Returns a unique string identifying the form. Overrides EnrollActionForm::getFormId
EventInviteEnrollActionForm::submitForm public function Form submission handler. Overrides EnrollActionForm::submitForm
FormBase::$requestStack protected property The request stack. 1
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.