You are here

function set_role_users in Patterns 7.2

Wraps the call to drupal_form_submit() to assign roles.

Parameters

string $form_id String containing the form ID. In the case of custom functions the value is empty.:

array $form_state Set of values after parsing the action.:

1 string reference to 'set_role_users'
user_patterns in patterns_components/components/user.inc
Implements hook_patterns() for the user module.

File

patterns_components/components/user.inc, line 668

Code

function set_role_users($form_id, &$form_state) {

  //Transform names in identifiers for role and users
  $role = user_role_load_by_name($form_state['values']['role']);
  $uids = array();
  foreach ($form_state['values']['users'] as $username) {
    $u = user_load_by_name($username);
    $uids[] = $u->uid;
  }

  //Add the role to the list of uids
  user_multiple_role_edit($uids, 'add_role', $role->rid);
  $msg = count($uids) > 0 ? t('Role %role was assigned to the following users: %users.', array(
    '%role' => $form_state['values']['role'],
    '%users' => implode(', ', $uids),
  )) : t('Role %role was applied to no user.', array(
    '%role' => $form_state['values']['role'],
  ));
  return patterns_results(PATTERNS_SUCCESS, $msg);
}