Apr 24, 2017 in Drupal 8, Snippets

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;
}