Skip to main content
Category:

Code snippet that can be used to show role options to users and hide administrator role in user registration form in Drupal 8.

<?php

/**
 * @param $form
 * @param $form_state
 * @param $form_id
 */
function mymodule_form_user_register_form_alter(&$form, &$form_state, $form_id)
{
  unset($form['account']['roles']['#options']['administrator']);
  $form['account']['roles']['#access'] = TRUE;
}

With this snippet:

users with the permission of 'administer users' can assign roles on the user registration form.

Hide the 'administrator' role for your users.

 

 

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook