Form to re-order roles or add a new one.
function user_admin_roles($form, $form_state) {
$roles = db_select('role', 'r')
->addTag('translatable')
->fields('r')
->orderBy('weight')
->orderBy('name')
->execute();
$form['roles'] = array(
'#tree' => TRUE,
);
$max_weight = 0;
foreach ($roles as $role) {
$max_weight = max($max_weight, $role->weight);
$form['roles'][$role->rid]['#role'] = $role;
$form['roles'][$role->rid]['#weight'] = $role->weight;
$form['roles'][$role->rid]['name'] = array(
'#markup' => check_plain($role->name),
);
$form['roles'][$role->rid]['weight'] = array(
'#type' => 'textfield',
'#title' => t('Weight for @title', array(
'@title' => $role->name,
)),
'#title_display' => 'invisible',
'#size' => 4,
'#default_value' => $role->weight,
'#attributes' => array(
'class' => array(
'role-weight',
),
),
);
$links['edit'] = array(
'title' => t('edit role'),
'href' => 'admin/people/roles/edit/' . $role->rid,
'weight' => 0,
);
$links['permissions'] = array(
'title' => t('edit permissions'),
'href' => 'admin/people/permissions/' . $role->rid,
'weight' => 5,
);
$form['roles'][$role->rid]['operations'] = array(
'#type' => 'operations',
'#links' => $links,
);
}
// Embed the role add form.
$add_role = (object) array(
'rid' => NULL,
'name' => NULL,
'weight' => $max_weight + 1,
);
$add_form = user_admin_role(array(), $form_state, $add_role);
$add_form['actions']['submit']['#submit'] = array(
'user_admin_role_submit',
);
$add_form['role']['actions'] = $add_form['actions'];
unset($add_form['actions']);
$form += $add_form;
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save order'),
// Do not validate the add form when saving the order.
'#limit_validation_errors' => array(
array(
'roles',
),
),
'#submit' => array(
'user_admin_roles_order_submit',
),
);
return $form;
}