In my module I'm creating new permissions that I want by default also assigned to the Drupal admin user.

I know how to assign new permissions to a role, but I don't know how to get the Drupal administrator role id. In the database the rid for administrator is 3, but can I rely on that being always the case? I'm using Drupal 7.

  • If I am not mistaken, new modules always apply permissions to the administrator role defined on admin/config/people/accounts. But only if you install the module. So disabling, uninstalling and installing should grant the the permissions to the admin user as well. (Or if someone else installs the module fresh.) – Neograph734 Dec 8 '16 at 20:40
up vote 3 down vote accepted

unless you install Drupal in your custom installation profile and modify the administrator role there, the rid or administrator will be always 3.
Or you could just do a db_query() on role table to return the rid of administrator as:

$result = db_query("SELECT rid FROM {role} where name like :id",array(':id' => 'administrator'));
$admin_rid = $result->fetchField(0);

Consider that the administrator role may have a different name than administrator or may have a different role ID.

Drupal 7 has a configuration available at Account Settings page: Administrator role

Administrator role

The value is stored in variable named user_admin_role.

$admin_rid = variable_get('user_admin_role');

After getting the role ID, you can do pretty much anything you need with it.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.