1

I would like to create multiple roles with the same permissions in my install profile. To add one user in your install profile, it's quite simple. I use this snippet:

    // Make the new role
    $role = new stdClass;
    $role->name = 'User 1';
    $role->weight = 3;
    user_role_save($role);

    // Permissions to assign to the role.
    $perms = array(
    'access content',
    'access content overview',
    );

    // Grant the permissions. This function takes care of all necessary cache resets
    user_role_grant_permissions($role->rid, $perms);

I tried with an array, but it didn't work out. Drupal just ignored the array.

The Array didn't work out...:

    // Make the new role
    $role = new stdClass;
    $roles = array('Eindredacteur 1', 'Eindredacteur 2');
    $role->name = $roles;
    $role->weight = 3;
    user_role_save($role);

    // Permissions to assign to the role.
    $perms = array(
    'access content',
    );

    // Grant the permissions. This function takes care of all necessary cache resets
    user_role_grant_permissions($role->rid, $perms);

Anyone who can give me some advice?

2

This should work:

// Make the first role
$role = new stdClass;
$role->name = 'Eindredacteur 1';
$role->weight = 3;
user_role_save($role);
$roleid_1 = $role->rid;

// Make the second role
$role = new stdClass;
$role->name = 'Eindredacteur 2';
$role->weight = 3;
user_role_save($role);
$roleid_2 = $role->rid;

// Permissions to assign to the role.
$perms = array(
'access content',
);

// Grant the permissions. This function takes care of all necessary cache resets
user_role_grant_permissions($roleid_1, $perms);    
user_role_grant_permissions($roleid_2, $perms);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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