You are here

class EnrollRequestModalForm in Open Social 10.3.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_event/src/Form/EnrollRequestModalForm.php \Drupal\social_event\Form\EnrollRequestModalForm
  2. 10.0.x modules/social_features/social_event/src/Form/EnrollRequestModalForm.php \Drupal\social_event\Form\EnrollRequestModalForm
  3. 10.1.x modules/social_features/social_event/src/Form/EnrollRequestModalForm.php \Drupal\social_event\Form\EnrollRequestModalForm
  4. 10.2.x modules/social_features/social_event/src/Form/EnrollRequestModalForm.php \Drupal\social_event\Form\EnrollRequestModalForm

SendToDestinationsForm class.

Hierarchy

Expanded class hierarchy of EnrollRequestModalForm

File

modules/social_features/social_event/src/Form/EnrollRequestModalForm.php, line 16

Namespace

Drupal\social_event\Form
View source
class EnrollRequestModalForm extends FormBase {

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, $options = NULL) {
    $node = \Drupal::routeMatch()
      ->getParameter('node');
    $nid = $node
      ->id();
    $form['#prefix'] = '<div id="request_enrollment">';
    $form['#suffix'] = '</div>';

    // The status messages that will contain any form errors.
    $form['status_messages'] = [
      '#type' => 'status_messages',
      '#weight' => -10,
    ];
    $form['event'] = [
      '#type' => 'hidden',
      '#value' => $nid,
    ];
    $form['description'] = [
      '#type' => 'html_tag',
      '#tag' => 'p',
      '#value' => $this
        ->t('You can leave a message in your request. Only when your request is approved, you will receive a notification via email and notification center.'),
    ];
    $form['message'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Message'),
      '#maxlength' => 250,
    ];
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Send request'),
      '#button_type' => 'primary',
      '#ajax' => [
        'callback' => [
          $this,
          'submitModalFormAjax',
        ],
        'event' => 'click',
      ],
    ];
    $form['#attached']['library'] = [
      'core/drupal.dialog.ajax',
      'social_event/modal',
    ];
    $form['#attached']['drupalSettings']['eventEnrollmentRequest'] = [
      'closeDialog' => TRUE,
    ];
    return $form;
  }

  /**
   * AJAX callback handler that displays any errors or a success message.
   */
  public function submitModalFormAjax(array $form, FormStateInterface $form_state) {
    $response = new AjaxResponse();
    if ($form_state
      ->getErrors()) {

      // If there are errors, we can show the form again with the errors in
      // the status_messages section.
      $form['status_messages'] = [
        '#type' => 'status_messages',
        '#weight' => -10,
      ];
      $form_state
        ->setRebuild();
      return $response
        ->addCommand(new OpenModalDialogCommand($this
        ->t('Request to enroll'), $form, static::getDataDialogOptions()));
    }

    // Refactor this into a service or helper.
    $message = $form_state
      ->getValue('message');
    $current_user = \Drupal::currentUser();
    $uid = $current_user
      ->id();
    $nid = $form_state
      ->getValue('event');

    // Default event enrollment field set.
    $fields = [
      'user_id' => $uid,
      'field_event' => $nid,
      'field_enrollment_status' => '0',
      'field_account' => $uid,
      'field_request_or_invite_status' => EventEnrollmentInterface::REQUEST_PENDING,
      'field_request_message' => $message,
    ];

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

    // On success leave a message and reload the page.
    \Drupal::messenger()
      ->addStatus(t('Your request has been sent successfully'));
    return $response
      ->addCommand(new CloseDialogCommand());
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
  }

  /**
   * Gets the configuration names that will be editable.
   *
   * @return array
   *   An array of configuration object names that are editable if called in
   *   conjunction with the trait's config() method.
   */
  protected function getEditableConfigNames() {
    return [
      'config.request_enrollment_modal_form',
    ];
  }

  /**
   * Helper method so we can have consistent dialog options.
   *
   * @return string[]
   *   An array of jQuery UI elements to pass on to our dialog form.
   */
  protected static function getDataDialogOptions() {
    return [
      'dialogClass' => 'form--default social_event-popup',
      'closeOnEscape' => TRUE,
      'width' => '582',
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
EnrollRequestModalForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
EnrollRequestModalForm::getDataDialogOptions protected static function Helper method so we can have consistent dialog options.
EnrollRequestModalForm::getEditableConfigNames protected function Gets the configuration names that will be editable.
EnrollRequestModalForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
EnrollRequestModalForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
EnrollRequestModalForm::submitModalFormAjax public function AJAX callback handler that displays any errors or a success message.
EnrollRequestModalForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 3
FormBase::container private function Returns the service container.
FormBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 105
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.
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.
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. 27
MessengerTrait::messenger public function Gets the messenger. 27
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. 4
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.