Drupal 8 user registration form: show role options and hide administrator rol

By default, users with the permission of 'administer users' cannot assign roles on the user registration form. With this snippet you can, but make sure to hide the 'administrator'-role for your users.

/*
 * Allow adding roles in user registration form
 */
function MYMODULE_form_user_register_form_alter(&$form, &$form_state, $form_id) {
    unset($form['account']['roles']['#options']['administrator']);
    $form['account']['roles']['#access'] = TRUE;
}

 

 

Saved you some valuable time?

Buy me a drink 🍺 to keep me motivated to create free content like this!