SettingsForm.php in Bulk User Registration 8
File
src/Form/SettingsForm.php
View source
<?php
namespace Drupal\bulk_user_registration\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\user\UserInterface;
class SettingsForm extends ConfigFormBase {
public function getFormId() {
return 'bulk_user_registration_settings';
}
protected function getEditableConfigNames() {
return [
'bulk_user_registration.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('bulk_user_registration.settings');
$form['allowed_roles'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Roles for import'),
'#default_value' => $config
->get('allowed_roles'),
'#options' => user_role_names(TRUE),
'#description' => $this
->t('These roles can be assigned with Bulk user registration. The authenticated role will always be set.'),
];
return parent::buildForm($form, $form_state);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$roles = $form_state
->getValue('allowed_roles');
$roles[UserInterface::AUTHENTICATED_ROLE] = UserInterface::AUTHENTICATED_ROLE;
$form_state
->setValue('allowed_roles', $roles);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$config = $this
->config('bulk_user_registration.settings');
$config
->set('allowed_roles', $values['allowed_roles'])
->save();
parent::submitForm($form, $form_state);
}
}