Access input format based on user role 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 =&gt; $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 =&gt; $name) {
      user_role_grant_permissions($rid, $permissions);
    }
  }
}
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.



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-&gt;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.