Change record status: 
Project: 
Introduced in branch: 
8.x
Description: 

As a part of #1479454: Convert user roles to configurables the 'role' table has been removed from the database schema and user roles have been converted to configuration entities (configurables).

Summary

  • The 'role' table has been removed from the database schema.
  • User roles become classed objects a.k.a. Drupal\user\Plugin\Core\Entity\UserRole.
  • Core and Standard Installation Profile user roles are now defined in .yml files.
  • Instead of directly accessing user role values we now use the appropriate Entity API methods (e.g. $role->label() or $role->id())
  • The custom CRUD API for user roles has been removed in favor of a proper Entity API storage controller a.k.a. Drupal\user\Plugin\Core\Entity\UserRoleStorageController.
  • The old CRUD methods (user_role_save() and user_role_delete()) have been removed in favor of the Entity CRUD API. We will keep user_role_load() as a wrapper for entity_load('user_role', ...) for now.

API changes

Use the Entity API CRUD functions & methods in the future instead of user_role_load(), user_role_delete() or user_role_save().
Custom user roles previously defined in code through user_role_save() should be defined through .yml files in the future.
A new function user_role_names() has been added.

Examples

Defining a custom user role

Drupal 7

<?php
// Create a default role for site administrators, with all available permissions assigned.
$admin_role = new stdClass();
$admin_role->rid = 'administrator';
$admin_role->name = 'Administrator';
$admin_role->weight = 2;
user_role_save($admin_role);
?>

Drupal 8

Through a YAML file (@see core/profiles/standard/config/user.role.administrator.yml).

id: administrator
label: Administrator
weight: 2

User role CRUD operations

Drupal 7

<?php
// Load 'anonymous' user role.
$role = user_role_load(DRUPAL_ANONYMOUS_RID);
// Change the weight of a user role.
$role->weight = 30;
user_role_save($role);
// Delete a user role.
user_role_delete('administrator');
?>

Drupal 8

<?php
// Load 'anonymous' user role.
$role = entity_load('user_role', DRUPAL_ANONYMOUS_RID);
// Change the weight of a user role.
$role->weight = 30;
$role->save();
// Delete a user role that has already been loaded.
$role->delete();
// Delete a user role by name/ID.
entity_delete('user_role', 'administrator');
?>
Impacts: 
Site builders, administrators, editors
Module developers
Updates Done (doc team, etc.)
Online documentation: 
Not done
Theming guide: 
Not done
Module developer documentation: 
Not done
Examples project: 
Not done
Coder Review: 
Not done
Coder Upgrade: 
Not done
Other: 
Other updates done