Creating a custom locked role Within Drupal 7 I am trying to modify the page myserver.com/admin/people/permissions/roles so that a custom role which I have added cannot be edited. In other words, I'd like it to be locked like the anonymous and administrator roles.

I have found one possible solution at http://drupal.org/node/397480 but I think it is a Drupal 6 fix.

I've tried modifying the render array for the content directly, but also can't figure that out quite right.

Here is the closest I've been able to get it. Any suggestions or solutions will be much appreciated.

function access_group_preprocess_page(&amp;$variables) {
  //check to see if the current page matches the path specified
  $match_path = explode('/', 'admin/people/permissions/roles');
  $match = TRUE;
  for ($i = 0; $i &lt; count($match_path); $i++) {
    if ($match_path[$i] != arg($i)) {
      $match = FALSE;
      break;
    }
  }

  //if there is a match, modify the page
  if($match){
    //define the locked roles
    $locked_roles = array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID,);
    $access_group_role = user_role_load_by_name(ACCESS_GROUP_ROLE);
    if ($access_group_role) $locked_roles[] = $access_group_role-&gt;rid;
    kpr($locked_roles);

    kpr($variables);

    //recreate the render array for the table
    $header = array(
        t('Name'),
        array(
            'data' =&gt; t('Operations'),
            'colspan' =&gt; 2
        )
    );
    foreach (user_roles() as $rid =&gt; $name) {
      $edit_permissions = l(t('edit permissions'), 'admin/user/permissions/'. $rid);
      if (!in_array($rid, $locked_roles)) {
        $rows[] = array($name, l(t('edit role'), 'admin/user/roles/edit/'. $rid), $edit_permissions);
      }else{
        $rows[] = array($name . " &lt;em&gt;(locked)&lt;/em&gt;", '', $edit_permissions);
      }
    }

    //$form = user_admin_role(array(), array(), );
    //$rows[] = array(drupal_render($form['name']), array('data' =&gt; drupal_render($form['submit']), 'colspan' =&gt; 2));
    $form = $variables['page']['content']['system_main'];
    $rows[] = array(
      array(
          'data' =&gt; drupal_render($form['name']) . drupal_render($form['add]']),
          'colspan' =&gt; 3,
      ),
    );


    $output = drupal_render_children($form);
    $output .= theme('table', array(
        'header' =&gt; $header,
        'rows' =&gt; $rows,
    ));

    $variables['page']['content'] = $output;
  }
}
Unfortunately this behaviour is hard coded into the theme_user_admin_roles() function, so you'll need to override that theme function to remove the link and add the locked label. You'll also need to override the access check for the role edit/deletion page so it can't be visited directly.

This is the theme override you'll need to use (it's exactly the same as theme_user_admin_roles() except it has an extra test for your custom role):

function MYTHEME_user_admin_roles($variables) {
  $my_role_id = function_to_get_custom_role_id();

  $form = $variables['form'];

  $header = array(t('Name'), t('Weight'), array(
      'data' =&gt; t('Operations'),
      'colspan' =&gt; 2,
    ));
  foreach (element_children($form['roles']) as $rid) {
    $name = $form['roles'][$rid]['#role']-&gt;name;
    $row = array();
    if (in_array($rid, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID, $my_role_id))) {
      $row[] = t('@name &lt;em&gt;(locked)&lt;/em&gt;', array('@name' =&gt; $name));
      $row[] = drupal_render($form['roles'][$rid]['weight']);
      $row[] = '';
      $row[] = l(t('edit permissions'), 'admin/people/permissions/' . $rid);
    }
    else {
      $row[] = check_plain($name);
      $row[] = drupal_render($form['roles'][$rid]['weight']);
      $row[] = l(t('edit role'), 'admin/people/permissions/roles/edit/' . $rid);
      $row[] = l(t('edit permissions'), 'admin/people/permissions/' . $rid);
    }
    $rows[] = array(
      'data' =&gt; $row,
      'class' =&gt; array('draggable'),
    );
  }
  $rows[] = array(array(
      'data' =&gt; drupal_render($form['name']) . drupal_render($form['add']),
      'colspan' =&gt; 4,
      'class' =&gt; 'edit-name',
    ));

  drupal_add_tabledrag('user-roles', 'order', 'sibling', 'role-weight');

  $output = theme('table', array('header' =&gt; $header, 'rows' =&gt; $rows, 'attributes' =&gt; array('id' =&gt; 'user-roles')));
  $output .= drupal_render_children($form);

  return $output;
}


And this is the code for the access override (needs to go in a custom module)

function MYMODULE_menu_alter(&amp;$items) {
  $items['admin/people/permissions/roles/edit/%user_role']['access callback'] = 'MYMODULE_user_roles_edit';
  $items['admin/people/permissions/roles/delete/%user_role']['access callback'] = 'MYMODULE_user_roles_edit';
}

function MYMODULE_user_roles_edit($role) {
  $my_role_id = function_to_get_custom_role_id();

  if ($role-&gt;rid == DRUPAL_ANONYMOUS_RID || 
    $role-&gt;rid == DRUPAL_AUTHENTICATED_RID ||
    $role-&gt;rid == $my_role_id) {
    return FALSE;
  }

  return user_access('administer permissions');
}