CluesShop.com

Tuesday, 28 October 2014

get role id from role name in drupal 7?

This is quite straightforward with user_roles() and array_search(). Below is a function which will return the role ID if there is a role matching the name and FALSE otherwise.
function get_role_by_name($name) {
  $roles = user_roles();
  return array_search($name, $roles);
}

// Sample usage
$rid = get_role_by_name('administrator');
One liner would be:
$rid = array_search('administrator', user_roles());

No comments:

Post a Comment