diff -u b/core/modules/user/user.admin.inc b/core/modules/user/user.admin.inc --- b/core/modules/user/user.admin.inc +++ b/core/modules/user/user.admin.inc @@ -820,60 +820,62 @@ * @see theme_user_admin_roles() */ function user_admin_roles($form, $form_state) { - $roles = user_roles(); + $roles = db_select('role', 'r') + ->addTag('translatable') + ->fields('r') + ->orderBy('weight') + ->orderBy('name') + ->execute(); $form['roles'] = array( '#tree' => TRUE, ); - $order = 0; - foreach ($roles as $rid => $name) { - $form['roles'][$rid]['#role'] = (object) array( - 'rid' => $rid, - 'name' => $name, - 'weight' => $order, + $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'][$rid]['#weight'] = $order; - $form['roles'][$rid]['weight'] = array( + $form['roles'][$role->rid]['weight'] = array( '#type' => 'textfield', - '#title' => t('Weight for @title', array('@title' => $name)), + '#title' => t('Weight for @title', array('@title' => $role->name)), '#title_display' => 'invisible', '#size' => 4, - '#default_value' => $order, + '#default_value' => $role->weight, '#attributes' => array('class' => array('role-weight')), ); - $order++; + $form['roles'][$role->rid]['operations']['edit'] = array( + '#type' => 'link', + '#title' => t('edit role'), + '#href' => 'admin/people/permissions/roles/edit/' . $role->rid, + ); + $form['roles'][$role->rid]['operations']['permissions'] = array( + '#type' => 'link', + '#title' => t('edit permissions'), + '#href' => 'admin/people/permissions/' . $role->rid, + ); } - $form['name'] = array( - '#type' => 'textfield', - '#title' => t('Name'), - '#title_display' => 'invisible', - '#size' => 32, - '#maxlength' => 255, - ); - $form['rid'] = array( - '#type' => 'machine_name', - '#size' => 30, - '#maxlength' => 64, - '#machine_name' => array( - 'exists' => 'user_role_load', - 'source' => array('name'), - ), - // The machine name is not required here to avoid form errors when - // re-ordering the roles. - // @see user_admin_add_role_validate() - '#required' => FALSE, - ); - $form['add'] = array( - '#type' => 'submit', - '#value' => t('Add role'), - '#validate' => array('user_admin_add_role_validate'), - '#submit' => array('user_admin_role_submit'), - ); - $form['actions'] = array('#type' => 'actions'); + // 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'), ); @@ -881,18 +883,6 @@ } /** - * Form validation handler for the user_admin_roles() form. - */ -function user_admin_add_role_validate($form, &$form_state) { - if (empty($form_state['values']['name'])) { - form_set_error('name', t('You must specify a valid role name.')); - } - elseif (empty($form_state['values']['rid'])) { - form_set_error('rid', t('Machine-readable name is required.')); - } -} - -/** * Form submit function. Update the role weights. */ function user_admin_roles_order_submit($form, &$form_state) { @@ -916,25 +906,29 @@ function theme_user_admin_roles($variables) { $form = $variables['form']; + $form['role']['name']['#title_display'] = 'invisible'; + unset($form['role']['name']['#description']); + unset($form['role']['rid']['#description']); + + // Distribute role add form into table columns. + $form['roles']['add']['name'] = array(); + $form['roles']['add']['weight'] = $form['role']['weight']; + $form['roles']['add']['operations'] = $form['role']['actions']; + unset($form['role']['weight']); + unset($form['role']['actions']); + $form['roles']['add']['name'] = $form['role']; + unset($form['role']); + $header = array(t('Name'), t('Weight'), array('data' => t('Operations'), 'colspan' => 2)); foreach (element_children($form['roles']) as $rid) { - $name = $form['roles'][$rid]['#role']->name; $row = array(); - if (in_array($rid, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID))) { - $row[] = t('@name (locked)', array('@name' => $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); + foreach (element_children($form['roles'][$rid]) as $column) { + $row[] = drupal_render($form['roles'][$rid][$column]); } $rows[] = array('data' => $row, 'class' => array('draggable')); + if (isset($form['roles'][$rid]['weight'])) { + } } - $rows[] = array(array('data' => drupal_render($form['name']) . drupal_render($form['rid']) . drupal_render($form['add']), 'colspan' => 4, 'class' => 'edit-name')); drupal_add_tabledrag('user-roles', 'order', 'sibling', 'role-weight'); @@ -955,8 +949,12 @@ drupal_goto('admin/people/permissions/roles'); } - // Display the edit role form. - $form['name'] = array( + $form['role'] = array( + '#tree' => TRUE, + '#parents' => array('role'), + ); + + $form['role']['name'] = array( '#type' => 'textfield', '#title' => t('Role name'), '#default_value' => $role->name, @@ -965,23 +963,31 @@ '#maxlength' => 64, '#description' => t('The name for this role. Example: "moderator", "editorial board", "site architect".'), ); - $form['rid'] = array( + $form['role']['rid'] = array( '#type' => 'machine_name', '#default_value' => $role->rid, - '#disabled' => TRUE, + '#required' => TRUE, + '#disabled' => !empty($role->rid), + '#size' => 30, + '#maxlength' => 64, + '#machine_name' => array( + 'exists' => 'user_role_load', + 'source' => array('role', 'name'), + ), ); - $form['weight'] = array( + $form['role']['weight'] = array( '#type' => 'value', '#value' => $role->weight, ); $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array( '#type' => 'submit', - '#value' => t('Save role'), + '#value' => !empty($role->rid) ? t('Save role') : t('Add role'), ); $form['actions']['delete'] = array( '#type' => 'submit', '#value' => t('Delete role'), + '#access' => !empty($role->rid), '#submit' => array('user_admin_role_delete_submit'), ); @@ -992,24 +998,22 @@ * Form submit handler for the user_admin_role() form. */ function user_admin_role_submit($form, &$form_state) { - $role = (object) $form_state['values']; - if ($form_state['values']['op'] == t('Save role')) { - user_role_save($role); + $role = (object) $form_state['values']['role']; + $status = user_role_save($role); + if ($status === SAVED_UPDATED) { drupal_set_message(t('The role has been renamed.')); } - elseif ($form_state['values']['op'] == t('Add role')) { - user_role_save($role); + else { drupal_set_message(t('The role has been added.')); } $form_state['redirect'] = 'admin/people/permissions/roles'; - return; } /** * Form submit handler for the user_admin_role() form. */ function user_admin_role_delete_submit($form, &$form_state) { - $form_state['redirect'] = 'admin/people/permissions/roles/delete/' . $form_state['values']['rid']; + $form_state['redirect'] = 'admin/people/permissions/roles/delete/' . $form_state['values']['role']['rid']; } /** diff -u b/core/modules/user/user.css b/core/modules/user/user.css --- b/core/modules/user/user.css +++ b/core/modules/user/user.css @@ -15,19 +15,6 @@ } /** - * Override default textfield float to put the "Add role" button next to - * the input textfield. - */ -#user-admin-roles td.edit-name { - clear: both; -} -#user-admin-roles .form-item-name, -#user-admin-roles .form-item-rid { - float: left; /* LTR */ - margin-right: 1em; /* LTR */ -} - -/** * Password strength indicator. */ .password-strength { diff -u b/core/modules/user/user.install b/core/modules/user/user.install --- b/core/modules/user/user.install +++ b/core/modules/user/user.install @@ -97,7 +97,7 @@ // limit the maximum length. 'length' => 64, 'not null' => TRUE, - 'description' => 'Primary Key: Unique user role ID.', + 'description' => 'Primary Key: Unique role ID.', ), 'name' => array( 'type' => 'varchar', @@ -389,7 +389,7 @@ 'type' => 'varchar', 'length' => 64, 'not null' => TRUE, - 'description' => 'Primary Key: Unique user role ID.', + 'description' => 'Primary Key: Unique role ID.', ); db_change_field('role', 'rid', 'rid', $column); diff -u b/core/modules/user/user.module b/core/modules/user/user.module --- b/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -2650,24 +2650,10 @@ $query->innerJoin('role_permission', 'p', 'r.rid = p.rid'); $query->condition('p.permission', $permission); } - $result = $query->execute(); - - $roles = array(); - foreach ($result as $role) { - switch ($role->rid) { - // We only translate the built in role names - case DRUPAL_ANONYMOUS_RID: - if (!$membersonly) { - $roles[$role->rid] = t($role->name); - } - break; - case DRUPAL_AUTHENTICATED_RID: - $roles[$role->rid] = t($role->name); - break; - default: - $roles[$role->rid] = $role->name; - } + if ($membersonly) { + $query->condition('r.rid', DRUPAL_ANONYMOUS_RID, '!='); } + $roles = $query->execute()->fetchAllKeyed(); if (empty($permission)) { $user_roles[$cid] = $roles; diff -u b/core/modules/user/user.test b/core/modules/user/user.test --- b/core/modules/user/user.test +++ b/core/modules/user/user.test @@ -2008,7 +2008,7 @@ public static function getInfo() { return array( - 'name' => 'User role administration', + 'name' => 'Role administration', 'description' => 'Test adding, editing and deleting user roles and changing role weights.', 'group' => 'User', ); @@ -2029,7 +2029,7 @@ // correspond to an integer, to test that the role administration pages // correctly distinguish between role names and IDs.) $role_name = '123'; - $edit = array('name' => $role_name, 'rid' => $role_name); + $edit = array('role[name]' => $role_name, 'role[rid]' => $role_name); $this->drupalPost('admin/people/permissions/roles', $edit, t('Add role')); $this->assertText(t('The role has been added.'), t('The role has been added.')); $role = user_role_load($role_name); @@ -2042,7 +2042,7 @@ // Test renaming a role. $old_name = $role_name; $role_name = '456'; - $edit = array('name' => $role_name); + $edit = array('role[name]' => $role_name); $this->drupalPost("admin/people/permissions/roles/edit/{$role->rid}", $edit, t('Save role')); $this->assertText(t('The role has been renamed.'), t('The role has been renamed.')); $new_role = user_role_load($old_name); only in patch2: unchanged: --- a/core/modules/user/user-rtl.css +++ b/core/modules/user/user-rtl.css @@ -4,12 +4,6 @@ padding-right: 1.5em; } -#user-admin-roles .form-item-name { - float: right; - margin-left: 1em; - margin-right: 0; -} - /** * Password strength indicator. */