0

I have the Developer user role which should only access the developer_html input format. I am trying to grant the necessary permission with user_role_grant_permissions().

I got this piece of code here and also tried to edit according to my requirement, but failed.

function filter_update_7008() {
  // Build the list of permissions to grant.
  $permissions = array();
  foreach (filter_formats() as $format_id => $format) {
    if ($permission = filter_permission_name($format)) {
      $permissions[] = $permission;
    }
  }

  // Grant text format permissions to all roles that can 'administer filters'.
  // Albeit anonymous users *should not* have the permission, we cannot presume
  // that they do not or must not.
  if ($roles = user_roles(FALSE, 'administer filters')) {
    foreach ($roles as $rid => $name) {
      user_role_grant_permissions($rid, $permissions);
    }
  }
}
1

The general solution for cases like this, where you want to export an input format, a role, and a permission associated to that role is using the Features module. Selecting what you want to export on admin/structure/features/create, the module will create a feature: a module that (once installed) will make available what exported.

screenshot

If you don't want to use the Features module, you can create a module that:

  • Creates the Developer role if it doesn't exist
  • Creates the input format if it doesn't exist
  • Creates the permission to use that input format if it doesn't exists
  • Assigns the permission to use that input format to the Developer role

Assuming that the role and the input format already exist in the site, the code is simply the following.

if ($format = filter_format_load('developer_html')) {
  if ($permission = filter_permission_name($format)) {
    if ($role = user_role_load_by_name('developer')) {
      user_role_grant_permissions($role->rid, $permission);
    }
  }
}

As to where to use this code, you have two possibilities:

  • hook_install()
  • hook_enable()

hook_enable() is what the Forum module uses to create the forum vocabulary and the "taxonomy_forums" field it uses (see forum_enable()). I would use this hook if the module requires something to be set/present before it runs; differently from hook_install(), it has also the pro of not requiring a hook_update_N() to be added for those users who update the module, since hook_install() runs only during installation, while hook_enable() runs every time the module is enabled.

| improve this answer | |
  • i recreate my features module, applied this changes then download and updated my files, cache cleared but still no change for role developer for developer html means permission still unchecked – Somik May 22 '13 at 6:23
  • last help sir, i done with all changes according to your explained steps but still cant achieve this functionality Developer user role which should only access the developer_html input format – Somik May 24 '13 at 5:43
0

hook_update_N() should never be used for data migration or common work of a module. It's one and only purpose is to adjust data between module versions - not your case.

If you need to share initial setup, there are dozens of manuals on how to make a Drupal distribution easy to find using any major internet search engine. Just set what settings you need to share, make it a distribution and reuse.

If you need to keep it updated, you can wrap text formats and user roles in a Feature, set one site as a feature server and make others update from it. Or write own XML-based solution.

| improve this answer | |
  • ok i can understand we do not filter text before save in Drupal but we can do one thing we can grant permission to developer to access text format: developer_html from our module, sorry if i broke any rule or firing this same issues again and again, actually i already spent so much time for this – Somik May 20 '13 at 8:42

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.