EventSettingsForm.php in Open Social 8
File
modules/social_features/social_event/src/Form/EventSettingsForm.php
View source
<?php
namespace Drupal\social_event\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\group\Entity\GroupType;
class EventSettingsForm extends ConfigFormBase {
protected function getEditableConfigNames() {
return [
'social_event.settings',
];
}
public function getFormId() {
return 'event_settings_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$group_type_ids = $this->configFactory
->getEditable('social_event.settings')
->get('enroll');
$form['enroll'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Enroll user which is not group member'),
'#description' => $this
->t('Enroll button should be visible for users that are not in the group and automatic enroll people to groups when they enroll to events that are part of the group.'),
'#default_value' => $group_type_ids,
'#states' => [
'visible' => [
':input[name="enroll"]' => [
'checked' => TRUE,
],
],
],
];
foreach (GroupType::loadMultiple() as $group_type) {
if ($group_type
->hasContentPlugin('group_node:event')) {
$form['enroll']['#options'][$group_type
->id()] = $group_type
->label();
}
}
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$group_type_ids = [];
foreach ($form_state
->getValue('enroll') as $group_type_id => $enable) {
if ($enable) {
$group_type_ids[] = $group_type_id;
}
}
$this->configFactory
->getEditable('social_event.settings')
->set('enroll', $group_type_ids)
->save();
}
}