class EnrollRequestModalForm in Open Social 10.3.x
Same name and namespace in other branches
- 8.9 modules/social_features/social_event/src/Form/EnrollRequestModalForm.php \Drupal\social_event\Form\EnrollRequestModalForm
- 10.0.x modules/social_features/social_event/src/Form/EnrollRequestModalForm.php \Drupal\social_event\Form\EnrollRequestModalForm
- 10.1.x modules/social_features/social_event/src/Form/EnrollRequestModalForm.php \Drupal\social_event\Form\EnrollRequestModalForm
- 10.2.x modules/social_features/social_event/src/Form/EnrollRequestModalForm.php \Drupal\social_event\Form\EnrollRequestModalForm
SendToDestinationsForm class.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\social_event\Form\EnrollRequestModalForm
Expanded class hierarchy of EnrollRequestModalForm
File
- modules/
social_features/ social_event/ src/ Form/ EnrollRequestModalForm.php, line 16
Namespace
Drupal\social_event\FormView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
EnrollRequestModalForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
EnrollRequestModalForm:: |
protected static | function | Helper method so we can have consistent dialog options. | |
EnrollRequestModalForm:: |
protected | function | Gets the configuration names that will be editable. | |
EnrollRequestModalForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
EnrollRequestModalForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
EnrollRequestModalForm:: |
public | function | AJAX callback handler that displays any errors or a success message. | |
EnrollRequestModalForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
FormBase:: |
protected | property | The config factory. | 3 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 3 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
105 |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function | Returns a redirect response object for the specified route. | |
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |