1

I am trying to set several permissions when a feature is being enabled (a new content type). By default this content type is disabled, and an admin can go to structure/features and enable it

under docroot/profile/my_profile/features/foo I created the following foo.install file

function foo_modules_installed($modules) {
 if (in_array('foo', $modules)) {
  $perms = array(
    'create foo content',
    'edit own foo content',
    ); 
      user_role_grant_permissions(user_role_load_by_name('publisher')->rid, $perms);
      user_role_grant_permissions(user_role_load_by_name('administrator')->rid, $perms);
}
}

but when I go to structure/features and enable it, I get the following error

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'module' cannot be null: INSERT INTO {role_permission} (rid, permission, module

I tried different hooks, like enabled and so forth and all have the same issue.

How can I have a feature disabled by default, but still grant permissions to some users? What am I doing wrong

  • which one? Cause someone claims he used it in hook_install and it worked – Quantico Dec 12 '16 at 14:52
  • I vaguely remember this being a Features bug. I would check the issue queue for Features and search for this. It is hard to track down... if I recall, it seems like you're trying to add permissions for a module dependency that is not yet enabled. At the same time, you cannot have a module be disabled, but still execute code (especially install hook). – Kevin Dec 12 '16 at 15:07
  • @Kevin, I am trying to have a feature that is disabled by default, and that will have permissions after it was enabled. I doubt I am the first person in the world, who needs a module to not be enabled by default, but still get permissions at some point during the process – Quantico Dec 12 '16 at 15:23
  • There is some history here: drupal.org/node/1063204 - looks like for me, setting permissions in hook_enable worked over hook_install. YMMV.. probably worth digging through Features queue. – Kevin Dec 12 '16 at 15:27
0

Seems like a hook_install() in the .install file for the feature will work if called with

drupal_flush_all_caches();

I am not sure if this is the best solution, but it seems to work for me

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.