function single_user_role_form_validate in Single User Role 8
Same name and namespace in other branches
- 7 single_user_role.module \single_user_role_form_validate()
Validation callback for single_user_role_form_alter().
Transforms the roles value to be wrapped in an array that drupal core expects: eg: `['role_name' => 'role_name']`
Parameters
array $form: User form array.
\Drupal\Core\Form\FormStateInterface $form_state: User form state.
1 string reference to 'single_user_role_form_validate'
- single_user_role_form_alter in ./
single_user_role.module - Implements hook_form_alter().
File
- ./
single_user_role.module, line 51 - Single User Role module file.
Code
function single_user_role_form_validate(array &$form, FormStateInterface &$form_state) {
// Get the value of the selected role.
$role = $form_state
->getValue('roles');
// Transform the roles value to be wrapped in an array.
if ($role && !is_array($role)) {
$form_state
->setValue('roles', array_combine([
$role,
], [
$role,
]));
}
$original_role = $form['account']['roles']['#default_value'];
if ($original_role && !is_array($original_role)) {
$form['account']['roles']['#default_value'] = [
$original_role,
];
}
}