Role IDs (rid) have been converted from integers to (varchar) strings. They denote the machine name of a user role.
The built-in default roles for "anonymous user" and "authenticated user" use the machine names 'anonymous'
and 'authenticated'
. The DRUPAL_ANONYMOUS_RID
and DRUPAL_AUTHENTICATED_RID
constants are still available, but are deprecated, and you should use one of Drupal\Core\Session\AccountInterface::ANONYMOUS_ROLE
, Drupal\user\RoleInterface::ANONYMOUS_ID
, Drupal\Core\Session\AccountInterface::AUTHENTICATED_ROLE
, or Drupal\user\RoleInterface::AUTHENTICATED_ID
instead.
Each role also has a human-readable label now, for which proper capitalization may be used (e.g., "Anonymous user" instead of "anonymous user"). The label of built-in roles can be changed.
The 'roles' property of user account objects no longer uses numeric role IDs as array keys but machine name strings instead. The human-readable role name (label) is the array value.
Drupal 7
$user->roles = array(
2 => 'authenticated user',
3 => 'administrator',
4 => 'site editor',
);
Drupal 8
$user->roles = array(
'authenticated' => 'Authenticated user',
'administrator' => 'Administrator',
'site_editor' => 'Site editor',
);
user_role_load_by_name()
was removed, since the role ID is the machine-readable name now. Use user_role_load()
instead.
Previously existing custom role IDs (e.g., 5) are upgraded as-is into strings (i.e., '5').
All database tables referencing role IDs need to be converted into varchar strings, which should be performed after User module converted the primary role tables:
'rid' => array(
- 'type' => 'int',
- 'unsigned' => TRUE,
+ 'type' => 'varchar',
+ 'length' => 64,
'not null' => TRUE,
'description' => "The user's role ID from {roles}.rid.",
),
/**
* Implements hook_update_dependencies().
*/
function block_update_dependencies() {
// Convert role IDs after User module converted {role}.
$dependencies['block'][8002] = array(
'user' => 8002,
);
return $dependencies;
}
/**
* Replace serial role IDs with machine name strings.
*
* @see user_update_8002()
*/
function block_update_8002() {
// Change serial rid column into string.
$column = array(
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'description' => "The user's role ID from {users_roles}.rid.",
);
db_change_field('block_role', 'rid', 'rid', $column);
// Rename the built-in serial role IDs into the hardcoded machine names.
db_update('block_role')
->fields(array('rid' => DRUPAL_ANONYMOUS_RID))
->condition('rid', 1)
->execute();
db_update('block_role')
->fields(array('rid' => DRUPAL_AUTHENTICATED_RID))
->condition('rid', 2)
->execute();
}
If you are getting errors to the effect of "General error: 1364 Field 'rid' doesn't have a default value: INSERT INTO {role} (rid, name, weight) VALUES (default, :db_insert_placeholder_0, :db_insert_placeholder_1); Array ( [:db_insert_placeholder_0] => administrator [:db_insert_placeholder_1] => 2 )" you are probably doing something like this:
$admin_role = new stdClass();
$admin_role->name = 'administrator';
$admin_role->weight = 2;
user_role_save($admin_role);
You need to specify a rid like so:
$admin_role = new stdClass();
// New rid property.
$admin_role->rid = 'administrator';
$admin_role->name = 'Administrator';
$admin_role->weight = 2;
user_role_save($admin_role);