function _single_user_role_edit_element in Single User Role 8
Helper function that alters the role form element.
Parameters
$element: The form element that should be altered.
2 calls to _single_user_role_edit_element()
- single_user_role_form_alter in ./
single_user_role.module - Implements hook_form_alter().
- single_user_role_form_role_delegation_role_assign_form_alter in ./
single_user_role.module - Implements hook_form_ID_alter() for 'role_delegation_role_assign_form'.
File
- ./
single_user_role.module, line 79 - Single User Role module file.
Code
function _single_user_role_edit_element(&$element) {
// Change role field type from checkbox to select or radio field.
$element['#type'] = \Drupal::config('single_user_role.settings')
->get('single_user_role_field_type');
$element['#multiple'] = FALSE;
// Set role field help text.
$element['#description'] = \Drupal::config('single_user_role.settings')
->get('single_user_role_field_desc');
// Set role field title singular.
$element['#title'] = t('Role');
// Enable authenticated role option.
$element['authenticated']['#disabled'] = FALSE;
// Fix default value for authenticated role.
unset($element['authenticated']['#default_value']);
// Always have authenticated role in options as radios cannot be unselected.
if (!isset($element['#options'][RoleInterface::AUTHENTICATED_ID])) {
$authenticated_role = [
RoleInterface::AUTHENTICATED_ID => t('Authenticated user'),
];
$element['#options'] = array_merge($authenticated_role, $element['#options']);
}
// Set default role. Authenticated role is assigned if no other role found.
$default_role = 'authenticated';
foreach ($element['#default_value'] as $key => $value) {
if ($key !== 0) {
// Set first assigned role as default, excluding authenticated.
$default_role = $value;
break;
}
}
$element['#default_value'] = $default_role;
}