Skip to content

Instantly share code, notes, and snippets.

@hannesl
Created September 6, 2012 08:27
Show Gist options
  • Select an option

  • Save hannesl/3653037 to your computer and use it in GitHub Desktop.

Select an option

Save hannesl/3653037 to your computer and use it in GitHub Desktop.
Check user Drupal role id for Features sanity
/**
* Implements hook_requirements().
*/
function mymodule_requirements($phase) {
$requirements = array();
// Ensure translations don't break at install time
$t = get_t();
if (defined('MYMODULE_RID')) {
// Test role ID, make sure it equals MYMODULE_RID.
$roles = user_roles();
$rid = array_search('member', $roles);
$requirements['member_role'] = array(
'title' => $t('Member role ID'),
'value' => $rid,
);
if ($rid != MYMODULE_RID) {
$requirements['member_role']['description'] = $t('The X role has an incorrect ID. It needs to be <code>!rid</code>.', array('!rid' => MYMODULE_RID));
$requirements['member_role']['severity'] = REQUIREMENT_ERROR;
}
}
return $requirements;
}
@hannesl
Copy link
Author

hannesl commented Sep 6, 2012

Various exportables use the rid when referring to a user role, but exported roles are defined by their name only. There are no guarantees that the rid will be the same on your dev, staging and production sites. This hook_requirements() implementation warns admins if the rid is incorrect.

MYMODULE_RID needs to be defined obviously, or you can hard-code it if you feel lazy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment