You are here

class EventAnEnrollForm in Open Social 8.6

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_event/modules/social_event_an_enroll/src/Form/EventAnEnrollForm.php \Drupal\social_event_an_enroll\Form\EventAnEnrollForm
  2. 8.3 modules/social_features/social_event/modules/social_event_an_enroll/src/Form/EventAnEnrollForm.php \Drupal\social_event_an_enroll\Form\EventAnEnrollForm
  3. 8.4 modules/social_features/social_event/modules/social_event_an_enroll/src/Form/EventAnEnrollForm.php \Drupal\social_event_an_enroll\Form\EventAnEnrollForm
  4. 8.5 modules/social_features/social_event/modules/social_event_an_enroll/src/Form/EventAnEnrollForm.php \Drupal\social_event_an_enroll\Form\EventAnEnrollForm
  5. 8.7 modules/social_features/social_event/modules/social_event_an_enroll/src/Form/EventAnEnrollForm.php \Drupal\social_event_an_enroll\Form\EventAnEnrollForm
  6. 8.8 modules/social_features/social_event/modules/social_event_an_enroll/src/Form/EventAnEnrollForm.php \Drupal\social_event_an_enroll\Form\EventAnEnrollForm
  7. 10.3.x modules/social_features/social_event/modules/social_event_an_enroll/src/Form/EventAnEnrollForm.php \Drupal\social_event_an_enroll\Form\EventAnEnrollForm
  8. 10.0.x modules/social_features/social_event/modules/social_event_an_enroll/src/Form/EventAnEnrollForm.php \Drupal\social_event_an_enroll\Form\EventAnEnrollForm
  9. 10.1.x modules/social_features/social_event/modules/social_event_an_enroll/src/Form/EventAnEnrollForm.php \Drupal\social_event_an_enroll\Form\EventAnEnrollForm
  10. 10.2.x modules/social_features/social_event/modules/social_event_an_enroll/src/Form/EventAnEnrollForm.php \Drupal\social_event_an_enroll\Form\EventAnEnrollForm

Class EventAnEnrollForm.

@package Drupal\social_event_an_enroll\Form

Hierarchy

Expanded class hierarchy of EventAnEnrollForm

1 string reference to 'EventAnEnrollForm'
social_event_an_enroll.routing.yml in modules/social_features/social_event/modules/social_event_an_enroll/social_event_an_enroll.routing.yml
modules/social_features/social_event/modules/social_event_an_enroll/social_event_an_enroll.routing.yml

File

modules/social_features/social_event/modules/social_event_an_enroll/src/Form/EventAnEnrollForm.php, line 19

Namespace

Drupal\social_event_an_enroll\Form
View source
class EventAnEnrollForm extends EnrollActionForm {

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $nid = $this->routeMatch
      ->getRawParameter('node');

    // Load node object.
    if (!is_null($nid) && !is_object($nid)) {
      $node = Node::load($nid);
    }

    // Set hidden values.
    $form['field_event'] = [
      '#type' => 'hidden',
      '#value' => $nid,
    ];
    $form['field_first_name'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('First name'),
    ];
    $form['field_last_name'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Last name'),
    ];
    $form['field_email'] = [
      '#type' => 'email',
      '#required' => TRUE,
      '#title' => $this
        ->t('Email address'),
      '#description' => $this
        ->t('Enter your email, so we can send you event updates.'),
    ];
    if ($this->moduleHandler
      ->moduleExists('data_policy')) {
      $data_policy_config = $this->configFactory
        ->get('data_policy.data_policy');

      // Check if data policy is created.
      if (!empty($data_policy_config
        ->get('entity_id'))) {
        $form['#attached']['library'][] = 'core/drupal.dialog.ajax';
        $link = Link::createFromRoute($this
          ->t('data policy'), 'data_policy.data_policy', [], [
          'attributes' => [
            'class' => [
              'use-ajax',
            ],
            'data-dialog-type' => 'modal',
            'data-dialog-options' => Json::encode([
              'title' => t('Data policy'),
              'width' => 700,
              'height' => 700,
            ]),
          ],
        ]);
        $enforce_consent = !empty($data_policy_config
          ->get('enforce_consent'));
        $form['data_policy'] = [
          '#type' => 'checkbox',
          '#title' => $this
            ->t('I agree with the @url', [
            '@url' => $link
              ->toString(),
          ]),
          '#required' => $enforce_consent,
        ];
      }
    }
    $submit_text = $this
      ->t('Enroll in event');
    $enrollment_open = TRUE;

    // Add the enrollment closed label.
    if ($this
      ->eventHasBeenFinished($node)) {
      $submit_text = $this
        ->t('Event has passed');
      $enrollment_open = FALSE;
    }
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#button_type' => 'primary',
      '#button_level' => 'raised',
      '#value' => $submit_text,
      '#disabled' => !$enrollment_open,
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $current_user = $this->currentUser;
    $uid = $current_user
      ->id();
    if ($current_user
      ->isAnonymous()) {
      $nid = $form_state
        ->getValue('field_event');
      $values = $form_state
        ->getValues();
      $values['user_id'] = $uid;
      $values['field_account'] = $uid;
      $values['field_enrollment_status'] = '1';

      // Check if there is enrollment with the same email.
      $conditions = [
        'field_email' => $values['field_email'],
        'field_event' => $nid,
      ];
      $enrollments = $this->entityStorage
        ->loadByProperties($conditions);
      if ($enrollment = array_pop($enrollments)) {
        $values['field_token'] = $enrollment
          ->get('field_token')
          ->getString();
        drupal_set_message($this
          ->t('You have been already enrolled to this event. You have also received a notification via email.'));
      }
      else {
        $values['field_token'] = Crypt::randomBytesBase64();

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

        // 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);
        drupal_set_message($this
          ->t('You have successfully enrolled to this event. You have also received a notification via email.'));
      }

      // Redirect anonymous use to login page before enrolling to an event.
      $form_state
        ->setRedirect('entity.node.canonical', [
        'node' => $nid,
      ], [
        'query' => [
          'token' => $values['field_token'],
        ],
      ]);

      // Send email if the setting is enabled.
      $event_an_enroll_config = $this
        ->config('social_event_an_enroll.settings');
      if ($event_an_enroll_config
        ->get('event_an_enroll_email_notify')) {
        social_event_an_enroll_send_mail($values);
      }
    }
  }

}

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.
EventAnEnrollForm::buildForm public function Form constructor. Overrides EnrollActionForm::buildForm
EventAnEnrollForm::getFormId public function Returns a unique string identifying the form. Overrides EnrollActionForm::getFormId
EventAnEnrollForm::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.